java查询elasticsearch_java – 与ElasticSearch完全匹配(在查询时)
将您执行术语过滤的字段编入索引为not_analyzed.例如,您可以将“country”字段索引为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();
请注意,term filters和term queries要求not_analyzed字段起作用(即返回完全匹配).