Elastic search 带空格的术语

发布于 2021-02-01 11:50:38

我在为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
        }
      }
    }
  }
}'

然后,使用“ alias”
属性中的分析器在自动完成中创建一个类型:

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"
      }
    }
  }
}'

Afterwards; add a document:

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"
}'

When I run the following:

curl -XGET 'localhost:9200/autocomplete/geo/_search?q=alias:bs%20as'

result is

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

and

curl -XGET 'localhost:9200/autocomplete/geo/_search?q=alias:bs as'  
curl: (52) Empty reply from server

But I should be getting my document for in the “alias” field I have a “bs as”.

I tried using the _analyze API and I get what I think is the correct answer
with the expected tokens:

curl -XGET 'localhost:9200/autocomplete/_analyze?analyzer=placeNameIndexAnalyzer' -d 'bs as'

result:

{
  "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
    }
  ]
}

Any Hints?

EDIT: when I run analyze with the actual type I get this:

curl -XGET 'localhost:9200/autocomplete/_analyze?analyzer=placeNameIndexAnalyzer' -d 'bs as'

result:

{
  "_index": "autocomplete",
  "_type": "geo",
  "_id": "_analyze",
  "exists": false
}
关注者
0
被浏览
145
1 个回答
  • 面试哥
    面试哥 2021-02-01
    为面试而生,有面试问题,就找面试哥。

    参数上使用的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"
            }
        }
    }'
    


推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看