首页 > 解决方案 > How index the same field in multiple ways with wildcard in ElasticSearch

问题描述

I have the below mappings for a field ("name"):

            "name": {
                "analyzer": "ngram_analyzer",
                "search_analyzer": "keyword_analyzer",
                "type": "text",
                "fields": {
                    "raw": {
                        "type": "keyword"
                    }
                }
            }

It works fine and allows to search as both text and keyword. As per the ES documentation:

https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html A string field could be mapped as a text field for full-text search and as a keyword field for sorting or aggregation.

But I am trying to extend this mapping to also support wildcard search.

I tried to modify the mapping(for eg. like below) but couldn't get it working.

            "name": {
                "analyzer": "ngram_analyzer",
                "search_analyzer": "keyword_analyzer",
                "type": "text",
                "fields": [{
                    "raw": {
                        "type": "wildcard"
                    }
                }, {
                    "type": "keyword"
                }]
            }

Also tried with,

            "name": {
                "analyzer": "ngram_analyzer",
                "search_analyzer": "keyword_analyzer",
                "type": "text",
                "fields": [{
                    "raw": {
                        "type": "wildcard"
                    }
                }, {"raw": {
                    "type": "keyword"
                }}]
            }

How should the mapping look like to allow name to be searched as text, keyword and wildcard.

标签: amazon-web-serviceselasticsearchkibana

解决方案


您可以使用多字段以多种方式对字段进行索引name。修改后的索引映射将是

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ngram_analyzer",
        "search_analyzer": "keyword_analyzer",
        "fields": {
          "raw": {
            "type": "wildcard"
          },
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

现在您可以name用于基于文本的搜索、name.raw通配符搜索、name.keyword关键字搜索


推荐阅读