2016-01-28 240 views
0

我有多個嵌套文檔的doc。嵌套查詢可以正常工作,但即使搜索查詢只匹配少數嵌套對象,它們仍會返回所有嵌套對象(即整個文檔)。但是,它確實將文檔過濾爲整體。在基於查詢的elasticsearch查詢中篩選嵌套對象

下面是一個例子:

PUT /demo 
{ 
    "mappings": { 
    "company": { 
     "properties": { 
     "employees": { 
      "type": "nested" 
     } 
     } 
    } 
    } 
} 

PUT 
/demo/company/1 
{ 
    "id": 1, 
    "name": "Google", 
    "emp_count": 3, 
    "employees": [{ 
    "id": 1, 
    "name": "John", 
    "address": { 
     "city": "Mountain View", 
     "state": "California", 
     "country": "United States" 
    } 
    }] 
} 

PUT 
/demo/company/2 
{ 
    "id": 1, 
    "name": "Facebook", 
    "emp_count": 3, 
    "employees": [{ 
    "id": 1, 
    "name": "Amber", 
    "address": { 
     "city": "Bangalore", 
     "state": "Karnataka", 
     "country": "India" 
    } 
    }, { 
    "id": 1, 
    "name": "Adrian", 
    "address": { 
     "city": "Palo Alto", 
     "state": "California", 
     "country": "United States" 
    } 
    }] 
} 

PUT 
/demo/company/3 
{ 
    "id": 1, 
    "name": "Microsoft", 
    "emp_count": 3, 
    "employees": [{ 
    "id": 1, 
    "name": "Aman", 
    "address": { 
     "city": "New York", 
     "state": "New York", 
     "country": "United States" 
    } 
    }] 
} 

當在地址搜索India,我應該理想地只得到Facebook一個嵌套的對象,但我得到的所有嵌套對象。我怎樣才能過濾返回的嵌套對象?

例子查詢:此查詢的

{ 
    "query": { 
    "function_score":{ 
     "query":{ 
     "nested":{ 
     "path":"employees", 
     "score_mode":"max", 
     "query": { 
      "multi_match":{ 
       "query":"India", 
       "type":"cross_fields", 
       "fields":[ 
       "employees.address.city", 
       "employees.address.country", 
       "employees.address.state" 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 

輸出爲Facebook與所有員工,而我只想要Amber

回答

0

您可以使用inner_hits以獲得所需的result.Use以下查詢:

GET /demo/company/_search 
{ 
"query" : { 
    "nested" : { 
     "path" : "employees", 
     "query" : { 
      "match" : {"employees.address.country" : "India"} 
     }, 
     "inner_hits" : {} 
    } 
    } 
} 

輸出將是:

"hits": { 
    "total": 1, 
    "max_score": 1.4054651, 
    "hits": [ 
    { 
     "_index": "demo", 
     "_type": "company", 
     "_id": "2", 
     "_score": 1.4054651, 
     "_source": { 
      "id": 1, 
      "name": "Facebook", 
      "emp_count": 3, 
      "employees": [ 
       { 
       "id": 1, 
       "name": "Amber", 
       "address": { 
        "city": "Bangalore", 
        "state": "Karnataka", 
        "country": "India" 
       } 
       }, 
       { 
       "id": 1, 
       "name": "Adrian", 
       "address": { 
        "city": "Palo Alto", 
        "state": "California", 
        "country": "United States" 
       } 
       } 
      ] 
     }, 
     "inner_hits": { 
      "employees": { 
       "hits": { 
       "total": 1, 
       "max_score": 1.4054651, 
       "hits": [ 
        { 
         "_index": "demo", 
         "_type": "company", 
         "_id": "2", 
         "_nested": { 
          "field": "employees", 
          "offset": 0 
         }, 
         "_score": 1.4054651, 
         "_source": { 
          "id": 1, 
          "name": "Amber", 
          "address": { 
          "city": "Bangalore", 
          "state": "Karnataka", 
          "country": "India" 
          } 
         } 
        } 
       ] 
       } 
      } 
     } 
    } 
    ] 
    } 

可以看到,inner_hits節只有那些符合其員工標準。 但是inner_hits是在elasticsearch 1.5.0中引入的。所以版本應該比elasticsearch 1.5.0更高。你可以參考here瞭解更多信息。

+0

我可以但'inner_hits'的問題是,我不能(或至少還沒有能夠弄清楚如何)讓排序只拾取匹配nested_objects。如果我將'emloyees.address.country'更改爲'United States'並在'employees.address.state'上應用排序,'Facebook'的'state'將顯示爲「Karnataka」,儘管結果不存在於inner_hits。 –