在ElasticSearch中对嵌套对象进行通配符搜索

时间:2014-09-21 06:13:53

标签: elasticsearch

假设我们在以下结构中存储类型车辆的对象,该对象具有对类型所有者的引用。然后运行以下请求:

`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

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

2 个答案:

答案 0 :(得分:3)

您需要使用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}"
        }
      }
    }
  }
}

答案 1 :(得分:0)

要将其他条件与requestAnimationFrame对象结合使用,请帮助此对象。

nested