2016-05-31 158 views
0

考慮以下指標結果:Elasticsearch嵌套數組或什麼是嵌套數組的路徑?

curl 'localhost:9200/index123/type123/_search?q=*&pretty' 
... 
      "_source": { 
       "objectId": "objectId123", 
       "list": [{ 
        "name": "SomeName123", 
        "value": "SomeValue123", 
        "type": "SomeType123"}, 
        {"name": "SomeOtherName123", ...}], 
       "someOther": { 
        "i": "i", 
        "value": 3 
       } 
      } 
... 

現在我想做一個搜索,並得到所有條目匹配兩個字段,例如list.value = SomeValue123 & & list.type = SomeType123。問題是,結果可能很大,所以應該可以滾動。

我到目前爲止有:

SearchResponse scrollResp = elasticSearchClient 
         .prepareSearch("index123") 
         .setTypes("type123") 
         .setScroll(new TimeValue(60000)) 
        .setQuery(
          QueryBuilders.nestedQuery(
            "list", 
            QueryBuilders 
              .boolQuery() 
              .must(QueryBuilders.matchQuery("value", "SomeValue123")) 
              .must(QueryBuilders.matchQuery("type", "SomeType123"))) 

        ) 
        .setSize(100) 
        .execute() 
        .actionGet(); 

      SomeQueue<SomeBean> resultQ= new SomeQueue<SomeBean>(); 
      // Scroll until no hits are returned 
      while (true) { 

       resultQ.offer(getObjectOutOfHits(scrollResp.getHits().getHits())); 

       scrollResp = elasticSearchClient 
         .prepareSearchScroll(scrollResp.getScrollId()) 
         .setScroll(new TimeValue(60000)) 
         .execute() 
         .actionGet(); 
       // Break condition: No hits are returned 
       if (scrollResp.getHits().getHits().length == 0) { 
        break; 
       } 
      } 

但我得到的是:

Caused by: org.elasticsearch.index.query.QueryParsingException: [nested] failed to find nested object under path [list] 

如何,我可以得到的所有項目,那場比賽這一領域用的彈性搜索Java客戶端結合?

如果有人知道只是curl命令,那很好,所以我可以使用模板!

+0

點點offtopic,但你可以使用'做......而()爆發的'循環insteed;) – Antoniossss

+0

你是對的,但我只是複製從滾動例如該代碼HTTPS ://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html – hiaclibe

+0

要使嵌套查詢正常工作,您需要嵌套映射,但您沒有大概。可以添加映射嗎?如果沒有,上述查詢無法正常工作。 –

回答

1

爲什麼你將查詢嵌套到空查詢中? 試試這個:

.setQuery(
     QueryBuilders.boolQuery() 
      .must(QueryBuilders.matchQuery("value", "SomeValue123")) 
      .must(QueryBuilders.matchQuery("type", "SomeType123")) 
     ) 
+0

我試過了,沒有找到匹配,但在彈性搜索中有一些匹配條目。 – hiaclibe

+1

我現在正在與elasticsearch合作,我跑跑車進入類似的效果 - 沒有命中,而應該有一些。這通常是由錯誤的字段名稱引起的。例如,如果您嘗試查詢不存在的字段,ES不會拋出錯誤。同樣'匹配'查詢可能不會返回匹配,因爲它的性質(它不等於==,'term'查詢是)。請使用JSON查詢,您認爲應該返回正確的答案並將其添加到您的問題。 – Antoniossss