ElasticSearch 在查询中使用空格而不是破折号搜索带连字符的文本

ElasticSearch searching hyphened text with whitespace instead dash on the query

我已经用 firstName = "Jean-Marc" 为数据(人)建立了索引,我希望能够使用不同查询的组合找到这个人,例如 firstName "Jean-Marc"应该可以搜索: "Jean-Marc" 和 "Jean Marc"(带空格或破折号)

这是映射:

  "firstName": {
    "type": "keyword",
    "normalizer": "keyword_normalizer",
    "fields": {
      "analysed": {
        "type": "text",
        "analyzer": "hyphen_analyzer",
        "search_analyzer": "standard",
        "fielddata": true
      }
    }
  }

设置:

"char_filter": {
    "allowOnlyChar": {
        "pattern": "[^A-Za-z]",
        "type": "pattern_replace",
        "replacement": " "
    }
}

"analyzer": {
    "hyphen_analyzers": {
        "filter": "lowercase",
        "char_filter": [
            "allowOnlyChar"
        ],
        "type": "custom",
        "tokenizer": "standard"
    }
}

当我保留破折号时我得到了那个人,但空白查询没有结果

我用的是 elastic 6.2.4

定义您的分析器:

"char_filter": {
    "allowOnlyChar": {
        "pattern": "[^A-Za-z]",
        "type": "pattern_replace",
        "replacement": " "
    }
}

"analyzer": {
    "yourAnalyzer": {
        "filter": "lowercase",
        "char_filter": [
            "allowOnlyChar"
        ],
        "type": "custom",
        "tokenizer": "standard"
    }
}

当然还有使用此分析器为您的文档编制索引。 "analyzer": "yourAnalyzer"

link 文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html