python - 如何在ElasticSearch中正确使用 “term”过滤器?

标签 python elasticsearch

我在代码(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/

相关文章:

elasticsearch - 搜索在模糊查询中返回不同的值-Elasticsearch

python - DataFrame 需要帮助来组织 DF 的列和行

python - 如何将所有被调用的函数参数变成一个(Python 3)

python - 如果日期时间列值具有特定间隔,则获取行数

python - 使用 Python 正则表达式从字符串中提取门牌号和街道名称

elasticsearch - Elasticsearch过帐文档:FORBIDDEN/12/index只读/允许删除(api)]

python - 正确配置 gae_mini_profiler 时出现问题

elasticsearch - "Limit of total fields [1000] in index [] has been exceeded"在 Elasticsearch 中是什么意思

sql-server - 我们可以用 HBase 替换 SQL Server 吗?

elasticsearch - Elasticsearch查询计数索引中的文档数据数