根据数组计数筛选弹性数据

jv4diomz  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(275)

我们如何从下面的索引数据中提取至少有一个电话号码的候选人,以及必须和应该这样的其他条件?
使用弹性版本6*

{
            "_index": "test",
            "_type": "docs",
            "_id": "1271",
            "_score": 1.518617,
            "_source": {
                "record": {
                    "createdDate": "2020-10-16T10:49:51.53",
                    "phoneNumbers": [
                        {
                            "type": "Cell",
                            "id": 0,
                            "countryCode": "+1",
                            "phoneNumber": "7845200448",
                            "extension": "",
                            "typeId": 700
                        }
                    ]
                },
                "entityType": "Candidate",                   
                "dbId": "1271",
                "id": "1271"
            }
        }
66bbxpm5

66bbxpm51#

可以使用术语查询返回在提供的字段中包含一个或多个精确术语的文档。
搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "record.phoneNumbers.phoneNumber.keyword": [
              "7845200448"
            ]
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64388591",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "record": {
            "createdDate": "2020-10-16T10:49:51.53",
            "phoneNumbers": [
              {
                "type": "Cell",
                "id": 0,
                "countryCode": "+1",
                "phoneNumber": "7845200448",
                "extension": "",
                "typeId": 700
              }
            ]
          },
          "entityType": "Candidate",
          "dbId": "1271",
          "id": "1271"
        }
      }
    ]

更新1:版本7*
您需要使用脚本查询,根据提供的脚本筛选文档。

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['record.phoneNumbers.phoneNumber.keyword'].length > 0",
            "lang": "painless"
          }
        }
      }
    }
  }
}

对于版本6*

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['record.phoneNumbers.phoneNumber.keyword'].values.length > 0",
            "lang": "painless"
          }
        }
      }
    }
  }
}
0kjbasz6

0kjbasz62#

你可以用 exists 用于此目的的查询如下所示,与脚本相比,这是一个轻量级查询:

{
  "query": {
      "exists": {
        "field": "record.phoneNumbers.phoneNumber"
      }
  }
}

相关问题