Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#setPreference()

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder#setPreference() . 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: Job.java    From zentity with Apache License 2.0 6 votes vote down vote up
/**
 * Submit a search query to Elasticsearch.
 *
 * @param indexName The name of the index to search.
 * @param query     The query to search.
 * @return The search response returned by Elasticsearch.
 * @throws IOException
 */
private SearchResponse search(String indexName, String query) throws IOException {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
    try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new NamedXContentRegistry(searchModule
            .getNamedXContents()), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, query)) {
        searchSourceBuilder.parseXContent(parser);
    }
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
    searchRequestBuilder.setIndices(indexName).setSource(searchSourceBuilder);
    if (this.searchAllowPartialSearchResults != null)
        searchRequestBuilder.setAllowPartialSearchResults(this.searchAllowPartialSearchResults);
    if (this.searchBatchedReduceSize != null)
        searchRequestBuilder.setBatchedReduceSize(this.searchBatchedReduceSize);
    if (this.searchMaxConcurrentShardRequests != null)
        searchRequestBuilder.setMaxConcurrentShardRequests(this.searchMaxConcurrentShardRequests);
    if (this.searchPreFilterShardSize != null)
        searchRequestBuilder.setPreFilterShardSize(this.searchPreFilterShardSize);
    if (this.searchPreference != null)
        searchRequestBuilder.setPreference(this.searchPreference);
    if (this.searchRequestCache != null)
        searchRequestBuilder.setRequestCache(this.searchRequestCache);
    if (this.maxTimePerQuery != null)
        searchRequestBuilder.setTimeout(TimeValue.parseTimeValue(this.maxTimePerQuery, "timeout"));
    return searchRequestBuilder.execute().actionGet();
}
 
Example 2
Source File: QueryAction.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected void updateRequestWithPreference(Select select, SearchRequestBuilder request) {
    for (Hint hint : select.getHints()) {
        if (hint.getType() == HintType.PREFERENCE && hint.getParams() != null && 0 < hint.getParams().length) {
            request.setPreference(hint.getParams()[0].toString());
        }
    }
}
 
Example 3
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
@Override
protected int delegateSelectCountUniquely(final ConditionBean cb) {
    // #pending check response and cast problem
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    return (int) getSearchHits(esCb.build(builder).execute().actionGet(searchTimeout)).getTotalHits().value;
}
 
Example 4
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected void delegateBulkRequest(final ConditionBean cb, Function<SearchHits, Boolean> handler) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForCursor).setSize(sizeForCursor);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            if (!handler.apply(searchHits)) {
                break;
            }

            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
}
 
Example 5
Source File: EsAbstractConditionBean.java    From fess with Apache License 2.0 5 votes vote down vote up
public void build(SearchRequestBuilder builder) {
    if (explain != null) {
        builder.setExplain(explain);
    }
    if (minScore != null) {
        builder.setMinScore(minScore);
    }
    if (preference != null) {
        builder.setPreference(preference);
    }
    if (routing != null) {
        builder.setRouting(routing);
    }
    if (searchType != null) {
        builder.setSearchType(searchType);
    }
    if (timeoutInMillis != -1) {
        builder.setTimeout(new TimeValue(timeoutInMillis));
    }
    if (version != null) {
        builder.setVersion(version);
    }
    if (seqNoAndPrimaryTerm != null) {
        builder.seqNoAndPrimaryTerm(seqNoAndPrimaryTerm);
    }
    if (terminateAfter > 0) {
        builder.setTerminateAfter(terminateAfter);
    }
}
 
Example 6
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
@Override
protected int delegateSelectCountUniquely(final ConditionBean cb) {
    // #pending check response and cast problem
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    return (int) getSearchHits(esCb.build(builder).execute().actionGet(searchTimeout)).getTotalHits().value;
}
 
Example 7
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected void delegateBulkRequest(final ConditionBean cb, Function<SearchHits, Boolean> handler) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForCursor).setSize(sizeForCursor);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            if (!handler.apply(searchHits)) {
                break;
            }

            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
}
 
