简体   繁体   English

ElasticSearch的独特搜索结果

[英]Unique search results from ElasticSearch

I am new to ElasticSearch and can't quite figure out what I want is possible or not. 我是ElasticSearch的新手,无法完全弄清楚我想要什么是可能的。

I can query like this: 我可以这样查询:

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

and it will return top search results and the term aggregation buckets. 它将返回最热门的搜索结果和术语汇总桶。 But ideally what I would like for the search results to return, is only one (perhaps the top one, does not matter) for each of unique Id's defined in the aggregation terms. 但是理想情况下,我希望搜索结果返回的只是聚合术语中定义的每个唯一ID的一个(也许是最重要的,无所谓)。

You can make use of Terms Aggregation along with the Top Hits Aggregation to give you the result you are looking for. 您可以将术语汇总热门匹配汇总结合使用,从而为您提供所需的结果。

Now once you do that, specify the size as 1 in the Top Hits Aggregation 现在,一旦完成,在“ 热门匹配”中将大小指定为1

Based on your query I've created sample mapping,documents, aggregation query and the response for your reference. 根据您的查询,我创建了示例映射,文档,聚合查询和响应供您参考。

Mapping: 制图:

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

Sample Documents: 样本文件:

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"
}

Query: 查询:

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
          }
        }
      }
    }
  }
}

Sample Response: 样本响应:

{
  "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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Notice that I am not returning any bool results in the above, the search result you are looking for comes in the form of Top Hits Aggregation. 请注意,上面我没有返回任何布尔结果,您要查找的搜索结果以“热门匹配”的形式出现。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM