2016-05-28 67 views
1

因此,在ES中構建了(右側)查詢並使用sense插件對本地ES安裝進行了測試後,我現在面臨的問題是:如何使用ES JAVA API從我的代碼執行相同的操作。下面是該查詢我想翻譯:如何將ElasticSearch多匹配搜索查詢從cURL轉換爲JAVA?

{ 
"size": 5, 
"query": { 
    "multi_match": { 
    "query": "physics", 
    "type": "most_fields", 
    "fields": [ 
     "document.title^10", 
     "document.title.shingles^2", 
     "document.title.ngrams", 
     "person.name^10", 
     "person.name.shingles^2", 
     "person.name.ngrams", 
     "document.topics.name^10", 
     "document.topics.name.shingles^2", 
     "document.topics.name.ngrams" 
     ], 
     "operator": "and" 
    } 
    } 
}' 

我知道它應該是這樣的,但我不是很確定:

Node node = nodeBuilder().client(true).node(); 
    Client client = node.client(); 

    SearchResponse response = client.prepareSearch("dlsnew") 
      .setTypes("person", "document") 
      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 
      .setQuery(QueryBuilders.multiMatchQuery("physics", 
        "document.title^10", 
        "document.title.shingles^2", 
        "document.title.ngrams", 
        "person.name^10", 
        "person.name.shingles^2", 
        "person.name.ngrams", 
        "document.topics.name^10", 
        "document.topics.name.shingles^2", 
        "document.topics.name.ngrams")) 
      .setFrom(0).setSize(5).setExplain(true) 
      .execute() 
      .actionGet(); 

    SearchHit[] results = response.getHits().getHits(); 

此外,如何處理「經營者」和「查詢」中的「type」:「most_fields」部分?

回答

1

你幾乎做到了

QueryBuilders.multiMatchQuery("physics", 
       "document.title^10", 
       "document.title.shingles^2", 
       "document.title.ngrams", 
       "person.name^10", 
       "person.name.shingles^2", 
       "person.name.ngrams", 
       "document.topics.name^10", 
       "document.topics.name.shingles^2", 
       "document.topics.name.ngrams") 
       .operator(MatchQueryBuilder.Operator.AND) 
       .type(MultiMatchQueryBuilder.Type.MOST_FIELDS); 
+0

謝謝,這作品! –

相關問題