Example 8
Source File: EsAbstractConditionBean.java    From fess with Apache License 2.0 5 votes vote down vote up
public void build(SearchRequestBuilder builder) {
    if (explain != null) {
        builder.setExplain(explain);
    }
    if (minScore != null) {
        builder.setMinScore(minScore);
    }
    if (preference != null) {
        builder.setPreference(preference);
    }
    if (routing != null) {
        builder.setRouting(routing);
    }
    if (searchType != null) {
        builder.setSearchType(searchType);
    }
    if (timeoutInMillis != -1) {
        builder.setTimeout(new TimeValue(timeoutInMillis));
    }
    if (version != null) {
        builder.setVersion(version);
    }
    if (seqNoAndPrimaryTerm != null) {
        builder.seqNoAndPrimaryTerm(seqNoAndPrimaryTerm);
    }
    if (terminateAfter > 0) {
        builder.setTerminateAfter(terminateAfter);
    }
}
 
Example 9
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
@Override
protected int delegateSelectCountUniquely(final ConditionBean cb) {
    // #pending check response and cast problem
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    return (int) getSearchHits(esCb.build(builder).execute().actionGet(searchTimeout)).getTotalHits().value;
}
 
Example 10
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected void delegateBulkRequest(final ConditionBean cb, Function<SearchHits, Boolean> handler) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForCursor).setSize(sizeForCursor);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            if (!handler.apply(searchHits)) {
                break;
            }

            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
}
 
Example 11
Source File: EsAbstractConditionBean.java    From fess with Apache License 2.0 5 votes vote down vote up
public void build(SearchRequestBuilder builder) {
    if (explain != null) {
        builder.setExplain(explain);
    }
    if (minScore != null) {
        builder.setMinScore(minScore);
    }
    if (preference != null) {
        builder.setPreference(preference);
    }
    if (routing != null) {
        builder.setRouting(routing);
    }
    if (searchType != null) {
        builder.setSearchType(searchType);
    }
    if (timeoutInMillis != -1) {
        builder.setTimeout(new TimeValue(timeoutInMillis));
    }
    if (version != null) {
        builder.setVersion(version);
    }
    if (seqNoAndPrimaryTerm != null) {
        builder.seqNoAndPrimaryTerm(seqNoAndPrimaryTerm);
    }
    if (terminateAfter > 0) {
        builder.setTerminateAfter(terminateAfter);
    }
}
 
Example 12
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) {
    // #pending check response
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final int from;
    final int size;
    if (cb.isFetchScopeEffective()) {
        from = cb.getPageStartIndex();
        size = cb.getFetchSize();
    } else {
        from = 0;
        size = 10;
    }
    builder.setFrom(from);
    builder.setSize(size);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout);

    final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder);
    final SearchHits searchHits = getSearchHits(response);
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSourceAsMap();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        docMeta.seqNo(hit.getSeqNo());
        docMeta.primaryTerm(hit.getPrimaryTerm());
        list.add(entity);
    });

    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits().value);
    list.setCurrentPageNumber(cb.getFetchPageNumber());

    list.setTook(response.getTook().getMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setTotalHits(searchHits.getTotalHits());

    list.setAggregation(response.getAggregations());

    // #pending others

    return list;
}
 
Example 13
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) {
    // #pending check response
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final int from;
    final int size;
    if (cb.isFetchScopeEffective()) {
        from = cb.getPageStartIndex();
        size = cb.getFetchSize();
    } else {
        from = 0;
        size = 10;
    }
    builder.setFrom(from);
    builder.setSize(size);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout);

    final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder);
    final SearchHits searchHits = getSearchHits(response);
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSourceAsMap();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        docMeta.seqNo(hit.getSeqNo());
        docMeta.primaryTerm(hit.getPrimaryTerm());
        list.add(entity);
    });

    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits().value);
    list.setCurrentPageNumber(cb.getFetchPageNumber());

    list.setTook(response.getTook().getMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setTotalHits(searchHits.getTotalHits());

    list.setAggregation(response.getAggregations());

    // #pending others

    return list;
}
 
Example 14
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) {
    // #pending check response
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final int from;
    final int size;
    if (cb.isFetchScopeEffective()) {
        from = cb.getPageStartIndex();
        size = cb.getFetchSize();
    } else {
        from = 0;
        size = 10;
    }
    builder.setFrom(from);
    builder.setSize(size);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout);

    final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder);
    final SearchHits searchHits = getSearchHits(response);
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSourceAsMap();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        docMeta.seqNo(hit.getSeqNo());
        docMeta.primaryTerm(hit.getPrimaryTerm());
        list.add(entity);
    });

    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits().value);
    list.setCurrentPageNumber(cb.getFetchPageNumber());

    list.setTook(response.getTook().getMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setTotalHits(searchHits.getTotalHits());

    list.setAggregation(response.getAggregations());

    // #pending others

    return list;
}