代码之家  ›  专栏  ›  技术社区  ›  MondKin

如何使用通配符查询ElasticSearch

  •  0
  • MondKin  · 技术社区  · 6 年前

    message:org.jooq.exception.TooManyRowsException
    

    但是如果我这样做,我没有得到任何结果(即使它看起来像一个更广泛的查询):

    message:*TooManyRowsException
    

    此字段中的映射是:

    "messages": {
      "match": "*message",
      "match_mapping_type":"string",
      "mapping": {
        "type":"text",
        "analyzer":"custom_standard"
      }
    }
    

    * 是否与文本字段中的“任何字符”不匹配?

    顺便说一句,我也尝试过这些变化,但没有运气:

    message:*.TooManyRowsException
    message:"*TooManyRowsException"
    message:(*TooManyRowsException)
    message:TooManyRowsException
    

    可复制示例

    • curl -H 'Content-Type: application/json' -XPUT localhost:9200/twitter
      {
          "settings": {
              "analysis": {
                  "analyzer": {
                      "custom_standard": {
                          "type": "custom",
                          "tokenizer": "uax_url_email",
                          "filter": ["lowercase", "snowball", "stop"]
                      }
                  },
                  "normalizer": {
                      "custom_lowercase": {
                          "type": "custom",
                          "filter": ["lowercase"]
                      }
                  }
              }
          },
          "mappings": {
              "dynamic_templates": [
                  {
                      "messages": {
                          "match": "*message",
                          "match_mapping_type":"string",
                          "mapping": {
                              "type":"text",
                              "analyzer":"custom_standard"
                          }
                      }
                  }
              ]
          }
      }
    • 索引文档

      curl -s -H 'Content-Type: application/json' -XPOST localhost:9200/twitter/_doc
      {
         "message": "org.jooq.exception.TooManyRowsException"
      }
    • curl -s -H 'Content-Type: application/json' -XGET localhost:9200/twitter/_search
      {
          "query": {
              "query_string": {
                  "query": "message:org.jooq.exception.TooManyRowsException"
              }
          }
      }
    • 执行不产生结果的搜索

      curl -s -H 'Content-Type: application/json' -XGET localhost:9200/twitter/_search
      {
          "query": {
              "query_string": {
                  "query": "message:*TooManyRowsException"
              }
          }
      }
    0 回复  |  直到 6 年前
        1
  •  1
  •   Stefano Branco    6 年前

    忽略此项,请参见编辑2

    我想你确实误解了通配符的工作原理。你能偶然发布你的完整地图吗?我看你在用 dynamic mapping

    这是否是你想要的还很难说,但是如果你这样做是为了允许通配符搜索,你可能不需要这些。

    要执行通配符搜索,实际上只需要 * 在查询字符串的适当位置。

    {
        "query": {
            "query_string": {
                "query": "*.TooManyRowsException",
                "default_field" : "message",
                "allow_leading_wildcard": true
            }
        }
    }
    

    {
        "query": {
            "query_string": {
                "query": "*message:.TooManyRowsException",
                "allow_leading_wildcard": true
            }
        }
    }
    

    应该匹配所有以 message: 最后以 .TooManyRowsExceptions . 您还可以设置 default_field messages . 更多的信息可以找到 here . 另外,查询字符串查询不仅仅允许通配符搜索。它分析您的查询,并解析如下内容 AND OR . 就像@Gabriel提到的,如果你只想要通配符搜索 wildcard 查询可能是更好的选择:

    {
        "query": {
            "wildcard": {
                "message": {
                    "value": "*.TooManyRowsException",
                }
            }
        }
    }
    

    根据反馈更改查询示例

    编辑2:

    {
      "analyzer": "custom_standard",
      "text": "org.jooq.exception.TooManyRowsException"
    }
    

    {
      "tokens" : [
        {
          "token" : "org.jooq.exception.toomanyrowsexcept",
          "start_offset" : 0,
          "end_offset" : 39,
          "type" : "<ALPHANUM>",
          "position" : 0
        }
      ]
    }
    

    因此,您的初始查询将不匹配,而

    {
        "query": {
            "query_string": {
                "query": "message:*org.jooq.exception.toomanyrowsexcept"
            }
        }
    }