查询elasticsearch返回所有文档

问题描述:

我想知道为什么搜索特定的术语返回索引的所有文档而不是包含所请求术语的文档。查询elasticsearch返回所有文档

这里的索引以及如何将它设置:射击时

{"test": "ijskoud"} 
{"test": "plaatstaal"} 
{"test": "kristalfabriek"} 

所以现在: (使用elasticsearch头插件浏览器的界面)

{ 
    "settings": { 
    "number_of_replicas": 1, 
    "number_of_shards": 1, 
    "analysis": { 
     "filter": { 
     "dutch_stemmer": { 
      "type": "dictionary_decompounder", 
      "word_list": [ 
      "koud", 
      "plaat", 
      "staal", 
      "fabriek" 
      ] 
     }, 
     "snowball_nl": { 
      "type": "snowball", 
      "language": "dutch" 
     } 
     }, 
     "analyzer": { 
     "dutch": { 
      "tokenizer": "standard", 
      "filter": [ 
      "length", 
      "lowercase", 
      "asciifolding", 
      "dutch_stemmer", 
      "snowball_nl" 
      ] 
     } 
     } 
    } 
    } 
} 

{ 
    "properties": { 
    "test": { 
     "type": "string", 
     "fields": { 
     "dutch": { 
      "type": "string", 
      "analyzer": "dutch" 
     } 
     } 
    } 
    } 
} 

于是我加了一些文档搜索“plaat”不知何故,人们会期望搜索将返回包含“plaatstaal”的文档。

{ 
    "match": { 
    "test": "plaat" 
    } 
} 

但是,保存我进一步搜索弹性搜索无论文本内容如何都会回退所有文档。 有什么我在这里失踪? 足够有趣:使用GET或POST时有所不同。在使用后者时不会返回匹配,GET将返回所有文档。

任何帮助,非常感谢。

您需要配置您的索引使用自定义分析:

PUT /some_index 
{ 
    "settings": { 
    ... 
    }, 
    "mappings": { 
    "doc": { 
     "properties": { 
     "test": { 
      "type": "string", 
      "analyzer": "dutch" 
     } 
     } 
    } 
    } 
} 

如果您有使用此分析仪和不想指定每个分析仪更多的领域,你可以像下面这样做在该指数的特定类型:

"mappings": { 
    "doc": { 
     "analyzer": "dutch" 
    } 
    } 

如果您希望您的所有类型的那个索引,使用自定义分析:

"mappings": { 
    "_default_": { 
     "analyzer": "dutch" 
    } 
    } 

要以一种简单的方式测试你的分析:

GET /some_index/_analyze?text=plaatstaal&analyzer=dutch 

这将是步骤的完整列表执行:

DELETE /some_index 

PUT /some_index 
{ 
    "settings": { 
    "number_of_replicas": 1, 
    "number_of_shards": 1, 
    "analysis": { 
     "filter": { 
     "dutch_stemmer": { 
      "type": "dictionary_decompounder", 
      "word_list": [ 
      "koud", 
      "plaat", 
      "staal", 
      "fabriek" 
      ] 
     }, 
     "snowball_nl": { 
      "type": "snowball", 
      "language": "dutch" 
     } 
     }, 
     "analyzer": { 
     "dutch": { 
      "tokenizer": "standard", 
      "filter": [ 
      "length", 
      "lowercase", 
      "asciifolding", 
      "dutch_stemmer", 
      "snowball_nl" 
      ] 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "doc": { 
     "properties": { 
     "test": { 
      "type": "string", 
      "analyzer": "dutch" 
     } 
     } 
    } 
    } 
} 

POST /some_index/doc/_bulk 
{"index":{}} 
{"test": "ijskoud"} 
{"index":{}} 
{"test": "plaatstaal"} 
{"index":{}} 
{"test": "kristalfabriek"} 

GET /some_index/doc/_search 
{ 
    "query": { 
    "match": { 
     "test": "plaat" 
    } 
    } 
} 

而且搜索结果:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1.987628, 
     "hits": [ 
     { 
      "_index": "some_index", 
      "_type": "doc", 
      "_id": "jlGkoJWoQfiVGiuT_TUCpg", 
      "_score": 1.987628, 
      "_source": { 
       "test": "plaatstaal" 
      } 
     } 
     ] 
    } 
} 
+0

谢谢!现在映射已经到位,您是否知道为什么搜索“plaat”不会返回包含“plaatstaal”的文档。我是否必须在请求体内声明分析器? – flipchip 2014-10-22 08:51:17

+0

你正在使用的完整命令是什么? – 2014-10-22 08:54:20

+0

'curl -XGET localhost:9200/foo/_search -d'{“query”:{“match”:{“test”:“plaat”}}}' ' – flipchip 2014-10-22 09:26:31

当您使用GET时,您不会传递请求正文,因此无需任何筛选即可执行搜索并返回所有文档。

当您使用POST时,您的搜索查询确实会传递。它不会返回任何内容,可能是因为您的文档没有按照您的预期进行分析。

+0

但是当使用curl在shell中执行搜索时,GET/POST没有区别吗? '卷曲-XGET本地主机:9200 /富/ _search -d '{ “查询”:{ “匹配”:{ “测试”: “plaatstaal”}}} ' ' – flipchip 2014-10-22 08:41:28