2014-10-22 196 views
1

我想知道爲什麼搜索特定的術語返回索引的所有文檔而不是包含所請求術語的文檔。查詢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將返回所有文檔。

任何幫助,非常感謝。

回答

0

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

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

2

當您使用GET時,您不會傳遞請求正文,因此無需任何篩選即可執行搜索並返回所有文檔。

當您使用POST時,您的搜索查詢確實會傳遞。它不會返回任何內容,可能是因爲您的文檔沒有按照您的預期進行分析。

+0

但是當使用curl在shell中執行搜索時,GET/POST沒有區別嗎? '捲曲-XGET本地主機:9200 /富/ _search -d '{ 「查詢」:{ 「匹配」:{ 「測試」: 「plaatstaal」}}} ' ' – flipchip 2014-10-22 08:41:28

相關問題