【问题标题】:Exclude results that contains special characters from elasticsearch从 elasticsearch 中排除包含特殊字符的结果
【发布时间】:2016-07-14 01:43:43
【问题描述】:

我已经寻找了很多答案,但没有任何效果,所以这是我的问题,我有一个带有字符串类型字段“名称”的索引,我使用 match_phrase 进行简单的全文搜索,但该字段有时是由逗号、点、斜线或连字符分隔的几个单词组成的字符串复合词,例如“engineer,operator,maintenance”。我需要排除这些结果,例如,如果我有下一个名字:

  1. “工程师、操作员、维护人员”
  2. “工程师”
  3. “工业工程师”

如果我搜索“工程师”,我想得到最后两个结果并排除第一个。我尝试使用这样的 must not 子句:

"query": {
  "bool": {
    "must": {
      "match_phrase": {
        "name": "Vendedor"
      }
    },
    "must_not":{
      "match":{
        "name": "\."
      }
    }
  }
}

我也尝试过使用正则表达式,但它总是让我得到错误字符的结果:

"must_not":{
      "regexp":{
        "name": ".*[\-\.\/\.].*"
      }
    }

我做错了什么或者应该如何完成这项任务?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在索引设置中,您可以添加类似的字符过滤器和模式,但是您需要重新索引,请在此处查看更多详细信息https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-analyzer.html

                  "char_filter": {
                      "pattern": {
                         "pattern": "\\W+",
                         "type": "pattern_replace",
                         "replacement": " "
                      },
                      "html": {
                         "type": "html_strip"
                      }
                   }
    

    【讨论】:

    • 嗨,我不需要替换错误的字符,我需要的是排除包含这些字符的结果。我用一个例子编辑我的答案
    • 您好,ElasticSearch 中默认分析字符串字段。您是否尝试过在映射中将其设置为 not_analyzed? elastic.co/guide/en/elasticsearch/guide/current/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2020-05-08
    • 2014-02-12
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多