弹性搜索与空格的术语 [英] Elastic search for terms with spaces

查看:101
本文介绍了弹性搜索与空格的术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法实现弹性搜索的自动填充,这里是我的设置:

I'm having trouble implementing an autocomplete for elasticsearch, here's my setup:

创建一个分析器进行自动填充

Create an analyzer for autocomplete

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属性中的分析器创建自动填充类型:

Then I create a type in autocomplete, using the analyzer in the "alias" property:

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。

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

我尝试使用 _analyze API,而我认为是正确的答案与预期的令牌:

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'

结果:

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

任何提示?

编辑:当我使用实际类型进行分析时,我得到:

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

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

结果:

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


推荐答案

q 参数中使用的query_string查询首先解析查询字符串它在空间上您需要将其替换为保留空格的其他内容。 匹配查询将是一个很好的选择。我也会使用不同的分析器来进行搜索 - 你不需要在那里使用ngram:

The query_string query that is used on the q parameter parses the query string first by splitting it on spaces. You need to replace it with something else that preserves spaces. The match query would be a good choice here. I would also use different analyzer for searching - you don't need to apply ngram there:

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

这篇关于弹性搜索与空格的术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