【问题标题】:How do I get Elasticsearch to ignore terms emptied by a char_filter?如何让 Elasticsearch 忽略 char_filter 清空的术语?
【发布时间】:2017-10-31 00:53:07
【问题描述】:

我有一组已编入索引的美国街道地址。源数据不完善,有时字段包含垃圾。具体来说,我有 zip5zip4 字段和一个 pattern_replace char_filter 可以去除任何非数字字符。当char_filter 最终替换所有内容(产生一个空字符串)时,匹配似乎仍然关注该字段。如果原始字段只是一个空字符串(而不是 null),也会发生同样的情况。我怎样才能设置它,使它只忽略空字符串的字段(通过源或 char_filter 的结果)?

示例

首先,让我们使用digits_only 模式替换器和使用它的分析器创建一个索引:

curl -XPUT "http://localhost:9200/address_bug" -d'
{
  "settings": {
    "index": {
      "number_of_shards": "4",
      "number_of_replicas": "1"
    },
    "analysis": {
      "char_filter" : {
        "digits_only" : {
          "type" : "pattern_replace",
          "pattern" : "([^0-9])",
          "replacement" : ""
        }
      },
      "analyzer" : {
        "zip" : {
          "type" : "custom",
          "tokenizer" : "keyword",
          "char_filter" : [
            "digits_only"
          ]
        }
      }
    }
  }
}'

现在,让我们创建一个使用分析器的映射(注意:我使用with_positions_offsets 来突出显示):

curl -XPUT "http://localhost:9200/address_bug/_mapping/address" -d'
{
  "address": {
    "properties": {
      "zip5": {
        "type" : "string",
        "analyzer" : "zip",
        "term_vector" : "with_positions_offsets"
      },
      "zip4": {
        "type" : "string",
        "analyzer" : "zip",
        "term_vector" : "with_positions_offsets"
      }
    }
  }
}'

现在我们的索引和类型已经设置好了,让我们索引一些不完美的数据:

curl -XPUT "http://localhost:9200/address_bug/address/1234" -d'
{
  "zip5" : "02144",
  "zip4" : "ABCD"
}'

好的,让我们搜索它,然后将其发送给explain 本身。在这种情况下,搜索词是 Street,因为在我的实际应用程序中,我有一个用于完整地址搜索的字段。

curl -XGET "http://localhost:9200/address_bug/address/_search?explain" -d'
{
  "query": {
    "match": {
      "zip4": "Street"
    }
  }
}'

而且,这是结果中有趣的部分:

"_explanation": {
   "value": 0.30685282,
   "description": "weight(zip4: in 0) [PerFieldSimilarity], result of:",
   "details": [
      {
         "value": 0.30685282,
         "description": "fieldWeight in 0, product of:",
         "details": [
            {
               "value": 1,
               "description": "tf(freq=1.0), with freq of:",
               "details": [
                  {
                     "value": 1,
                     "description": "termFreq=1.0"
                  }
               ]
            },
            {
               "value": 0.30685282,
               "description": "idf(docFreq=1, maxDocs=1)"
            },
            {
               "value": 1,
               "description": "fieldNorm(doc=0)"
            }
         ]
      }
   ]
}

(完整回复是in this gist。)

预期结果

我没想到会有任何点击。如果我改为使用 "zip4" : null 索引文档,则会产生预期结果:没有命中。

帮助?我在这里采取了正确的方法吗?在我的完整应用程序中,我对phone 字段使用了相同的技术,并且怀疑我的结果会遇到同样的问题。

【问题讨论】:

  • 您是否尝试使用停止令牌过滤器?用它过滤空字符串可能会奏效。

标签: elasticsearch


【解决方案1】:

正如@plmaheu 提到的,您可以使用停止令牌过滤器来完全删除 空字符串,例如,这是我测试过的配置 作品:

POST /myindex
{
  "settings": {
    "analysis": {
      "char_filter" : {
        "digits_only" : {
          "type" : "pattern_replace",
          "pattern" : "[^0-9]+",
          "replacement" : ""
        }
      },
      "filter": {
        "remove_empty": {
          "type": "stop",
          "stopwords": [""]
        }
      },
      "analyzer" : {
        "zip" : {
          "type" : "custom",
          "tokenizer" : "keyword",
          "char_filter" : [
            "digits_only"
          ],
          "filter": ["remove_empty"]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "zip": {
          "type": "string",
          "analyzer": "zip"
        }
      }
    }
  }
}

这里remove_empty 过滤器删除了停用词“”,如果您使用分析 字符串“abcd”上的 API,你会得到响应 {"tokens":[]},所以没有 如果邮政编码完全无效,令牌将被编入索引。

我也在搜索“foo”时测试了这个作品,没有找到结果。

【讨论】:

  • 是的,我认为这会奏效。我还在 zip5zip4multi_match cross_fields 查询中对其进行了测试,并且运行良好。
【解决方案2】:

您可以像这样使用length token filter

"filter": {
  "remove_empty": {
    "type": "length",
    "min": 1
  }
}

【讨论】:

    猜你喜欢
    • 2014-08-03
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2021-12-31
    • 2011-02-05
    • 1970-01-01
    • 2010-11-10
    相关资源
    最近更新 更多