弹性搜索带空格的术语

时间:2013-12-04 19:39:18

标签: 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"
      }
    }
  }
}'

之后;添加文档:

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

但我应该在“alias”字段中获取我的文档,我有一个“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
    }
  ]
}

任何提示?

编辑:当我使用实际类型运行analyze时,我得到了这个:

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

结果:

{
  "_index": "autocomplete",
  "_type": "geo",
  "_id": "_analyze",
  "exists": false
}

1 个答案:

答案 0 :(得分:3)

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"
        }
    }
}'
相关问题