O
OOR
文档
OOR
文档
简介
Getting Started
安装
快速开始
Query Flow
Core Concepts
Suffix
Schema
Condition Tree
Adapters
SQLite
PostgreSQL / MySQL
Elasticsearch
Integrations
Drizzle Integration
ES Client

#Elasticsearch

oor/es 把同一套 query 语义映射成 Elasticsearch body。

#导出

TypeScript
import {
  esQuery,
  esNode,
  esBody,
  esBodyNode,
  terms,
  dateGroup,
  range,
  avg,
  sum,
  min,
  max,
  unique
} from 'oor/es'

#esBody

TypeScript
import { avg, dateGroup, esBody, terms } from 'oor/es'

const body = esBody(
  {
    status: 'active',
    page: 2,
    size: 5,
    sort: 'createdAt',
    order: 'desc'
  },
  {
    soft: {
      mode: 'flag',
      field: 'isDeleted',
      del: true,
      keep: false
    }
  },
  {
    map: {
      status: 'status.keyword',
      createdAt: 'created_at',
      price: 'price_cents',
      isDeleted: 'is_deleted'
    },
    sortMap: {
      createdAt: 'created_at'
    },
    track: true,
    groups: {
      byStatus: terms('status', {
        size: 5,
        aggs: {
          byMonth: dateGroup('createdAt', {
            calendar: 'month',
            format: 'yyyy-MM',
            minCount: 0
          })
        }
      }),
      averagePrice: avg('price')
    }
  }
)

会返回:

  • query
  • from
  • size
  • sort
  • track_total_hits
  • aggs

#map 与 sortMap

TypeScript
{
  map: {
    status: 'status.keyword',
    createdAt: 'created_at'
  },
  sortMap: {
    createdAt: 'created_at'
  }
}

#聚合 helpers

TypeScript
terms('status', { size: 10 })
dateGroup('createdAt', { calendar: 'month' })
range('price', [
  { key: 'cheap', to: 100 },
  { key: 'mid', from: 100, to: 500 },
  { key: 'premium', from: 500 }
])
avg('price')
sum('price')
min('price')
max('price')
unique('userId')

dateGroup 需要 calendar 或 fixed;range 至少要有一项。

#下一步

  • ES Client
  • Drizzle 插件
  • 导出
  • esBody
  • map 与 sortMap
  • 聚合 helpers
  • 下一步