我在代码(Python 3.6)中使用elasticsearch:
result_search = es.search(index="area", body={
"query": {
"bool": {
"filter": [
{ "term": { "country_id": "ID" }}
]
}
}
})
但它返回{'took': 0,
'timed_out': False,
'_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
'hits': {'total': {'value': 0, 'relation': 'eq'},
'max_score': None,
'hits': []}}
在数据中,country_id字段中有“ID”我怎么了?
如果我将“term”更改为“match”,它将返回某值(不是零结果),但是我想使用“term”,因为我只想过滤精确值
Python Elasticsearch
最佳答案
您将text
字段与default analyzer
一起使用了。
因此,ID
将作为id
存储在索引中。您可以通过http://host:port/indexName/_analyze
API进行确认。Term
查询不是full text search
查询意味着不对其进行分析。 match
查询是全文查询。
因此,当您搜索ID
时,它期望索引中包含ID
。因此,在使用term时不会返回任何内容。
因此,您需要将mapping
设置为使用term
查询。将keyword
设置为country_id
。然后,将不会对其进行分析,并且将在编写时将其存储为添加的内容。然后可以搜索相同的内容。
要么
您可以在查询时使用字段的default keyword
版本-词项查询使用country_id.keyword
,匹配查询使用country_id
。
关于python - 如何在ElasticSearch中正确使用 “term”过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63071125/