2016-10-21 36 views
0

不同的映射具有指數tester以下映射兩種類型itemsitems_two搜索在兩種不同類型與Elasticsearch

curl -XPUT 'localhost:9200/tester?pretty=true' -d '{ 
    "mappings": { 
     "items": { 
     "properties" : { 
      "body" : { "type": "string" } 
}}, 
     "items_two": { 
     "properties" : { 
      "body" : { "type": "string" }, 
      "publised" : { "type": "integer"} 
}}}}' 

我把三個要素就可以了。

curl -XPUT 'localhost:9200/tester/items/1?pretty=true' -d '{ 
    "body" : "Hey there im reading a book" 
}' 

curl -XPUT 'localhost:9200/tester/items_two/1?pretty=true' -d '{ 
    "body" : "I love the new book of my brother", 
    "publised" : 0 
}' 

curl -XPUT 'localhost:9200/tester/items_two/2?pretty=true' -d '{ 
    "body" : "Stephen kings book is very nice", 
    "publised" : 1 
}' 

我需要相匹配的詞book並具有published = 1查詢和未published對映射的,但有它book(截至items的唯一項目)。

以下查詢我只與"Stephen kings book is very nice"項目(顯然)匹配。

curl -XGET 'localhost:9200/tester/_search?pretty=true' -d '{ 
"query": { 
"bool": { 
     "must": [ 
     { 
      "match": { "body": "book" } 
     }, 
     { 
      "match": { "publised": "1" } 
     }] 
}}}' 

我的期望的輸出,如果我搜索字符串book應該從類型items"Hey there im reading a book")和item#2從類型items_two"Stephen kings book is very nice")匹配項目#1。

我不想更改映射或其他任何內容,我需要通過一個查詢來完成此操作,那麼如何構建我的查詢?

在此先感謝。

回答

2

您可以使用_type字段進行這些類型的搜索。請嘗試以下查詢

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "bool": { 
      "must": [ 
       { 
       "match": { 
        "body": "text" 
       } 
       }, 
       { 
       "match": { 
        "publised": "1" 
       } 
       } 
      ], 
      "filter": { 
       "term": { 
       "_type": "items_two" 
       } 
      } 
      } 
     }, 
     { 
      "bool": { 
      "must": [ 
       { 
       "match": { 
        "body": "text" 
       } 
       } 
      ], 
      "filter": { 
       "term": { 
       "_type": "items" 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
相關問題