use of org.opensearch.search.SearchHit in project fess by codelibs.
the class EsAbstractBehavior method delegateQueryDelete.
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
if (esCb.getPreference() != null) {
esCb.setPreference(esCb.getPreference());
}
esCb.request().build(builder);
SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
String scrollId = response.getScrollId();
int count = 0;
try {
while (scrollId != null) {
final SearchHits searchHits = getSearchHits(response);
final SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
break;
}
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
}
count += hits.length;
final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
if (bulkResponse.hasFailures()) {
throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
}
response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
if (!scrollId.equals(response.getScrollId())) {
deleteScrollContext(scrollId);
}
}
} finally {
deleteScrollContext(scrollId);
}
return count;
}
use of org.opensearch.search.SearchHit in project fess by codelibs.
the class EsAbstractBehavior method delegateBulkRequest.
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);
}
}
use of org.opensearch.search.SearchHit in project fess by codelibs.
the class EsAbstractBehavior method delegateQueryDelete.
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
if (esCb.getPreference() != null) {
esCb.setPreference(esCb.getPreference());
}
esCb.request().build(builder);
SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
String scrollId = response.getScrollId();
int count = 0;
try {
while (scrollId != null) {
final SearchHits searchHits = getSearchHits(response);
final SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
break;
}
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
}
count += hits.length;
final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
if (bulkResponse.hasFailures()) {
throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
}
response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
if (!scrollId.equals(response.getScrollId())) {
deleteScrollContext(scrollId);
}
}
} finally {
deleteScrollContext(scrollId);
}
return count;
}
use of org.opensearch.search.SearchHit in project fess by codelibs.
the class EsAbstractBehavior method delegateQueryDelete.
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
if (esCb.getPreference() != null) {
esCb.setPreference(esCb.getPreference());
}
esCb.request().build(builder);
SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
String scrollId = response.getScrollId();
int count = 0;
try {
while (scrollId != null) {
final SearchHits searchHits = getSearchHits(response);
final SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
break;
}
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
}
count += hits.length;
final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
if (bulkResponse.hasFailures()) {
throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
}
response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
if (!scrollId.equals(response.getScrollId())) {
deleteScrollContext(scrollId);
}
}
} finally {
deleteScrollContext(scrollId);
}
return count;
}
use of org.opensearch.search.SearchHit in project fess by codelibs.
the class QueryResponseList method init.
public void init(final OptionalEntity<SearchResponse> searchResponseOpt, final int start, final int pageSize) {
searchResponseOpt.ifPresent(searchResponse -> {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final SearchHits searchHits = searchResponse.getHits();
allRecordCount = searchHits.getTotalHits().value;
allRecordCountRelation = searchHits.getTotalHits().relation.toString();
queryTime = searchResponse.getTook().millis();
if (searchResponse.getTotalShards() != searchResponse.getSuccessfulShards()) {
partialResults = true;
}
// build highlighting fields
final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
final String hlPrefix = queryHelper.getHighlightPrefix();
for (final SearchHit searchHit : searchHits.getHits()) {
final Map<String, Object> docMap = parseSearchHit(fessConfig, hlPrefix, searchHit);
if (fessConfig.isResultCollapsed()) {
final Map<String, SearchHits> innerHits = searchHit.getInnerHits();
if (innerHits != null) {
final SearchHits innerSearchHits = innerHits.get(fessConfig.getQueryCollapseInnerHitsName());
if (innerSearchHits != null) {
final long totalHits = innerSearchHits.getTotalHits().value;
if (totalHits > 1) {
docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_count", totalHits);
final DocumentField bitsField = searchHit.getFields().get(fessConfig.getIndexFieldContentMinhashBits());
if (bitsField != null && !bitsField.getValues().isEmpty()) {
docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_hash", bitsField.getValues().get(0));
}
docMap.put(fessConfig.getQueryCollapseInnerHitsName(), StreamUtil.stream(innerSearchHits.getHits()).get(stream -> stream.map(v -> parseSearchHit(fessConfig, hlPrefix, v)).toArray(n -> new Map[n])));
}
}
}
}
parent.add(docMap);
}
// facet
final Aggregations aggregations = searchResponse.getAggregations();
if (aggregations != null) {
facetResponse = new FacetResponse(aggregations);
}
});
if (pageSize > 0) {
calculatePageInfo(start, pageSize);
}
}
Aggregations