我的ElasticSearch服务器包含以下格式的文档:
{ "_index": "xindex", "_type": "xtype", "_id": "1100", "_score": 3.00010, "_source": { "_id": "2333345", "field1": "11111111111111", "field2": "y", "name": "hello world", } }
我需要获取名称前缀为"hello wo"和field2 "y"的所有文档.尝试了很多查询,但都没有.有空格问题的前缀有各种解决方案,但是当为field2添加过滤/另一个查询时,结果会被破坏.
谢谢.
您可以通过3个步骤实现此目的:
将字段名称的映射更改为not_analyzed
使用match_phrase_prefix查询(此处的文档)
通过将此查询结果包装在已过滤的查询中并在字段2上使用值为"y" 的术语过滤器来过滤此查询结果
您可以看到它使用以下数据集:
PUT test/prefix/_mapping { "properties": { "name":{ "type": "string", "index": "not_analyzed" } } } //should match PUT test/prefix/2333345 { "field1": "11111111111111", "field2": "y", "name": "hello world" } //should match PUT test/prefix/1112223 { "field1": "22222222222222", "field2": "y", "name": "hello wombat" } //should not match (field2 value is different) PUT test/prefix/4445556 { "field1": "33333333333333", "field2": "z", "name": "hello world" } //should not match (second word not starting with wo) PUT test/prefix/4445556 { "field1": "33333333333333", "field2": "y", "name": "hello zombie" }
然后,查询是:
GET test/prefix/_search { "query": { "filtered": { "query": { "match_phrase_prefix" : { "name" : "hello wo" } }, "filter": { "term": { "field2": "y" } } } } }
按预期输出文件1112223和2333345:
{ "took": 20, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1.592944, "hits": [ { "_index": "test", "_type": "prefix", "_id": "2333345", "_score": 1.592944, "_source": { "field1": "11111111111111", "field2": "y", "name": "hello world" } }, { "_index": "test", "_type": "prefix", "_id": "1112223", "_score": 1.592944, "_source": { "field1": "22222222222222", "field2": "y", "name": "hello wombat" } } ] } }