【问题标题】:Elastic search query and only return top unique results弹性搜索查询,只返回最重要的唯一结果
【发布时间】:2023-03-09 00:16:01
【问题描述】:

试图在弹性搜索上运行术语查询,但不知道如何将返回限制为唯一的结果? 假设这是查询。

"query": {
    "bool": {
        "must": [{
            "terms": {
                "id": [
                    "1",
                    "2",
                    "3",
                ],
                "boost": 1.0
            }
        }],
        "adjust_pure_negative": true,
        "boost": 1.0
    }
},
"aggs": {
    "top-results": {
        "terms": {
            "field": "id"
        },
        "aggs": {
            "test": {
                "top_hits": {
                    "size": 1
                }
            }
        }
    }
} 

理想情况下,我希望只返回 3 个结果,每个结果与 1、2 或 3 的 id 匹配,但此查询返回的结果远不止这些。

【问题讨论】:

  • 您是否在寻找按 ID 查询?如果是,则有一个 IDs Query elastic.co/guide/en/elasticsearch/reference/current/…
  • Id 只是映射中的一个字段,多个条目可以有相同的 id 值,不一定是 ID,实际上可以是任何字段。我只想要与 id 的值匹配的第一个结果条目。 IE 索引可以有 10 个条目,其中 1 作为 id 的值,20 作为 2 等,但我只想要 id 值为 1 的第一个条目结果,id 值为 2 的第一个条目结果等

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

为了模仿您的场景,我们在 elasticsearch 中推送了一组 5 条不同薪水的员工记录。因此,我正在尝试获取列出的薪水,其中每个记录(热门)。

GET /employee/_doc/_search
{
  "query": {
    "bool": {
          "should": [
            { "match": { "salary": 90000 }},
            { "match": { "salary": 80000 }} 
          ]
    }
  },
  "size" : 0,
  "aggs": {
    "salaries": {
      "terms": {
        "field":   "salary",      
        "order": { "top_score": "desc" } 
      },
      "aggs": {
        "top_score": { "max":      { "script":  "_score"           }}, 
        "salary-num": { "top_hits": { "size": 1 }}   
      }
    }
  }
}

输出


{
...
  "aggregations" : {
    "salaries" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 80000,
          "doc_count" : 2,
          "top_score" : {
            "value" : 1.0
          },
          "salary-num" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "employee",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : 1.0,
                  "_source" : {
                    "id" : 10,
                    "name" : "Lydia",
                    "dept" : "HR",
                    "salary" : 80000
                  }
                }
              ]
            }
          }
        },
        {
          "key" : 90000,
          "doc_count" : 1,
          "top_score" : {
            "value" : 1.0
          },
          "salary-num" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "employee",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : 1.0,
                  "_source" : {
                    "id" : 20,
                    "name" : "Flora",
                    "dept" : "Accounts",
                    "salary" : 90000
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

【讨论】:

  • 这似乎在运行查询并对查询结果进行聚合,并且只计算唯一 ID。该查询返回与 id 值匹配的几千个条目,而我真的只想要 3 个条目。一个 id 值为 1,一个 id 值为 2,一个 id 值为 3。
  • 这接近我需要的,但不完全是。这似乎获取了索引中的所有唯一值并显示它们。有没有办法限制我的查询中只有 3 个 id?即只对查询结果运行聚合。
猜你喜欢
  • 2021-02-10
  • 2016-12-09
  • 1970-01-01
  • 2019-07-20
  • 2021-04-19
  • 2016-07-28
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多