首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在查找重复项时指定弹性搜索聚合字段

问在查找重复项时指定弹性搜索聚合字段
EN

Stack Overflow用户
提问于 2021-03-31 22:47:30
回答 1查看 69关注 0票数 0

在查找副本时,我将使用以下ES查询:

代码语言:javascript
复制
"aggs": {
    "duplicates": {
        "terms": {
            "field": "phone",
            "min_doc_count": 2,
            "size": 99999,
            "order": {
                "_term": "asc"
            }
        },
        "aggs": {
            "_docs": {
                "top_hits": {
                    "size": 99999
                }
            }
        }
    }
}

它工作得很好,它返回在本例中是phone的键,并在其中返回所有匹配项。主要的问题是,在_source上它带来了所有东西,这是我的情况下的很多字段,我想指定只带我需要的字段。返回内容的示例:

代码语言:javascript
复制
        "duplicates": {
                "1": {
                    "key": "1",
                    "doc_count": 2,
                    "_docs": {
                        "hits": {
                            "total": 2,
                            "max_score": 1,
                            "hits": [
                                {
                                    "_index": "local:company_id:1:sync",
                                    "_type": "leads",
                                    "_id": "23",
                                    "_score": 1,
                                    "_source": {
                                        "id": 23,
                                        "phone": 123456,
                                        "areacode_id": 426,
                                        "areacode_state_id": 2,
                                        "firstName": "Brayan",
                                        "lastName": "Rastelli",
                                        "state": "", // .... and so on

我想指定将在_source上返回的字段,这可能吗?

我遇到的另一个问题是,我希望按特定字段(通过id)排序聚合结果,但是如果我把任何字段名而不是_term放在这里,它就会给出一个错误。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-01 02:22:37

在下面的示例中,带有id 29和23的文档具有相同的phone,因此它们是重复的。搜索查询将只显示两个字段,即id和phone (您可以根据您的情况更改这些字段),并根据id对最高点击结果进行排序。

使用索引数据、搜索查询和搜索结果添加工作示例

索引数据:

代码语言:javascript
复制
{
  "id": 29,
  "phone": 123456,
  "areacode_id": 426,
  "areacode_state_id": 2,
  "firstName": "Brayan",
  "lastName": "Rastelli",
  "state": ""
}
{
  "id": 23,
  "phone": 123456,
  "areacode_id": 426,
  "areacode_state_id": 2,
  "firstName": "Brayan",
  "lastName": "Rastelli",
  "state": ""
}
{
  "id": 30,
  "phone": 1235,
  "areacode_id": 92,
  "areacode_state_id": 10,
  "firstName": "Mark",
  "lastName": "Smith",
  "state": ""
}

搜索查询:

代码语言:javascript
复制
{
  "size": 0,
  "aggs": {
    "duplicates": {
      "terms": {
        "field": "phone",
        "min_doc_count": 2,
        "size": 99999
      },
      "aggs": {
        "_docs": {
          "top_hits": {
            "_source": {
              "includes": [
                "phone",
                "id"
              ]
            },
            "sort": [
              {
                "id": {
                  "order": "asc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

搜索结果:

代码语言:javascript
复制
"aggregations": {
    "duplicates": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 123456,
          "doc_count": 2,
          "_docs": {
            "hits": {
              "total": {
                "value": 2,
                "relation": "eq"
              },
              "max_score": null,
              "hits": [
                {
                  "_index": "66896259",
                  "_type": "_doc",
                  "_id": "1",
                  "_score": null,
                  "_source": {
                    "phone": 123456,
                    "id": 23
                  },
                  "sort": [
                    23                       // note this
                  ]
                },
                {
                  "_index": "66896259",
                  "_type": "_doc",
                  "_id": "2",
                  "_score": null,
                  "_source": {
                    "phone": 123456,
                    "id": 29
                  },
                  "sort": [
                    29                         // note this
                  ] 
                }
              ]
            }
          }
        }
      ]
    }
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66896259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档