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

时间:2019-02-06 15:10:16

标签: elasticsearch search whitespace hyphen

我已经为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"
    }
}

当我保留破折号时我就知道了这个人,但是空白查询没有结果

我使用弹性6.2.4

1 个答案:

答案 0 :(得分:1)

定义分析仪:

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

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

当然可以使用该分析器为您的文档建立索引。 “ analyzer”:“ yourAnalyzer”

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