实现此目的的一种方法是对字段使用muti_fields映射
description。多字段中的一个字段不应进行分析。数据重新索引后,您可以使用简单的布尔查询来实现所需的目标:例
创建索引:
put test{ "mappings": { "data" : { "properties": { "description" : { "type": "string", "fields": { "raw" : {"type": "string","index": "not_analyzed"} } } }} }}
索引数据:
put test/data/1 { "description" : "a", "test_id" : 10}put test/data/2{ "description" : "", "test_id" : 10}put test/data/3{ "description" : "hello", "test_id" : 10}put test/data/4{ "description": "a", "test_id" : 20}
查询:
post test/data/_search{ "query": { "filtered": { "query": { "bool": { "disable_coord": "true", "should": [ { "query_string": { "fields": [ "description" ], "query": "a" } }, { "constant_score": { "filter": { "term": { "description.raw": "" } }, "boost": 0.2 } }, { "constant_score": { "filter": { "exists": { "field": "description" } }, "boost": 0.1 } } ] } }, "filter": { "terms": { "test_id": [ 10 ] } } } }}
结果:
"hits": [ { "_index": "test", "_type": "data", "_id": "1", "_score": 0.5113713, "_source": { "description": "a", "test_id": 10 } }, { "_index": "test", "_type": "data", "_id": "2", "_score": 0.29277003, "_source": { "description": "", "test_id": 10 } }, { "_index": "test", "_type": "data", "_id": "3", "_score": 0.097590014, "_source": { "description": "hello", "test_id": 10 } } ]
查询空字符串:
{ "query": { "filtered": { "query": { "bool": { "disable_coord": "true", "should": [ { "query_string": { "fields": [ "description" ], "query": "" } }, { "constant_score": { "filter": { "term": { "description.raw": "" } }, "boost": 0.2 } }, { "constant_score": { "filter": { "exists": { "field": "description" } }, "boost": 0.1 } } ] } }, "filter": { "terms": { "test_id": [ 10 ] } } } }}
结果:
"hits": [ { "_index": "test", "_type": "data", "_id": "2", "_score": 1.3416407, "_source": { "description": "", "test_id": 10 } }, { "_index": "test", "_type": "data", "_id": "1", "_score": 0.44721356, "_source": { "description": "a", "test_id": 10 } }, { "_index": "test", "_type": "data", "_id": "3", "_score": 0.44721356, "_source": { "description": "hello", "test_id": 10 } } ]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)