Search in sources :

Example 56 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project fess by codelibs.

the class FessProp method processSearchPreference.

public default default void processSearchPreference(final SearchRequestBuilder searchRequestBuilder, final OptionalThing<FessUserBean> userBean) {
    userBean.map(user -> {
        if (user.hasRoles(getAuthenticationAdminRolesAsArray())) {
            return Constants.SEARCH_PREFERENCE_PRIMARY;
        }
        return user.getUserId();
    }).ifPresent(p -> searchRequestBuilder.setPreference(p)).orElse(() -> LaRequestUtil.getOptionalRequest().map(r -> {
        final HttpSession session = r.getSession(false);
        if (session != null) {
            return session.getId();
        }
        final String preference = r.getParameter("preference");
        if (preference != null) {
            return Integer.toString(preference.hashCode());
        }
        return null;
    }).ifPresent(p -> searchRequestBuilder.setPreference(p)));
}
Also used : ClassNotFoundRuntimeException(org.codelibs.core.exception.ClassNotFoundRuntimeException) Constants(org.codelibs.fess.Constants) SortBuilders(org.elasticsearch.search.sort.SortBuilders) Enumeration(java.util.Enumeration) Date(java.util.Date) JobConcurrentExec(org.lastaflute.job.subsidiary.JobConcurrentExec) OptionalThing(org.dbflute.optional.OptionalThing) Pair(org.codelibs.core.misc.Pair) PermissionHelper(org.codelibs.fess.helper.PermissionHelper) IntegerTypeValidator(org.lastaflute.web.validation.theme.typed.IntegerTypeValidator) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) LongTypeValidator(org.lastaflute.web.validation.theme.typed.LongTypeValidator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BasicAttribute(javax.naming.directory.BasicAttribute) FessUserBean(org.codelibs.fess.mylasta.action.FessUserBean) Attribute(javax.naming.directory.Attribute) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) Locale(java.util.Locale) Map(java.util.Map) PrunedTag(org.codelibs.fess.util.PrunedTag) HtmlNext(org.lastaflute.web.response.next.HtmlNext) SortBuilder(org.elasticsearch.search.sort.SortBuilder) HttpSession(javax.servlet.http.HttpSession) LaJob(org.lastaflute.job.LaJob) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) FloatTypeValidator(org.lastaflute.web.validation.theme.typed.FloatTypeValidator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StringUtil(org.codelibs.core.lang.StringUtil) Set(java.util.Set) RequiredValidator(org.lastaflute.web.validation.RequiredValidator) Tuple3(org.codelibs.core.misc.Tuple3) Collectors(java.util.stream.Collectors) List(java.util.List) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SortOrder(org.elasticsearch.search.sort.SortOrder) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) DoubleTypeValidator(org.lastaflute.web.validation.theme.typed.DoubleTypeValidator) FessFunctions(org.codelibs.fess.taglib.FessFunctions) HttpSession(javax.servlet.http.HttpSession)

Example 57 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project zeppelin by apache.

the class TransportBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    final SearchRequestBuilder reqBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
    reqBuilder.setIndices();
    if (indices != null) {
        reqBuilder.setIndices(indices);
    }
    if (types != null) {
        reqBuilder.setTypes(types);
    }
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            @SuppressWarnings("rawtypes") final Map source = gson.fromJson(query, Map.class);
            reqBuilder.setExtraSource(source);
        } catch (final JsonSyntaxException e) {
            // This is not a JSON (or maybe not well formatted...)
            reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
        }
    }
    reqBuilder.setSize(size);
    final SearchResponse searchResp = reqBuilder.get();
    final ActionResponse actionResp = new ActionResponse().succeeded(true).totalHits(searchResp.getHits().getTotalHits());
    if (searchResp.getAggregations() != null) {
        setAggregations(searchResp.getAggregations(), actionResp);
    } else {
        for (final SearchHit hit : searchResp.getHits()) {
            // Fields can be found either in _source, or in fields (it depends on the query)
            // => specific for elasticsearch's version < 5
            //
            String src = hit.getSourceAsString();
            if (src == null) {
                final Map<String, Object> hitFields = new HashMap<>();
                for (final SearchHitField hitField : hit.getFields().values()) {
                    hitFields.put(hitField.getName(), hitField.getValues());
                }
                src = gson.toJson(hitFields);
            }
            actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src));
        }
    }
    return actionResp;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) SearchHitField(org.elasticsearch.search.SearchHitField) HashMap(java.util.HashMap) Map(java.util.Map)

Example 58 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class SearchSliceIT method testNumericSort.

public void testNumericSort() throws Exception {
    int numShards = setupIndex(true);
    SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setSize(0).get();
    int numDocs = (int) sr.getHits().getTotalHits();
    assertThat(numDocs, equalTo(NUM_DOCS));
    int max = randomIntBetween(2, numShards * 3);
    for (String field : new String[] { "_uid", "random_int", "static_int" }) {
        int fetchSize = randomIntBetween(10, 100);
        SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).addSort(SortBuilders.fieldSort("random_int")).setSize(fetchSize);
        assertSearchSlicesWithScroll(request, field, max);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Scroll(org.elasticsearch.search.Scroll) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 59 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class SearchSliceIT method testDocIdSort.

public void testDocIdSort() throws Exception {
    int numShards = setupIndex(true);
    SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setSize(0).get();
    int numDocs = (int) sr.getHits().getTotalHits();
    assertThat(numDocs, equalTo(NUM_DOCS));
    int max = randomIntBetween(2, numShards * 3);
    for (String field : new String[] { "_uid", "random_int", "static_int" }) {
        int fetchSize = randomIntBetween(10, 100);
        SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).addSort(SortBuilders.fieldSort("_doc"));
        assertSearchSlicesWithScroll(request, field, max);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Scroll(org.elasticsearch.search.Scroll) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 60 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class SearchScrollIT method testDeepScrollingDoesNotBlowUp.

/**
     * Tests that we use an optimization shrinking the batch to the size of the shard. Thus the Integer.MAX_VALUE window doesn't OOM us.
     */
public void testDeepScrollingDoesNotBlowUp() throws Exception {
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).execute().get();
    /*
         * Disable the max result window setting for this test because it'll reject the search's unreasonable batch size. We want
         * unreasonable batch sizes to just OOM.
         */
    client().admin().indices().prepareUpdateSettings("index").setSettings(Settings.builder().put(IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey(), Integer.MAX_VALUE)).get();
    for (SearchType searchType : SearchType.values()) {
        SearchRequestBuilder builder = client().prepareSearch("index").setSearchType(searchType).setQuery(QueryBuilders.matchAllQuery()).setSize(Integer.MAX_VALUE).setScroll("1m");
        SearchResponse response = builder.execute().actionGet();
        try {
            ElasticsearchAssertions.assertHitCount(response, 1L);
        } finally {
            String scrollId = response.getScrollId();
            if (scrollId != null) {
                clearScroll(scrollId);
            }
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchType(org.elasticsearch.action.search.SearchType) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7