【问题标题】:Elasticsearch query for distinct values with multiple conditionsElasticsearch 查询具有多个条件的不同值
【发布时间】:2020-09-21 05:23:57
【问题描述】:

我想在弹性搜索中执行以下操作。

从 class= 10 且年龄 = 15 的学生中选择不同的姓名、城市;

它应该返回名称和城市的所有不同值。如何执行此操作?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-query elassandra


    【解决方案1】:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-collapse。字段折叠在这里应该会有所帮助。

    {
        "query": { 
        "bool": {
         "must": [
           {"match": 
             {
             "class": 10
             }
           },
           {"match": 
             {
             "age": 15
             }
           }
         ] 
        }
      },
        "collapse" : {
            "field" : "name.keyword" ,
            "inner_hits" : {
              "name": "by_type",
                "collapse" : {"field" : "city.keyword"}
            }
        }, 
        "size": 100
    }
    

    上面的查询会在匹配到class和age后返回一个document par name和city。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多