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);
}
}
}
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;
}
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());
}
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;
}
}
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;
}
Aggregations