无法为 ElasticSearch 编写通配符查询?

Not able to write the wildcard query for ElasticSearch?

我在使用通配符时遇到问题。每当我尝试使用通配符查找数据时,它都会返回一个错误

ES 中的数据集如下所示:

   "hits": {
      "total": 1000,
      "max_score": 1,
      "hits": [
         {
            "_index": "accounts",
            "_type": "data",
            "_id": "25",
            "_score": 1,
            "_source": {
               "account_number": 25,
               "balance": 40540,
               "firstname": "Virginia",
               "lastname": "Ayala",
               "age": 39,
               "gender": "F",
               "address": "171 Putnam Avenue",
               "employer": "Filodyne",
               "email": "virginiaayala@filodyne.com",
               "city": "Nicholson",
               "state": "PA"
            }
         }

我使用以下查询:

GET /_search
{
    "query":{       
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" :
          {
            "match":{
            "wildcard":{"lastname" : "Aya*" }
            }
          }      
        }
      }
    }
    }
}

但它抛出以下错误:

{
   "error": {
      "root_cause": [
         {
            "type": "query_parsing_exception",
            "reason": "[match] query does not support [lastname]",
            "index": "accounts",
            "line": 9,
            "col": 25
         }

我试过在没有匹配实例的情况下只使用查询中的通配符,但我仍然无法获取数据。此处搜索成功但没有返回数据

没有匹配查询:

GET /_search
{
    "query":{       
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" :
          {
            "wildcard":{"lastname" : "Aya*" }
          }      
        }
      }
    }
    }
}

请帮助我了解应该如何设置查询。我也必须在 JAVA API 中使用此查询字符串。因此,关于这方面的任何建议也会非常有帮助。

我猜你的 lastname 字段可能是 analyzed,因此被索引的标记是 ayala 而不是 Ayala。因此,由于未分析 wildcard 查询,您需要以小写形式指定搜索词:

试试这个:

POST /_search
{
    "query":{       
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" :
          {
            "wildcard":{"lastname" : "aya*" }
          }      
        }
      }
    }
    }
}

这对我有用:

GET /_search
{
  "query": {
    "wildcard": {
      "lastname": "aya*"
    }
  }
}

在我在线阅读这篇文章之前,我已经浪费了很多时间:

...if you search a not_analyzed field and use wildcards, you MUST NOT include any capital letters in your query...

一直以来它都不起作用,因为我一直在搜索例如Aya*。当我将它转换为小写 aya* 时,它就起作用了。