ElasticSearch中嵌套对象的通配符搜索

ghhaqwfi  于 2023-03-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(211)

假设我们正在存储vehicle类型的对象,这些对象在下面的结构中引用了一个类型所有者。然后运行以下请求:
`POST:localhost:9200/15/vehicles/_search'
以下机构:

{ "query": { "wildcard": {"make":"*toy*"} }}

返回相关对象:

"took": 5,
     "timed_out": false,
     "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
     },
    "hits": {
        "total": 1,
    "max_score": 1,
    "hits": [
        {
            "_index": "15.index",
            "_type": "vehicle",
            "_id": "352",
            "_score": 1,
            "_source": {
                "id": "352",
                "name": "toyota",
                "owner_id": "12",
                "owner": {
                    "id": "12",
                    "name": "John Smith",
                    "login_id": 1,
                    "active": true,
                }
            }
        }
    ]
}

我现在尝试按嵌套对象进行查询(例如,属于用户John Smith的所有车辆)

{"query": {"wildcard": {"owner.name": "*John*"}}}

不返回任何结果,而:

{"query": {"wildcard": {"owner": {"name": "*John*"}}}}

错误排除:

"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {....
  [4]: SearchParseException[....
  Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
 [wildcard] query does not support [name]]; }{....
 [3]: SearchParseException[....
: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
 [wildcard] query does not support [name]]; }{....
 [2]: SearchParseException[....
 ]: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
 [wildcard] query does not support [name]]; }{....
 : SearchParseException[[....
: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[....
 [wildcard] query does not support [name]]; }{....
 [0]: SearchParseException[....: Parse Failure [Failed to parse source [{ \"query\": { \"wildcard\": {\"owner\": {\"name\":\"*ab*\"} }}}]]]; nested: QueryParsingException[.... [wildcard] query does not support [name]]; }]",
"status": 400

针对嵌套对象的查询(通配符或其他)的正确格式是什么?通配符查询与该模板有何不同(如果有的话)?

whlutmcx

whlutmcx1#

您需要使用Nested Query来查询nested类型中的字段。

{
  "query":{
    "nested":{
      "path":"owner",
      "query":{
        "wildcard":{
           "owner.name":"John*"
        }
      }
    }
  }
}

此外,您不应该以通配符开始您的术语,因为它可能会导致非常缓慢的查询。
如果有多个级别的nested对象,则path值应该是nested对象的最深级别,并且查询中的属性应该是完整路径。

{
  "query":{
    "nested":{
      "path":"owner.pets",
      "query":{
        "wildcard":{
           "owner.pets.name":"{someValue}"
        }
      }
    }
  }
}
o2gm4chl

o2gm4chl2#

要将其他条件与nested对象组合,请帮助此帮助某个人。

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "*",
            "fields": [
              "field_a",
              "field_b",
              "field_c"
            ]
          }
        },
        {
          "nested": {
            "path": "students",
            "query": {
              "bool": {
                "must": [
                  {
                    "wildcard": {
                      "students.name": {
                        "value": "*keyword*"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

相关问题