一尘不染

elasticsearch带空格的术语

elasticsearch

我在为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”。

我尝试使用_analyzeAPI,并得到了我认为是预期令牌的正确答案:

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
}

阅读 463

收藏
2020-06-22

共1个答案

一尘不染

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"
        }
    }
}'
2020-06-22