[在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"
}
}
]
}
}
}