elasticsearch 带空格的术语的弹性搜索
我无法为elasticsearch实现自动完成,以下是我的设置: 为自动完成创建一个分析器elasticsearch 带空格的术语的弹性搜索,elasticsearch,elasticsearch,我无法为elasticsearch实现自动完成,以下是我的设置: 为自动完成创建一个分析器 curl -XPUT http://localhost:9200/autocomplete/ -d '{ "index": { "analysis": { "analyzer": { "placeNameIndexAnalyzer": { "type": "custom", "tokenizer": "keyword",
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
}
}
}
}
}'
然后,我使用“alias”属性中的分析器在autocomplete中创建一个类型:
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": []
}
}
及
但我应该在“别名”字段中获取我的文档,我有一个“bs as”
我尝试使用\u 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
}
]
}
有什么提示吗
编辑:当我使用实际类型运行analyze时,我得到以下结果:
curl -XGET 'localhost:9200/autocomplete/_analyze?analyzer=placeNameIndexAnalyzer' -d 'bs as'
结果:
{
"_index": "autocomplete",
"_type": "geo",
"_id": "_analyze",
"exists": false
}
在
q
参数上使用的查询字符串查询首先通过在空格上拆分来解析查询字符串。您需要将其替换为保留空间的其他内容。这里的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"
}
}
}'
非常感谢,我选择了另一种方法,但这回答了问题,谢谢。
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"
}
}
}'