Search in sources :

Example 91 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project incubator-sdap-mudrod by apache.

the class LinkageIntegration method aggregateRelatedTerms.

public void aggregateRelatedTerms(String input, String model) {
    // get the first 10 related terms
    SearchResponse usrhis = es.getClient().prepareSearch(props.getProperty(INDEX_NAME)).setTypes(model).setQuery(QueryBuilders.termQuery("keywords", input)).addSort(WEIGHT, SortOrder.DESC).setSize(11).execute().actionGet();
    LOG.info("{} results", model);
    for (SearchHit hit : usrhis.getHits().getHits()) {
        Map<String, Object> result = hit.getSource();
        String keywords = (String) result.get("keywords");
        String relatedKey = extractRelated(keywords, input);
        if (!relatedKey.equals(input)) {
            LinkedTerm lTerm = new LinkedTerm(relatedKey, (double) result.get(WEIGHT), model);
            LOG.info("( {} {} )", relatedKey, (double) result.get(WEIGHT));
            termList.add(lTerm);
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) JsonObject(com.google.gson.JsonObject) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 92 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method fetchAllEntities.

private <T> List<T> fetchAllEntities(final String docType, final BuildEntity<T> be, final FilterBuilder filter) throws CalFacadeException {
    final SearchRequestBuilder srb = getClient().prepareSearch(targetIndex);
    srb.setTypes(docType);
    if (debug) {
        debug("fetchAllEntities: srb=" + srb);
    }
    int tries = 0;
    // int ourPos = 0;
    final int ourCount = maxFetchCount;
    final List<T> res = new ArrayList<>();
    SearchResponse scrollResp = srb.setSearchType(SearchType.SCAN).setScroll(new TimeValue(60000)).setPostFilter(filter).setSize(ourCount).execute().actionGet();
    if (scrollResp.status() != RestStatus.OK) {
        if (debug) {
            debug("Search returned status " + scrollResp.status());
        }
    }
    for (; ; ) {
        if (tries > absoluteMaxTries) {
            // huge count or we screwed up
            warn("Indexer: too many tries");
            break;
        }
        scrollResp = getClient().prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
        if (scrollResp.status() != RestStatus.OK) {
            if (debug) {
                debug("Search returned status " + scrollResp.status());
            }
        }
        final SearchHits hits = scrollResp.getHits();
        // Break condition: No hits are returned
        if (hits.hits().length == 0) {
            break;
        }
        for (final SearchHit hit : hits) {
            // Handle the hit...
            final T ent = be.make(getEntityBuilder(hit.sourceAsMap()));
            if (ent == null) {
                // No access
                continue;
            }
            res.add(ent);
        // ourPos++;
        }
        tries++;
    }
    return res;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 93 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method fetchEntity.

private EntityBuilder fetchEntity(final String docType, final String val, final PropertyInfoIndex... index) throws CalFacadeException {
    if ((index.length == 1) && (index[0] == PropertyInfoIndex.HREF)) {
        final GetRequestBuilder grb = getClient().prepareGet(targetIndex, docType, val);
        final GetResponse gr = grb.execute().actionGet();
        if (!gr.isExists()) {
            return null;
        }
        return getEntityBuilder(gr.getSourceAsMap());
    }
    final SearchHit hit = fetchEntity(docType, getFilters(null).singleEntityFilter(docType, val, index));
    if (hit == null) {
        return null;
    }
    return getEntityBuilder(hit.sourceAsMap());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder)

Example 94 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method addOverrides.

private boolean addOverrides(final Response resp, final String indexName, final EventInfo ei) {
    try {
        final BwEvent ev = ei.getEvent();
        if (!ev.testRecurring()) {
            return true;
        }
        /* Fetch any overrides. */
        final ESQueryFilter flts = getFilters(null);
        final int batchSize = 100;
        int start = 0;
        while (true) {
            // Search original for overrides
            final SearchRequestBuilder srb = getClient().prepareSearch(Util.buildPath(false, indexName));
            srb.setSearchType(SearchType.QUERY_THEN_FETCH).setPostFilter(flts.overridesOnly(ev.getUid()));
            srb.setFrom(start);
            srb.setSize(batchSize);
            // if (debug) {
            // debug("Overrides: targetIndex=" + indexName +
            // "; srb=" + srb);
            // }
            final SearchResponse sresp = srb.execute().actionGet();
            if (sresp.status() != RestStatus.OK) {
                errorReturn(resp, "Search returned status " + sresp.status());
                return false;
            }
            final SearchHit[] hits = sresp.getHits().getHits();
            if ((hits == null) || (hits.length == 0)) {
                // No more data - we're done
                break;
            }
            for (final SearchHit hit : hits) {
                final String dtype = hit.getType();
                if (dtype == null) {
                    errorReturn(resp, "org.bedework.index.noitemtype");
                    return false;
                }
                final String kval = hit.getId();
                if (kval == null) {
                    errorReturn(resp, "org.bedework.index.noitemkey");
                    return false;
                }
                final EntityBuilder eb = getEntityBuilder(hit.sourceAsMap());
                final Object entity;
                switch(dtype) {
                    case docTypeEvent:
                    case docTypePoll:
                        entity = eb.makeEvent(kval, false);
                        final EventInfo oei = (EventInfo) entity;
                        final BwEvent oev = oei.getEvent();
                        if ((uidsMap != null) && (oev.getLocationUid() == null)) {
                            String locuid = null;
                            if (uidsOverideMap != null) {
                                locuid = uidsOverideMap.get(oev.getUid() + "|" + oev.getRecurrenceId());
                            }
                            if (locuid == null) {
                                locuid = uidsMap.get(oev.getUid());
                            }
                            if (locuid != null) {
                                uidOverridesSet++;
                                oev.setLocationUid(locuid);
                            }
                            oev.setLocationUid(locuid);
                        }
                        if (!restoreEvProps(resp, oei)) {
                            return false;
                        }
                        if (oev instanceof BwEventAnnotation) {
                            final BwEventAnnotation ann = (BwEventAnnotation) oev;
                            final BwEvent proxy = new BwEventProxy(ann);
                            ann.setTarget(ev);
                            ann.setMaster(ev);
                            ei.addOverride(new EventInfo(proxy));
                            continue;
                        }
                }
                // Unexpected type
                errorReturn(resp, "Expected override only: " + dtype);
                return false;
            }
            if (hits.length < batchSize) {
                // All remaining in this batch - we're done
                break;
            }
            start += batchSize;
        }
        return true;
    } catch (final Throwable t) {
        errorReturn(resp, t);
        return false;
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) EventInfo(org.bedework.calfacade.svc.EventInfo) BwEvent(org.bedework.calfacade.BwEvent) BwEventProxy(org.bedework.calfacade.BwEventProxy) SearchResponse(org.elasticsearch.action.search.SearchResponse) BwEventAnnotation(org.bedework.calfacade.BwEventAnnotation)

Example 95 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project incubator-skywalking by apache.

the class ApplicationEsCacheDAO method getApplicationIdByCode.

@Override
public int getApplicationIdByCode(String applicationCode) {
    ElasticSearchClient client = getClient();
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ApplicationTable.TABLE);
    searchRequestBuilder.setTypes(ApplicationTable.TABLE_TYPE);
    searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must().add(QueryBuilders.termQuery(ApplicationTable.COLUMN_APPLICATION_CODE, applicationCode));
    boolQueryBuilder.must().add(QueryBuilders.termQuery(ApplicationTable.COLUMN_IS_ADDRESS, BooleanUtils.FALSE));
    searchRequestBuilder.setQuery(boolQueryBuilder);
    searchRequestBuilder.setSize(1);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    if (searchResponse.getHits().totalHits > 0) {
        SearchHit searchHit = searchResponse.getHits().iterator().next();
        return (int) searchHit.getSource().get(ApplicationTable.COLUMN_APPLICATION_ID);
    }
    return 0;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ElasticSearchClient(org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)318 SearchResponse (org.elasticsearch.action.search.SearchResponse)225 SearchHits (org.elasticsearch.search.SearchHits)87 ArrayList (java.util.ArrayList)83 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)64 HashMap (java.util.HashMap)37 Map (java.util.Map)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)37 IOException (java.io.IOException)35 Test (org.junit.Test)32 TimeValue (org.elasticsearch.common.unit.TimeValue)29 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)22 List (java.util.List)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)17 SearchHitField (org.elasticsearch.search.SearchHitField)16 HashSet (java.util.HashSet)15 Text (org.elasticsearch.common.text.Text)14