如何让Elasticsearch忽略char_filter清空的术语?

时间:2015-04-17 15:08:02

标签: elasticsearch

我有一组美国街道地址,我已编入索引。源数据不完善,有时字段包含垃圾。具体来说,我有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"
          ]
        }
      }
    }
  }
}'

现在,让我们创建一个使用分析器的映射( NB:我正在使用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字段使用相同的技术,并怀疑我对结果有同样的问题。

2 个答案:

答案 0 :(得分: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"时也测试了这个效果,没有找到结果。

答案 1 :(得分:0)

你可以像这样使用length token filter

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