Elasticsearch - 精确匹配和部分匹配的索引映射设置

时间:2015-10-14 07:37:45

标签: indexing tokenize elasticsearch kibana-4

我是elasticsearch的新手,我正在尝试学习如何使用最佳映射设置进行索引以实现以下目标。

如果我有这样的文件

{"name":"Galapagos Islands"}

我希望以下两个查询得到这个结果

1)部分匹配

{
    "query": {
        "match": {
            "name": "ga"
        }
    }
}

2)完全匹配

{
    "query": {
        "term": {
            "name": "Galapagos Islands"
        }
    }
}

根据我目前的设置。我能够实现部分匹配部分。但完全匹配不会返回任何结果。请在下面找到我编入索引的设置。

{
  "mappings": {
        "islands": {
            "properties": {
                "name":{
                    "type": "string",
                    "index_analyzer": "autocomplete",
                    "search_analyzer": "search_ngram"
                }
            }
        }
    },

  "settings":{
    "analysis":{
      "analyzer":{
        "autocomplete":{
          "type":"custom",
          "tokenizer":"standard",
          "filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ] 
        },
        "search_ngram": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      },
      "filter":{
        "ngram":{
          "type":"ngram",
          "min_gram":2,
          "max_gram":15
        }
      }
    }
  }
}

在字段上进行精确匹配和部分匹配的正确方法是什么?

更新

使用下面给出的设置重新创建索引。我的映射看起来像这样

curl -XGET 'localhost:9200/testing/_mappings?pretty'
{
  "testing" : {
    "mappings" : {
      "islands" : {
        "properties" : {
          "name" : {
            "type" : "string",
            "index_analyzer" : "autocomplete",
            "search_analyzer" : "search_ngram",
            "fields" : {
              "raw" : {
                "type" : "string",
                "analyzer" : "my_keyword_lowercase_analyzer"
              }
            }
          }
        }
      }
    }
  }
}

我的索引设置如下

{
  "mappings": {
        "islands": {
            "properties": {
                "name":{
                    "type": "string",
                    "index_analyzer": "autocomplete",
                    "search_analyzer": "search_ngram",
                    "fields": {
                      "raw": {
                          "type": "string",
                          "analyzer": "my_keyword_lowercase_analyzer"
                      }
                    }
                }
            }
        }
    },

  "settings":{
    "analysis":{
      "analyzer":{
        "autocomplete":{
          "type":"custom",
          "tokenizer":"standard",
          "filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ] 
        },
        "search_ngram": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        },
        "my_keyword_lowercase_analyzer": {
          "type": "custom",
          "filter": ["lowercase"],
          "tokenizer": "keyword"
        }
      },
      "filter":{
        "ngram":{
          "type":"ngram",
          "min_gram":2,
          "max_gram":15
        }
      }
    }
  }
}

以上所有内容,当我这样查询时

curl -XGET 'localhost:9200/testing/islands/_search?pretty' -d '{"query": {"term": {"name.raw" : "Galapagos Islands"}}}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

我的文件就是这个

curl -XGET 'localhost:9200/testing/islands/1?pretty'
{
  "_index" : "testing",
  "_type" : "islands",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source":{"name":"Galapagos Islands"}
}

1 个答案:

答案 0 :(得分:2)

name属性中添加一个应为not_analyzed的子字段。或者,如果您关心小写/大写,请使用keyword标记生成器和lowercase过滤器。

这应该按原样索引Galapagos,而不是修改。然后,您可以进行term搜索。

例如,keyword分析器和lowercase过滤器:

    "my_keyword_lowercase_analyzer": {
      "type": "custom",
      "filter": [
        "lowercase"
      ],
      "tokenizer": "keyword"
    }

映射:

        "properties": {
            "name":{
                "type": "string",
                "index_analyzer": "autocomplete",
                "search_analyzer": "search_ngram",
                "fields": {
                    "raw": {
                        "type": "string",
                        "analyzer": "my_keyword_lowercase_analyzer"
                    }
                }
            }
        }

要使用的查询是:

{
    "query": {
        "term": {
            "name.raw": "galapagos islands"
        }
    }
}

因此,您应该使用name(子字段),而不是使用相同的字段 - name.raw