我會建議使用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"}
}
}
}
}'
你好imotov,感謝您的回答。 – Vinc
隨着match_phrase我必須有一個完全匹配有結果。 所以我嘗試了match_phrase_prefix,效果很好,但是如果我搜索「super-s」,我還需要一個「match_phrase_suffix」,我想要「我的超級字符串」。 事實上,我想有一個簡單的通配符,比如'* -str *',它可以匹配任何包含「-str」的slug 這是我遇到問題的「 - 」字符。任何時候,我有一個我的查詢,我沒有得到任何結果。 – Vinc
哦,我明白了。然後它是這樣的:http://stackoverflow.com/questions/6467067/how-to-search-for-a-part-of-a-word-with-elasticsearch/6471449#6471449 – imotov