es在查詢時,可以指定搜尋類型為QUERY_THEN_FETCH,QUERY_AND_FEATCH,DFS_QUERY_THEN_FEATCH和DFS_QUERY_AND_FEATCH,COUNT,SCAN。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.elasticsearch.action.search;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
public enum SearchType {
/**
* Same as {@link #QUERY_THEN_FETCH}, except for an initial scatter phase which goes and computes the distributed
* term frequencies for more accurate scoring.
*/
DFS_QUERY_THEN_FETCH((byte) 0),
/**
* The query is executed against all shards, but only enough information is returned (not the document content).
* The results are then sorted and ranked, and based on it, only the relevant shards are asked for the actual
* document content. The return number of hits is exactly as specified in size, since they are the only ones that
* are fetched. This is very handy when the index has a lot of shards (not replicas, shard id groups).
*/
QUERY_THEN_FETCH((byte) 1),
/**
* Same as {@link #QUERY_AND_FETCH}, except for an initial scatter phase which goes and computes the distributed
* term frequencies for more accurate scoring.
*/
DFS_QUERY_AND_FETCH((byte) 2),
/**
* The most naive (and possibly fastest) implementation is to simply execute the query on all relevant shards
* and return the results. Each shard returns size results. Since each shard already returns size hits, this
* type actually returns size times number of shards results back to the caller.
*/
QUERY_AND_FETCH((byte) 3),
/**
* Performs scanning of the results which executes the search without any sorting.
* It will automatically start scrolling the result set.
* @deprecated will be removed in 3.0, you should do a regular scroll instead, ordered by `_doc`
*/
@Deprecated
SCAN((byte) 4),
/**
* Only counts the results, will still execute aggregations and the like.
* @deprecated does not any improvements compared to {@link #QUERY_THEN_FETCH} with a `size` of {@code 0}
*/
@Deprecated
COUNT((byte) 5);
public static final SearchType DEFAULT = QUERY_THEN_FETCH;
private static final ParseField COUNT_VALUE = (new ParseField("count", new String[0])).withAllDeprecated("query_then_fetch");
private static final ParseField SCAN_VALUE = (new ParseField("scan", new String[0])).withAllDeprecated("query_then_fetch sorting on `_doc`");
private byte id;
private SearchType(byte id) {
this.id = id;
}
public byte id() {
return this.id;
}
public static SearchType fromId(byte id) {
if(id == 0) {
return DFS_QUERY_THEN_FETCH;
} else if(id == 1) {
return QUERY_THEN_FETCH;
} else if(id == 2) {
return DFS_QUERY_AND_FETCH;
} else if(id == 3) {
return QUERY_AND_FETCH;
} else if(id == 4) {
return SCAN;
} else if(id == 5) {
return COUNT;
} else {
throw new IllegalArgumentException("No search type for [" + id + "]");
}
}
public static SearchType fromString(String searchType, ParseFieldMatcher parseFieldMatcher) {
if(searchType == null) {
return DEFAULT;
} else if("dfs_query_then_fetch".equals(searchType)) {
return DFS_QUERY_THEN_FETCH;
} else if("dfs_query_and_fetch".equals(searchType)) {
return DFS_QUERY_AND_FETCH;
} else if("query_then_fetch".equals(searchType)) {
return QUERY_THEN_FETCH;
} else if("query_and_fetch".equals(searchType)) {
return QUERY_AND_FETCH;
} else if(parseFieldMatcher.match(searchType, SCAN_VALUE)) {
return SCAN;
} else if(parseFieldMatcher.match(searchType, COUNT_VALUE)) {
return COUNT;
} else {
throw new IllegalArgumentException("No search type for [" + searchType + "]");
}
}
}
SearchRequestBuilder有很多很實用的方法:
(1) setIndices(String... indices):上文中描述過,參數可為一個或多個字元串,表示要進行檢索的index;
(2) setTypes(String... types):參數可為一個或多個字元串,表示要進行檢索的type,當參數為0個或者不調用此方法時,表示查詢所有的type;
(3) setSearchType(SearchType searchType):執行檢索的類别,值為org.elasticsearch.action.search.SearchType的元素,SearchType是一個枚舉類型的類,其值如下所示:
元素 含義
QUERY_THEN_FETCH 查詢是針對所有的塊執行的,但傳回的是足夠的資訊,而不是文檔内容(Document)。結果會被排序和分級,基于此,隻有相關的塊的文檔對象會被傳回。由于被取到的僅僅是這些,故而傳回的hit的大小正好等于指定的size。這對于有許多塊的index來說是很便利的(傳回結果不會有重複的,因為塊被分組了)。
QUERY_AND_FETCH 最原始(也可能是最快的)實作就是簡單的在所有相關的shard上執行檢索并傳回結果。每個shard傳回一定尺寸的結果。由于每個shard已經傳回了一定尺寸的hit,這種類型實際上是傳回多個shard的一定尺寸的結果給調用者。
DFS_QUERY_THEN_FETCH 與QUERY_THEN_FETCH相同,預期一個初始的散射相伴用來為更準确的score計算配置設定了的term頻率。
DFS_QUERY_AND_FETCH 與QUERY_AND_FETCH相同,預期一個初始的散射相伴用來為更準确的score計算配置設定了的term頻率。
SCAN 在執行了沒有進行任何排序的檢索時執行浏覽。此時将會自動的開始滾動結果集。
COUNT 隻計算結果的數量,也會執行facet。
(4) setSearchType(String searchType),與setSearchType(SearchType searchType)類似,差別在于其值為字元串型的SearchType,值可為dfs_query_then_fetch、dfsQueryThenFetch、dfs_query_and_fetch、dfsQueryAndFetch、query_then_fetch、queryThenFetch、query_and_fetch或queryAndFetch;