2012-11-15 53 views
2

我是Elasticsearch的新手,我在查詢時遇到了問題。Elasticsearch忽略單詞破壞者

我索引串那樣:

​​

此字符串是蛞蝓。 所以,他們是沒有空格的,只有字母數字字符。相關字段的映射僅爲「type = string」。

我使用的是這樣的查詢:

{ "query":{ "query_string":{ "query": "*"+<MY_QUERY>+"*", "rewrite": "top_terms_10" } }} 

其中 「MY_QUERY」 也是塞。例如,像「我的超級」之類的東西。

當搜索「我的」我得到的結果。

當搜索「我的超級」我沒有結果,我想有「我的超級字符串」。

有人可以幫助我嗎?謝謝!

回答

1

我會建議使用match_phrase,而不是使用帶有前導和尾隨通配符的查詢字符串。即使是標準分析儀也應該能夠正確地將slu split分成令牌,所以不需要通配符。

curl -XPUT "localhost:9200/slugs/doc/1" -d '{"slug": "my-super-string"}' 
echo 
curl -XPUT "localhost:9200/slugs/doc/2" -d '{"slug": "my-other-string"}' 
echo 
curl -XPUT "localhost:9200/slugs/doc/3" -d '{"slug": "my-little-string"}' 
echo 
curl -XPOST "localhost:9200/slugs/_refresh" 
echo 
echo "Searching for my" 
curl "localhost:9200/slugs/doc/_search?pretty=true&fields=slug" -d '{"query" : { "match_phrase": {"slug": "my"} } }' 
echo 
echo "Searching for my-super" 
curl "localhost:9200/slugs/doc/_search?pretty=true&fields=slug" -d '{"query" : { "match_phrase": {"slug": "my-super"} } }' 
echo 
echo "Searching for my-other" 
curl "localhost:9200/slugs/doc/_search?pretty=true&fields=slug" -d '{"query" : { "match_phrase": {"slug": "my-other"} } }' 
echo 
echo "Searching for string" 
curl "localhost:9200/slugs/doc/_search?pretty=true&fields=slug" -d '{"query" : { "match_phrase": {"slug": "string"} } }' 

或者,您也可以創建自己的分析,將只蛞蝓分成記號「 - 」

curl -XDELETE localhost:9200/slugs 
curl -XPUT localhost:9200/slugs -d '{ 
    "settings": { 
     "index": { 
      "number_of_shards": 1, 
      "number_of_replicas": 0, 
      "analysis": { 
       "analyzer" : { 
        "slug_analyzer" : { 
         "tokenizer": "slug_tokenizer", 
         "filter" : ["lowercase"] 
        } 
       }, 
       "tokenizer" :{ 
        "slug_tokenizer" : { 
         "type": "pattern", 
         "pattern": "-" 
        } 
       } 
      } 
     } 
    }, 
    "mappings" :{ 
     "doc" : { 
      "properties" : { 
       "slug" : {"type": "string", "analyzer" : "slug_analyzer"} 
      } 
     } 
    } 
}' 
+0

你好imotov,感謝您的回答。 – Vinc

+0

隨着match_phrase我必須有一個完全匹配有結果。 所以我嘗試了match_phrase_prefix,效果很好,但是如果我搜索「super-s」,我還需要一個「match_phrase_suffix」,我想要「我的超級字符串」。 事實上,我想有一個簡單的通配符,比如'* -str *',它可以匹配任何包含「-str」的slug 這是我遇到問題的「 - 」字符。任何時候,我有一個我的查詢,我沒有得到任何結果。 – Vinc

+0

哦,我明白了。然後它是這樣的:http://stackoverflow.com/questions/6467067/how-to-search-for-a-part-of-a-word-with-elasticsearch/64714​​49#64714​​49 – imotov