[英]Elastic search for terms with spaces
我在為Elasticsearch實現自動完成功能時遇到問題,這是我的設置:
創建自動完成的分析器
curl -XPUT http://localhost:9200/autocomplete/ -d '{
"index": {
"analysis": {
"analyzer": {
"placeNameIndexAnalyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"trim",
"lowercase",
"asciifolding",
"left_ngram"
]
}
},
"filter": {
"left_ngram": {
"type": "edgeNGram",
"side": "front",
"min_gram": 3,
"max_gram": 12
}
}
}
}
}'
然后,使用“別名”屬性中的分析器在自動完成中創建一個類型:
curl -XPUT http://localhost:9200/autocomplete/geo/_mapping/ -d '{
"geo": {
"properties": {
"application_id": {
"type": "string"
},
"alias": {
"type": "string",
"analyzer": "placeNameIndexAnalyzer"
},
"name": {
"type": "string"
},
"object_type": {
"type": "string"
}
}
}
}'
然后; 添加文檔:
curl -XPOST http://localhost:9200/autocomplete/geo -d '{
"application_id": "982",
"name": "Buenos Aires",
"alias": [
"bue",
"buenos aires",
"bsas",
"bs as",
"baires"
],
"object_type": "cities"
}'
當我運行以下命令時:
curl -XGET 'localhost:9200/autocomplete/geo/_search?q=alias:bs%20as'
結果是
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
和
curl -XGET 'localhost:9200/autocomplete/geo/_search?q=alias:bs as'
curl: (52) Empty reply from server
但是我應該在“別名”字段中有一個“ bs as”作為我的文檔。
我嘗試使用_analyze
API,得到了我認為是預期令牌的正確答案:
curl -XGET 'localhost:9200/autocomplete/_analyze?analyzer=placeNameIndexAnalyzer' -d 'bs as'
結果:
{
"tokens": [
{
"token": "bs ",
"start_offset": 0,
"end_offset": 5,
"type": "word",
"position": 1
},
{
"token": "bs a",
"start_offset": 0,
"end_offset": 5,
"type": "word",
"position": 1
},
{
"token": "bs as",
"start_offset": 0,
"end_offset": 5,
"type": "word",
"position": 1
}
]
}
有什么提示嗎?
編輯:當我用實際類型運行分析時,我得到以下信息:
curl -XGET 'localhost:9200/autocomplete/_analyze?analyzer=placeNameIndexAnalyzer' -d 'bs as'
結果:
{
"_index": "autocomplete",
"_type": "geo",
"_id": "_analyze",
"exists": false
}
q
參數上使用的query_string查詢首先通過在空格上分割查詢字符串來解析查詢字符串。 您需要用其他保留空間的東西替換它。 match
查詢將是一個不錯的選擇。 我還將使用其他分析器進行搜索-您無需在其中應用ngram:
curl -XPUT http://localhost:9200/autocomplete/ -d '
{
"index": {
"analysis": {
"analyzer": {
"placeNameIndexAnalyzer" : {
"type": "custom",
"tokenizer": "keyword",
"filter" : ["trim", "lowercase", "asciifolding", "left_ngram"]
},
"placeNameSearchAnalyzer" : {
"type": "custom",
"tokenizer": "keyword",
"filter" : ["trim", "lowercase", "asciifolding"]
}
},
"filter": {
"left_ngram": {
"type" : "edgeNGram",
"side" : "front",
"min_gram" : 3,
"max_gram" : 12
}
}
}
}
}'
curl -XPUT http://localhost:9200/autocomplete/geo/_mapping/ -d '
{
"geo": {
"properties": {
"application_id": {
"type": "string"
},
"alias": {
"type": "string",
"index_analyzer": "placeNameIndexAnalyzer",
"search_analyzer": "placeNameSearchAnalyzer"
},
"name": {
"type": "string"
},
"object_type": {
"type": "string"
}
}
}
}'
curl -XPOST "http://localhost:9200/autocomplete/geo?refresh=true" -d '
{
"application_id":"982",
"name":"Buenos Aires",
"alias":["bue", "buenos aires", "bsas", "bs as", "baires"],
"object_type":"cities"
}'
curl -XGET 'localhost:9200/autocomplete/geo/_search' -d '{
"query": {
"match": {
"alias": "bs as"
}
}
}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.