Elasticsearch搜索类型(SearchType )详解

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;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值