ElasticSearch数字匹配查询

时间:2019-03-21 03:01:38

标签: elasticsearch

如果我搜索带有数字的令牌,那么模糊状态将无法正常工作。

Dokument:
"Nice Photo S61"

为我提供查询结果

“不错”

但我不会立即使用

“ S6”

我没有得到结果

这是我的查询

GET /index1/_search
{
    "query": {
        "match" : {
         "url1" : {
             "query" : "s6",
             "fuzziness": "auto",
             "prefix_length": "3",
              "max_expansions": 6,
              "operator":  "and"
        }
    }
  }
}

这是我的索引设置:

  PUT /index1/_settings
{
  "settings": {
    "analysis": {
      "analyzer": {
        "product_analyzer": {
          "type": "custom",

          "tokenizer": "punctuation", 
          "filter": [
            "lowercase" 
          ]
        }
      },
      "tokenizer": {
        "punctuation": { 
          "type": "pattern",
          "pattern": "[-_.:~ ]"
        }
      }
      }
  },
  "mappings" : {
    "index1" : {
      "properties" : {
        "url":{
        "type" :"text",
        "analyzer" : "product_analyzer"
        }
        }
      }
    }
  }

2 个答案:

答案 0 :(得分:1)

这与数字无关。 将fuzziness设置为auto意味着根据您的条件自动生成编辑距离。

  

自动

     

根据术语的长度生成编辑距离。低和   可以选择提供高距离参数AUTO:[low],[high]。   如果未指定,则默认值为3和6,等效于   AUTO:3,6的长度:

     

0..2必须完全匹配

     

3..5允许一次编辑

     

> 5允许两次编辑AUTO通常应该是模糊性的首选值。

  

前缀长度

     

不会被“模糊化”的初始字符数。这有助于减少必须检查的术语数量。默认为0。

因此您可以将查询更改为此:

{
    "query": {
        "match" : {
         "url1" : {
             "query" : "S6",
             "fuzziness": "1",
             "prefix_length": "0",
              "max_expansions": 6,
              "operator":  "and"
        }
    }
  }
}

参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

答案 1 :(得分:0)

以下是一些注意事项。

  • 在您发布的示例中,您的查询尝试匹配字段url1,但是在映射中,您定义了字段url。我认为这只是您示例中的错字。
  • 在查询中,您设置的前缀长度为3个字符,但是随后搜索s6,这显然与长度条件不符。
  • 在查询中,将fuzziness的值设置为AUTO,对于字符串<= 2个字符,这意味着您的术语必须完全匹配(请参阅documentation