• es示例。。。


    {
    “query” : {
    “bool” : {
    “must” : [
    {“term”:{“type”:2}},
    { “terms” : {“share_id” : [391,390]}}
    ]
    }
    }
    }

    {
    “query” : {
    “bool” : {
    “must” : [
    {“term”:{“status”:1}},
    {“bool”:{“should”:[{ “term” : {“share_id” : 390}},
    { “term” : {“share_id” : 391}}]}}

              ]
           }
    
    • 1
    • 2

    }
    }
    where status =1 and (share_id=390 or share_id=391)

    {
    “exists” : { “field” : “tags” } tags is not null
    “missing” : { “field” : “tags” } tags IS NULL
    }


    多词匹配
    minimum_should_match 百分比
    match operator默认or

    更高权重
    GET /_search
    {
    “query”: {
    “bool”: {
    “must”: {这些语句使用默认的 boost 值 1 。
    “match”: {
    “content”: {
    “query”: “full text search”,
    “operator”: “and”
    }
    }
    },
    “should”: [
    { “match”: {
    “content”: {
    “query”: “Elasticsearch”,
    “boost”: 3 权重配比 这条语句更为重要,因为它有最高的 boost 值。
    }
    }},
    { “match”: {
    “content”: {
    “query”: “Lucene”,
    “boost”: 2 这条语句比使用默认值的更重要,但它的重要性不及 Elasticsearch 语句。
    }
    }}
    ]
    }
    }
    }

    dis_max (Disjunction Max Query)将任何与任一查询匹配的文档作为结果返回,
    但只将最佳匹配的评分作为查询的评分结果返回
    {
    “query”: {
    “dis_max”: {
    “queries”: [
    { “match”: { “title”: “Brown fox” }},
    { “match”: { “body”: “Brown fox” }}
    ]
    }
    }
    }
    {
    “query”: {
    “dis_max”: {
    “queries”: [
    { “match”: { “title”: “Quick pets” }},
    { “match”: { “body”: “Quick pets” }}
    ],
    “tie_breaker”: 0.3 参数将其他匹配语句的评分也考虑其中
    }
    }
    }===》{
    “multi_match”: {
    “query”: “Quick brown fox”,
    “type”: “best_fields”, best_fields 、 most_fields 和 cross_fields (最佳字段(默认)、多数字段、跨字段)。
    “fields”: [ “title”, “body” ],
    “tie_breaker”: 0.3,
    “minimum_should_match”: “30%”
    }
    }


    字段中心式
    词中心式 词 peter 和 smith 都必须出现,但是可以出现在任意字段中

    {
    “query”: {
    “multi_match”: {
    “query”: “peter smith”,
    “type”: “cross_fields”,
    “fields”: [ “title^2”, “description” ]
    }
    }
    }title 字段的权重提升值为 2 , description 字段的权重提升值默认为 1

    {
    “query” :{
    “term”:{“city_id”:1001}

    },
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "building_id"
            },
            "aggs": { 
            "avg_price": { 
               "avg": {
                  "field": "unit_price" 
               }
            }
         }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    }==聚合 select avg(unit_price) where city_id=1001 group by building_id;


    {
    “size” : 0,
    “aggs”: {
    “colors”: {–自定义
    “terms”: {
    “field”: “color”
    },
    “aggs”: {
    “avg_price”: { “avg”: { “field”: “price” }
    },
    “make” : {–自定义
    “terms” : {
    “field” : “make”
    },
    “aggs” : {
    “min_price” : { “min”: { “field”: “price”} },
    “max_price” : { “max”: { “field”: “price”} }
    }
    }
    }
    }
    }
    }
    有四辆红色车。
    红色车的平均售价是 $32,500 美元。
    其中三辆红色车是 Honda 本田制造,一辆是 BMW 宝马制造。
    最便宜的红色本田售价为 $10,000 美元。
    最贵的红色本田售价为 $20,000 美元。

    {
    “size” : 0,
    “aggs”: {
    “sales”: {
    “date_histogram”: {
    “field”: “sold”,
    “interval”: “month”,
    “format”: “yyyy-MM-dd”,
    “min_doc_count” : 0,
    “extended_bounds” : {
    “min” : “2014-01-01”,
    “max” : “2014-12-31”
    }
    }
    }
    }
    }
    每月销售多少台汽车

    {
    “size” : 0,
    “query” : {
    “match” : {
    “make” : "ford"聚合操作在查询范围内(例如:所有文档匹配 ford )
    }
    },
    “aggs” : {
    “single_avg_price”: {
    “avg” : { “field” : “price” }
    },
    “all”: {
    “global” : {}, global 全局桶没有参数。
    “aggs” : {
    “avg_price”: {
    “avg” : { “field” : “price” }
    }

            }
        }
    }
    
    • 1
    • 2
    • 3

    }
    福特汽车与 所有 汽车平均售价的比较
    聚合操作针对所有文档,忽略汽车品牌。

    {
    “size” : 0,
    “query”:{
    “match”: {
    “make”: “ford”
    }
    },
    “aggs”:{
    “recent_sales”: {
    “filter”: {
    “range”: {
    “sold”: {
    “from”: “now-1M”
    }
    }
    },
    “aggs”: {
    “average_price”:{
    “avg”: {
    “field”: “price”
    }
    }
    }
    }
    }
    }
    上个月度ford汽车的平均售价。 和 ford汽车所有数据

    ???{
    “size” : 0,
    “query”: {
    “match”: {
    “make”: “ford”
    }
    },
    “post_filter”: {
    “term” : {
    “color” : “green”
    }
    },
    “aggs” : {
    “all_colors”: {
    “terms” : { “field” : “color” }
    }
    }
    }查询 部分找到所有的 ford 汽车,然后用 terms 聚合创建一个颜色列表。因为聚合对查询范围进行操作,颜色列表与福特汽车有的颜色相对应。
    最后, post_filter 会过滤搜索结果,只展示绿色 ford 汽车。这在查询执行过 后 发生,所以聚合不受影响。

  • 相关阅读:
    C#开发的OpenRA游戏之金钱系统(3)
    从一到无穷大 #17 Db2 Event Store,A Purpose-Built IoT Database Engine
    springMAC的原理以及概述
    UI自动化怎么做?不会代码能不能做
    简单的收入支出明细记账软件有哪些,哪些适合开店用?
    c++之new和delete
    解决Mac下窗口无法最大化的问题 - 只有最小化和全屏怎么够
    go语言初学03 连接mysql
    移植STM32官方加密库STM32Cryptographic
    MyBatis学习:使用Map的方法传递参数
  • 原文地址:https://blog.csdn.net/qin34/article/details/127887206