我有一个位置索引,其中包含很多位置名称及其各自的国家/地区。
然后,我想知道我们在该国家/地区是否具有标题为“ Berlin”的地点,国家/地区代码为“ DE”。
这是我的Java代码尝试:
SearchResponse response = client.prepareSearch("locations")
.setQuery(QueryBuilders.matchQuery("title", "Berlin"))
.setFilter(FilterBuilders.termFilter("country", "DE"))
.execute()
.actionGet();
但这给了我太多的答复,例如“ Zoo Berlin”的结果等等。我需要完全匹配。
(但是请注意,在其他情况下,需要此子字符串/文本搜索匹配。)
有没有一种方法可以在查询时而不是在编制索引时决定一个人想要哪种行为(准确文本还是分析文本)?
将未执行术语过滤的字段编入索引。例如,您可以将“国家/地区”字段索引为multi_field,其中一个子字段为not_analyzed:
"country": {
"type": "multi_field",
"fields": {
"country": {"type": "string", "index": "analyzed"},
"exact": {"type": "string","index": "not_analyzed"}
}
}
另外,您可以对“标题”字段执行相同的操作以执行术语查询:
"title": {
"type": "multi_field",
"fields": {
"title": {"type": "string", "index": "analyzed"},
"exact": {"type": "string","index": "not_analyzed"}
}
}
然后在查询时,如果您想要一个标题与“柏林”完全相同的标题,并由术语“ DE”过滤,请使用一个带有not_analyzed字段的术语查询和术语过滤器:
SearchResponse response = client.prepareSearch("locations")
.setQuery(QueryBuilders.termQuery("title.exact", "Berlin"))
.setFilter(FilterBuilders.termFilter("country.exact", "DE"))
.execute()
.actionGet();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句