使用elasticsearch中的自定义分析器实现自动完成

时间:2016-06-02 11:55:49

标签: elasticsearch

参考this post,我创建了以下映射:

POST music
{
  "song": {
    "settings": {
      "analysis": {
        "filter": {
          "nGram_filter": {
            "type": "nGram",
            "min_gram": 2,
            "max_gram": 20,
            "token_chars": [
              "letter",
              "digit",
              "punctuation",
              "symbol"
            ]
          },
          "analyzer": {
            "nGram_analyzer": {
              "type": "custom",
              "tokenizer": "whitespace",
              "filter": [
                "lowercase",
                "asciifolding",
                "nGram_filter"
              ]
            },
            "whitespace_analyzer": {
              "type": "custom",
              "tokenizer": "whitespace",
              "filter": [
                "lowercase",
                "asciifolding"
              ]
            }
          }
        }
      }
    },
    "mappings": {
      "song_field_1": {
        "type": "string",
        "index": "not_analyzed",
        "index_analyzer": "nGram_analyzer",
        "search_analyzer": "whitespace_analyzer"
      }
    }
  }
}

插入以下文件:

POST music/song
{
  "song_field_1" : "Premeditiated fella"
}

并发送了此查询:

POST music/song/_search
{
   "size": 10,
   "query": {
      "match": {
         "_all": {
            "query": "pre"
         }
      }
   }
}

我希望将文档作为自动完成选项,但没有得到任何结果。

1 个答案:

答案 0 :(得分:0)

您需要像这样创建索引:

POST music
{
  "settings": {
    "analysis": {
      "filter": {
        "nGram_filter": {
          "type": "nGram",
          "min_gram": 2,
          "max_gram": 20,
          "token_chars": [
            "letter",
            "digit",
            "punctuation",
            "symbol"
          ]
        },
        "analyzer": {
          "nGram_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "asciifolding",
              "nGram_filter"
            ]
          },
          "whitespace_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "asciifolding"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "song": {
      "properties": {
        "song_field_1": {
          "type": "string",
          "index_analyzer": "nGram_analyzer",
          "search_analyzer": "whitespace_analyzer"
        }
      }
    }
  }
}

所以:

  1. song进入mappings
  2. 由于您正在指定分析器
  3. ,因此无需"index": "not_analyzed"
相关问题