【问题标题】:Unique search results from ElasticSearch来自 ElasticSearch 的独特搜索结果
【发布时间】:2019-11-15 04:53:36
【问题描述】:

我是 ElasticSearch 的新手,不太清楚我想要什么是可能的。

我可以这样查询:

GET entity/_search
{
  "query": {
    "bool": { 
      "must": [
        { "match": { "searchField":   "searchValue" }}
      ]
    }
  },
      "aggs" : {
    "uniq_Id" : {
      "terms" : { "field" : "Id", "size":500 }
      }
  }
}

它会返回热门搜索结果和术语聚合桶。但理想情况下,对于聚合术语中定义的每个唯一 ID,我希望返回的搜索结果只有一个(也许是顶部的,无关紧要)。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用Terms Aggregation 和Top Hits Aggregation 来为您提供所需的结果。

    现在,一旦你这样做了,在Top Hits Aggregation 中将大小指定为1

    根据您的查询,我创建了示例映射、文档、聚合查询和响应供您参考。

    映射:

    PUT mysampleindex
    {
      "mappings": {
        "mydocs": {
          "properties": {
            "searchField":{
              "type": "text"
            },
            "Id": {
              "type": "keyword"
            }
          }
        }
      }
    }
    

    示例文件:

    POST mysampleindex/mydocs/1
    {
      "searchField": "elasticsearch",
      "Id": "1000"
    }
    
    POST mysampleindex/mydocs/2
    {
      "searchField": "elasticsearch is awesome",
      "Id": "1000"
    }
    
    POST mysampleindex/mydocs/3
    {
      "searchField": "elasticsearch is awesome",
      "Id": "1001"
    }
    
    POST mysampleindex/mydocs/4
    {
      "searchField": "elasticsearch is pretty cool",
      "Id": "1001"
    }
    
    POST mysampleindex/mydocs/5
    {
      "searchField": "elasticsearch is pretty cool",
      "Id": "1002"
    }
    

    查询:

    POST mysampleindex/_search
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "searchField": "elasticsearch"
              }
            }
          ]
        }
      },
      "aggs": {
        "myUniqueIds": {
          "terms": {
            "field": "Id",
            "size": 10
          },
          "aggs": {
            "myDocs": {
              "top_hits": {                     <---- Top Hits Aggregation
                "size": 1                       <---- Note this
              }
            }
          }
        }
      }
    }
    

    示例响应:

    {
      "took": 7,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "myUniqueIds": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "1000",
              "doc_count": 2,
              "myDocs": {
                "hits": {
                  "total": 2,
                  "max_score": 0.2876821,
                  "hits": [
                    {
                      "_index": "mysampleindex",
                      "_type": "mydocs",
                      "_id": "1",
                      "_score": 0.2876821,
                      "_source": {
                        "searchField": "elasticsearch",
                        "Id": "1000"
                      }
                    }
                  ]
                }
              }
            },
            {
              "key": "1001",
              "doc_count": 2,
              "myDocs": {
                "hits": {
                  "total": 2,
                  "max_score": 0.25316024,
                  "hits": [
                    {
                      "_index": "mysampleindex",
                      "_type": "mydocs",
                      "_id": "3",
                      "_score": 0.25316024,
                      "_source": {
                        "searchField": "elasticsearch is awesome",
                        "Id": "1001"
                      }
                    }
                  ]
                }
              }
            },
            {
              "key": "1002",
              "doc_count": 1,
              "myDocs": {
                "hits": {
                  "total": 1,
                  "max_score": 0.2876821,
                  "hits": [
                    {
                      "_index": "mysampleindex",
                      "_type": "mydocs",
                      "_id": "5",
                      "_score": 0.2876821,
                      "_source": {
                        "searchField": "elasticsearch is pretty cool",
                        "Id": "1002"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
    

    请注意,我在上面没有返回任何 bool 结果,您要查找的搜索结果以 Top Hits Aggregation 的形式出现。

    希望这会有所帮助!

    【讨论】:

    • 这正是我所需要的。谢谢卡马尔!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多