在弹性搜索中如何过滤多个字段的结果集
In Elastic search how to filter result set on multiple fields
有人可以帮助我进行搜索查询,了解如何根据 2 个字段过滤结果吗?我已经建立了一个包含 1000 个文档的索引,从 UI 我们将调用这个索引,它由 2 个搜索字段组成
- 按邮政编码搜索
- 并按 city/state
搜索
基于这些组合,我们需要仅显示该邮政编码范围内的结果。
映射
{
"mappings": {
"properties": {
"address": {
"properties": {
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"zipcode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"startdate": {
"type": "date"
},
"enddate": {
"type": "date"
},
"customerstatus": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"customerid": {
"type": "long"
}
}
},
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
}
}
查询
{
"from": 0,
"size": 100,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "32081",
"fields": ["address.zipcode" ]
}
},
{
"query_string": {
"query": "FL",
"fields": ["address.cityname","address.state" ]
}
}
]
}
}
}
结果集
{
"customerid":1,
"customerstatus": Active,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
},
{
"customerid":2,
"customerstatus": Pending,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2018-01-01",
"enddate": "2019-01-01"
},
{
"customerid":3,
"customerstatus": Pending,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-06-01",
"enddate": "2021-06-01"
},
{
"customerid":4,
"customerstatus": Pending,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2021-01-01",
"enddate": "2022-01-01"
},
{
"customerid":5,
"customerstatus": Inactive,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
},
{
"customerid":6,
"customerstatus": cancelled,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
}
现在的要求是,
- 排除客户状态为 Inactive 和 Cancelled 的结果(不应显示客户 5 和 6)
- 仅显示活动和待定
- 如果状态为待定,则显示结束日期小于 500 天且结束日期不超过 91 天的客户
那么,我怎样才能在我的结果集中只得到 customerid 1 和 3。
您可以使用 bool query along with range query 的组合来根据天数范围查找文档。试试下面的查询
{
"from": 0,
"size": 100,
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"customerstatus": "pending"
}
},
{
"range": {
"enddate": {
"gt": "now-500d/d",
"lte": "now+91d/d"
}
}
}
]
}
},
{
"match": {
"customerstatus": "active"
}
}
],
"must_not": {
"terms": {
"customerstatus.keyword": [
"Inactive",
"cancelled"
]
}
}
}
}
}
搜索结果将是
"hits": [
{
"_index": "67260491",
"_type": "_doc",
"_id": "3",
"_score": 1.6931472,
"_source": {
"customerid": 3,
"customerstatus": "Pending",
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-06-01",
"enddate": "2021-06-01"
}
},
{
"_index": "67260491",
"_type": "_doc",
"_id": "1",
"_score": 1.5404451,
"_source": {
"customerid": 1,
"customerstatus": "Active",
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
}
}
]
有人可以帮助我进行搜索查询,了解如何根据 2 个字段过滤结果吗?我已经建立了一个包含 1000 个文档的索引,从 UI 我们将调用这个索引,它由 2 个搜索字段组成
- 按邮政编码搜索
- 并按 city/state 搜索
基于这些组合,我们需要仅显示该邮政编码范围内的结果。
映射
{
"mappings": {
"properties": {
"address": {
"properties": {
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"zipcode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"startdate": {
"type": "date"
},
"enddate": {
"type": "date"
},
"customerstatus": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"customerid": {
"type": "long"
}
}
},
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
}
}
查询
{
"from": 0,
"size": 100,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "32081",
"fields": ["address.zipcode" ]
}
},
{
"query_string": {
"query": "FL",
"fields": ["address.cityname","address.state" ]
}
}
]
}
}
}
结果集
{
"customerid":1,
"customerstatus": Active,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
},
{
"customerid":2,
"customerstatus": Pending,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2018-01-01",
"enddate": "2019-01-01"
},
{
"customerid":3,
"customerstatus": Pending,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-06-01",
"enddate": "2021-06-01"
},
{
"customerid":4,
"customerstatus": Pending,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2021-01-01",
"enddate": "2022-01-01"
},
{
"customerid":5,
"customerstatus": Inactive,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
},
{
"customerid":6,
"customerstatus": cancelled,
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
}
现在的要求是,
- 排除客户状态为 Inactive 和 Cancelled 的结果(不应显示客户 5 和 6)
- 仅显示活动和待定
- 如果状态为待定,则显示结束日期小于 500 天且结束日期不超过 91 天的客户
那么,我怎样才能在我的结果集中只得到 customerid 1 和 3。
您可以使用 bool query along with range query 的组合来根据天数范围查找文档。试试下面的查询
{
"from": 0,
"size": 100,
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"customerstatus": "pending"
}
},
{
"range": {
"enddate": {
"gt": "now-500d/d",
"lte": "now+91d/d"
}
}
}
]
}
},
{
"match": {
"customerstatus": "active"
}
}
],
"must_not": {
"terms": {
"customerstatus.keyword": [
"Inactive",
"cancelled"
]
}
}
}
}
}
搜索结果将是
"hits": [
{
"_index": "67260491",
"_type": "_doc",
"_id": "3",
"_score": 1.6931472,
"_source": {
"customerid": 3,
"customerstatus": "Pending",
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-06-01",
"enddate": "2021-06-01"
}
},
{
"_index": "67260491",
"_type": "_doc",
"_id": "1",
"_score": 1.5404451,
"_source": {
"customerid": 1,
"customerstatus": "Active",
"address": {
"city": "PONTE VEDRA",
"state": "FL",
"zipcode": "32081"
},
"startdate": "2020-07-15",
"enddate": "2021-07-15"
}
}
]