Java Code Examples for org.elasticsearch.search.internal.SearchContext#size()

The following examples show how to use org.elasticsearch.search.internal.SearchContext#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SizeParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        int size = parser.intValue();
        if (size < 0) {
            throw new SearchParseException(context, "size is set to [" + size + "] and is expected to be higher or equal to 0",
                    parser.getTokenLocation());
        }
        context.size(size);
    }
}
 
Example 2
Source File: IndicesRequestCache.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Can the shard request be cached at all?
 */
public boolean canCache(ShardSearchRequest request, SearchContext context) {
    // TODO: for now, template is not supported, though we could use the generated bytes as the key
    if (hasLength(request.templateSource())) {
        return false;
    }

    // for now, only enable it for requests with no hits
    if (context.size() != 0) {
        return false;
    }

    // We cannot cache with DFS because results depend not only on the content of the index but also
    // on the overridden statistics. So if you ran two queries on the same index with different stats
    // (because an other shard was updated) you would get wrong results because of the scores
    // (think about top_hits aggs or scripts using the score)
    if (!CACHEABLE_SEARCH_TYPES.contains(context.searchType())) {
        return false;
    }

    IndexMetaData index = clusterService.state().getMetaData().index(request.index());
    if (index == null) { // in case we didn't yet have the cluster state, or it just got deleted
        return false;
    }
    // if not explicitly set in the request, use the index setting, if not, use the request
    if (request.requestCache() == null) {
        if (!isCacheEnabled(index.getSettings(), Boolean.FALSE)) {
            return false;
        }
    } else if (!request.requestCache()) {
        return false;
    }
    // if the reader is not a directory reader, we can't get the version from it
    if (!(context.searcher().getIndexReader() instanceof DirectoryReader)) {
        return false;
    }
    // if now in millis is used (or in the future, a more generic "isDeterministic" flag
    // then we can't cache based on "now" key within the search request, as it is not deterministic
    if (context.nowInMillisUsed()) {
        return false;
    }
    return true;
}