如何编写匹配然后过滤记录的弹性搜索查询?
How to write an elastic search query that matches and then filters the records?
在 Elastic Search 中,我编写了一个匹配搜索词并获取结果的查询。
这是我使用的查询。
{
"size": 40,
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query": "Salem Chennai",
"fuzziness": 1,
"prefix_length": 2,
"operator": "or"
}
}
}
]
}
}
}
这是我得到的回复:
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 11.182817,
"hits": [
{
"_index": "locations",
"_type": "city",
"_id": "1610",
"_score": 11.182817,
"_source": {
"name": "Chennai",
"code": "IN-TN-CENAI",
"province_name": "Tamil Nadu",
"province_code": "IN-TN",
"country_name": "India",
"country_code": "IN"
}
},
{
"_index": "locations",
"_type": "city",
"_id": "24216",
"_score": 9.688159,
"_source": {
"name": "Salem",
"code": "US-IN-SALEM",
"province_name": "Indiana",
"province_code": "US-IN",
"country_name": "United States",
"country_code": "US"
}
}
]
}
现在我想过滤 country_code 上的回复。
所以我尝试了这个查询
{
"size": 40,
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query": "Salem Chennai",
"fuzziness": 1,
"prefix_length": 2,
"operator": "or"
}
}
}
],
"filter": [
{
"term": {
"country_name": "India"
}
}
]
}
}
}
这是索引映射:
{
"locations" : {
"mappings" : {
"city" : {
"properties" : {
"code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_country_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_country_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_country_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
我应该得到一个结果,但是,这个查询没有给我任何结果。
有人可以帮我写一个合适的查询来匹配然后从弹性搜索中过滤记录吗?
您应该使用关键字进行过滤,如下所示:
{
"size": 40,
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query": "Salem Chennai",
"fuzziness": 1,
"prefix_length": 2,
"operator": "or"
}
}
}
],
"filter": [
{
"term": {
"country_name.keyword": "India"
}
}
]
}
}
}
字段 country_name
(没有 .keyword)在索引时被分析(规范化),因此根据您的分析器,它已被转换为其他内容(例如小写,可能是词干,...) .
在 Elastic Search 中,我编写了一个匹配搜索词并获取结果的查询。 这是我使用的查询。
{
"size": 40,
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query": "Salem Chennai",
"fuzziness": 1,
"prefix_length": 2,
"operator": "or"
}
}
}
]
}
}
}
这是我得到的回复:
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 11.182817,
"hits": [
{
"_index": "locations",
"_type": "city",
"_id": "1610",
"_score": 11.182817,
"_source": {
"name": "Chennai",
"code": "IN-TN-CENAI",
"province_name": "Tamil Nadu",
"province_code": "IN-TN",
"country_name": "India",
"country_code": "IN"
}
},
{
"_index": "locations",
"_type": "city",
"_id": "24216",
"_score": 9.688159,
"_source": {
"name": "Salem",
"code": "US-IN-SALEM",
"province_name": "Indiana",
"province_code": "US-IN",
"country_name": "United States",
"country_code": "US"
}
}
]
}
现在我想过滤 country_code 上的回复。
所以我尝试了这个查询
{
"size": 40,
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query": "Salem Chennai",
"fuzziness": 1,
"prefix_length": 2,
"operator": "or"
}
}
}
],
"filter": [
{
"term": {
"country_name": "India"
}
}
]
}
}
}
这是索引映射:
{
"locations" : {
"mappings" : {
"city" : {
"properties" : {
"code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_country_code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_country_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_country_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
我应该得到一个结果,但是,这个查询没有给我任何结果。 有人可以帮我写一个合适的查询来匹配然后从弹性搜索中过滤记录吗?
您应该使用关键字进行过滤,如下所示:
{
"size": 40,
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query": "Salem Chennai",
"fuzziness": 1,
"prefix_length": 2,
"operator": "or"
}
}
}
],
"filter": [
{
"term": {
"country_name.keyword": "India"
}
}
]
}
}
}
字段 country_name
(没有 .keyword)在索引时被分析(规范化),因此根据您的分析器,它已被转换为其他内容(例如小写,可能是词干,...) .