2015-03-03 41 views
1

我在elasticsearch中搜索了索引數據。索引名稱是「演示」。 我有兩種類型(映射)「演示」,一個是「用戶」,另一個是「博客」。 「用戶」類型有字段 - 名稱,城市,國家其他字段和博客有 - 「標題」,描述「,」author_name「等 現在我想搜索」演示「如果我想搜索」java「那麼它將帶有任何類型的任何字段(「用戶」或「博客」)中的所有具有「java」的文檔。Elasticsearch - 在多種類型的索引及其不同類型之間進行搜索

+0

您試過了什麼解決方案? – bittusarkar 2015-03-03 14:11:41

回答

6

您可以使用該索引的"_all" field。默認情況下,每個字段都將包含在"_all"字段中,然後您可以對"_all"字段運行match查詢,並且在搜索索引時,只是不指定類型並且將搜索所有類型。 :

DELETE /test_index 

PUT /test_index 
{ 
    "settings": { 
     "number_of_shards": 1 
    }, 
    "mappings": { 
     "user": { 
     "properties": { 
      "name" : { "type": "string" }, 
      "city" : { "type": "string" }, 
      "country" : { "type": "string" } 
     } 
     }, 
     "blog": { 
     "properties": { 
      "title" : { "type": "string" }, 
      "description" : { "type": "string" }, 
      "author_name" : { "type": "string" } 
     } 
     } 
    } 
} 

POST /test_index/_bulk 
{"index":{"_index":"test_index","_type":"user"}} 
{"name":"Bob","city":"New York","country":"USA"} 
{"index":{"_index":"test_index","_type":"user"}} 
{"name":"John","city":"Jakarta","country":"Java/Indonesia"} 
{"index":{"_index":"test_index","_type":"blog"}} 
{"title":"Python/ES","description":"using Python with Elasticsearch","author_name":"John"} 
{"index":{"_index":"test_index","_type":"blog"}} 
{"title":"Java/ES","description":"using Java with Elasticsearch","author_name":"Bob"} 

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "_all": "Java" 
     } 
    } 
} 
... 
{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 0.68289655, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "blog", 
      "_id": "hNJ-AOG2SbS0nw4IPBuXGQ", 
      "_score": 0.68289655, 
      "_source": { 
       "title": "Java/ES", 
       "description": "using Java with Elasticsearch", 
       "author_name": "Bob" 
      } 
     }, 
     { 
      "_index": "test_index", 
      "_type": "user", 
      "_id": "VqfowNx8TTG69buY9Vd_MQ", 
      "_score": 0.643841, 
      "_source": { 
       "name": "John", 
       "city": "Jakarta", 
       "country": "Java/Indonesia" 
      } 
     } 
     ] 
    } 
} 
相關問題