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

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

最佳答案

向您的 name 属性添加一个子字段,该属性应为 not_analyzed。或者,如果您关心小写/大写,可以使用关键字分词器和小写过滤器。

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

例如,关键字分析器与小写过滤器一起使用:

    "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.raw (子字段),而不是使用相同的字段 - name

关于indexing - Elasticsearch - 用于精确匹配和部分匹配的索引映射设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33119360/

相关文章:

tokenize - Elasticsearch - 如何在使用小写过滤器时保留大写首字母缩略词?

indexing - cassandra:带索引表达式的查询不起作用

javascript - 如何将日期转换为列表中的随机索引?

sql - 重建数据库中的所有索引

json - Elasticsearch 7.8.1:批量添加json错误无法读取数据

ruby-on-rails - 如何构建任务 'elasticsearch:import:model'

c# - 如何获取Elasticsearch Nest查询C#的过去2小时的日志?

sql - 如何删除外键引用的唯一索引?

java - 输入系统,用户输入对象的数组位置,后跟 # 来指示数量,但它给了我一个错误

在 Lucene 中将 n 个单词表达式索引为单个术语