Elatsticsearch部分不区分大小写的匹配

时间:2020-06-22 13:57:53

标签: elasticsearch

我正在尝试在Elasticseach 7中实现部分区分大小写的匹配。

我正在使用以下设置创建索引:

{
  "merchant_3" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "2",
        "provided_name" : "merchant_3",
        "max_result_window" : "100000",
        "creation_date" : "1592833582520",
        "analysis" : {
          "analyzer" : {
            "englishAnalyzer" : {
              "filter" : [
                "lowercase"
              ],
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "5mjRMQ65TSGFFU0LfAH4eA",
        "version" : {
          "created" : "7060299"
        }
      }
    }
  }
}

和映射:

{
  "merchant_3" : {
    "mappings" : {
      "properties" : {
        "Name" : {
          "type" : "keyword"
        },
        ...
      }
    }
  }
}

以下查询正确返回了文档:

POST /merchant/_search
{
  "query": {
    "wildcard": {
        "Name": "*Example*"
    }
  }
}

但是当我将搜索词小写时,它不会返回文档:

POST /merchant/_search
{
  "query": {
    "wildcard": {
        "Name": "*example*"
    }
  }
}

如何使用小写搜索词配置Elasticsearch以使其与Name字段值匹配?

1 个答案:

答案 0 :(得分:1)

如评论中所述,当前方法存在一些缺陷,并且由于您没有提到用例,我建议阅读opster's blog on autocomplete,其中讨论了各种方法的权衡和{{3 }}解释了您应该考虑的各种功能和非功能需求。

在您的情况下,我正在使用const path = require("path"); module.exports = { //... resolve: { alias: { "animation.gsap": path.resolve( "node_modules", "scrollmagic/scrollmagic/uncompressed/plugins/animation.gsap.js" ), }, }, }; 添加索引时间方法,如果您需要部分搜索的前缀类型,可以将其更改为ngram analyzer

索引映射

edge ngram

索引样本文档

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "ngram",
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    },
    "index.max_ngram_diff": 5 // note this
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "autocomplete", // note this
        "search_analyzer": "standard" // note this
      }
    }
  }
}

使用{ "title" : "Example movie" }

搜索
Example

结果

{
    "query": {
        "match" : {
            "title" : "Example"
        }
    }
}

使用小写字母"hits": [ { "_index": "testpartial", "_type": "_doc", "_id": "1", "_score": 0.471659, "_source": { "title": "Example movie" } } ] 进行搜索也会产生相同的结果,只需更改上一个查询中的搜索词即可。

相关问题