use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.SortOrder in project ranger by apache.
the class ElasticSearchUtil method setSortClause.
public void setSortClause(SearchCriteria searchCriteria, List<SortField> sortFields, SearchSourceBuilder searchSourceBuilder) {
// TODO: We are supporting single sort field only for now
String sortBy = searchCriteria.getSortBy();
String querySortBy = null;
if (!stringUtil.isEmpty(sortBy)) {
sortBy = sortBy.trim();
for (SortField sortField : sortFields) {
if (sortBy.equalsIgnoreCase(sortField.getParamName())) {
querySortBy = sortField.getFieldName();
// Override the sortBy using the normalized value
searchCriteria.setSortBy(sortField.getParamName());
break;
}
}
}
if (querySortBy == null) {
for (SortField sortField : sortFields) {
if (sortField.isDefault()) {
querySortBy = sortField.getFieldName();
// Override the sortBy using the default value
searchCriteria.setSortBy(sortField.getParamName());
searchCriteria.setSortType(sortField.getDefaultOrder().name());
break;
}
}
}
if (querySortBy != null) {
// Add sort type
String sortType = searchCriteria.getSortType();
SortOrder order = SortOrder.ASC;
if (sortType != null && "desc".equalsIgnoreCase(sortType)) {
order = SortOrder.DESC;
}
searchSourceBuilder.sort(querySortBy, order);
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.SortOrder in project pancm_project by xuwujing.
the class IpHandler method query.
/**
* @return boolean
* @Author pancm
* @Description 根据条件查询
* @Date 2019/3/21
* @Param []
*/
public static List<Map<String, Object>> query(String index, String type, EsQueryCondition esQueryCondition, QueryBuilder... queryBuilders) throws IOException {
if (index == null || type == null) {
return null;
}
List<Map<String, Object>> list = new ArrayList<>();
try {
// 查询指定的索引库
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (esQueryCondition != null) {
Integer form = esQueryCondition.getIndex();
Integer pagesize = esQueryCondition.getPagesize();
if (form != null && form > 0 && pagesize != null && pagesize > 0) {
form = (form - 1) * pagesize;
pagesize = form + pagesize;
// 设置起止和结束
sourceBuilder.from(form);
sourceBuilder.size(pagesize);
}
String routing = esQueryCondition.getRouting();
if (routing != null && routing.length() > 0) {
// 设置路由
searchRequest.routing(routing);
}
// 设置索引库表达式
searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
// 设置排序
String order = esQueryCondition.getOrder();
if (order != null) {
String[] orderField = esQueryCondition.getOrderField();
SortOrder order2 = order.equals(SortOrder.DESC) ? SortOrder.DESC : SortOrder.ASC;
// 如果设置了排序字段则用排序的字段进行排序,否则就默认排序
if (orderField != null) {
for (String field : orderField) {
sourceBuilder.sort(new FieldSortBuilder(field).order(order2));
}
} else {
sourceBuilder.sort(new ScoreSortBuilder().order(order2));
}
}
String[] includeFields = esQueryCondition.getIncludeFields();
String[] excludeFields = esQueryCondition.getExcludeFields();
if (includeFields != null && includeFields.length > 0 && excludeFields != null && excludeFields.length > 0) {
sourceBuilder.fetchSource(includeFields, excludeFields);
}
sourceBuilder.fetchSource(esQueryCondition.isCloseSource());
}
// 设置条件
if (queryBuilders != null) {
for (QueryBuilder queryBuilder : queryBuilders) {
sourceBuilder.query(queryBuilder);
}
}
searchRequest.source(sourceBuilder);
// 同步查询
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
if (queryBuilders != null || (esQueryCondition != null && esQueryCondition.isQueryData())) {
// 结果
searchResponse.getHits().forEach(hit -> {
Map<String, Object> map = hit.getSourceAsMap();
list.add(map);
});
}
if (esQueryCondition != null && esQueryCondition.isNeedTotal()) {
Map<String, Object> mapTotal = new HashMap<>();
mapTotal.put("total", searchResponse.getHits().getTotalHits());
list.add(mapTotal);
}
} finally {
if (isAutoClose) {
close();
}
}
return list;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.SortOrder in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method search.
@Override
public SearchResult search(final String query, final boolean relevance, final FilterBase filter, final List<SortTerm> sort, final FilterBase defaultFilterContext, final String start, final String end, final int pageSize, final DeletedState deletedState, final RecurringRetrievalMode recurRetrieval) throws CalFacadeException {
if (basicSysprops.getTestMode()) {
final long timeSinceIndex = System.currentTimeMillis() - lastIndexTime;
final long waitTime = indexerDelay - timeSinceIndex;
if (waitTime > 0) {
try {
Thread.sleep(waitTime);
} catch (final InterruptedException ignored) {
}
}
}
final EsSearchResult res = new EsSearchResult(this);
res.start = start;
res.end = end;
res.pageSize = pageSize;
res.accessCheck = accessCheck;
res.delState = deletedState;
res.recurRetrieval = recurRetrieval;
if (query != null) {
final MatchQueryBuilder mqb = QueryBuilders.matchQuery("_all", query);
if (!relevance) {
mqb.operator(Operator.AND);
} else {
mqb.fuzziness("1");
mqb.prefixLength(2);
}
res.curQuery = mqb;
// res.curQuery = QueryBuilders.queryString(query);
}
final ESQueryFilter ef = getFilters(recurRetrieval);
res.curFilter = ef.buildFilter(filter);
if (res.curFilter instanceof MatchNone) {
res.setFound(0);
return res;
}
res.curFilter = ef.addDateRangeFilter(res.curFilter, IcalDefs.entityTypeEvent, start, end);
res.curFilter = ef.addLimits(res.curFilter, defaultFilterContext, res.delState);
if (res.curFilter instanceof MatchNone) {
res.setFound(0);
return res;
}
res.requiresSecondaryFetch = ef.requiresSecondaryFetch();
// res.canPage = ef.canPage();
res.curSort = sort;
final SearchRequestBuilder srb = getClient().prepareSearch(searchIndexes);
if (res.curQuery != null) {
srb.setQuery(res.curQuery);
}
srb.setSearchType(SearchType.COUNT).setPostFilter(res.curFilter).setFrom(0).setSize(0);
if (!Util.isEmpty(res.curSort)) {
SortOrder so;
for (final SortTerm st : res.curSort) {
if (st.isAscending()) {
so = SortOrder.ASC;
} else {
so = SortOrder.DESC;
}
srb.addSort(new FieldSortBuilder(ESQueryFilter.makePropertyRef(st.getProperties())).order(so));
}
}
res.latestStart = ef.getLatestStart();
res.earliestEnd = ef.getEarliestEnd();
if (debug) {
debug("Search: latestStart=" + res.latestStart + " earliestEnd=" + res.earliestEnd + " targetIndex=" + targetIndex + "; srb=" + srb);
}
final SearchResponse resp = srb.execute().actionGet();
if (debug) {
debug("Search: returned status " + resp.status() + " found: " + resp.getHits().getTotalHits());
}
res.setFound(resp.getHits().getTotalHits());
return res;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.SortOrder in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method getSearchResult.
@Override
public List<SearchResultEntry> getSearchResult(final SearchResult sres, final int offset, final int num, final int desiredAccess) throws CalFacadeException {
if (debug) {
debug("offset: " + offset + ", num: " + num);
}
final EsSearchResult res = (EsSearchResult) sres;
res.pageStart = offset;
final SearchRequestBuilder srb = getClient().prepareSearch(searchIndexes);
if (res.curQuery != null) {
srb.setQuery(res.curQuery);
}
srb.setSearchType(SearchType.QUERY_THEN_FETCH).setPostFilter(res.curFilter).setFrom(res.pageStart);
final int size;
if (num < 0) {
size = (int) sres.getFound();
} else {
size = num;
}
// TODO - need a configurable absolute max size for fetches
srb.setSize(size);
final List<SearchResultEntry> entities = new ArrayList<>(size);
if (!Util.isEmpty(res.curSort)) {
SortOrder so;
for (final SortTerm st : res.curSort) {
if (st.isAscending()) {
so = SortOrder.ASC;
} else {
so = SortOrder.DESC;
}
srb.addSort(new FieldSortBuilder(ESQueryFilter.makePropertyRef(st.getProperties())).order(so));
}
}
if (res.requiresSecondaryFetch) {
// Limit to href then fetch those
srb.addField(ESQueryFilter.hrefJname);
}
final SearchResponse resp = srb.execute().actionGet();
if (resp.status() != RestStatus.OK) {
if (debug) {
debug("Search returned status " + resp.status());
}
}
final SearchHits hitsResp = resp.getHits();
if ((hitsResp.getHits() == null) || (hitsResp.getHits().length == 0)) {
return entities;
}
// Break condition: No hits are returned
if (hitsResp.hits().length == 0) {
return entities;
}
final List<SearchHit> hits;
if (res.requiresSecondaryFetch) {
hits = multiFetch(hitsResp, res.recurRetrieval);
if (hits == null) {
return entities;
}
} else {
hits = Arrays.asList(hitsResp.getHits());
}
final Map<String, Collection<BwEventAnnotation>> overrides = new HashMap<>();
final Collection<EventInfo> masters = new TreeSet<>();
EntityBuilder.checkFlushCache(currentChangeToken());
/* If we are retrieving events with a time range query and we are asking for the
* master + overrides then we need to check that the master really has an
* instance in the given time range */
final boolean checkTimeRange = (res.recurRetrieval.mode == Rmode.overrides) && ((res.latestStart != null) || (res.earliestEnd != null));
final Set<String> excluded = new TreeSet<>();
for (final SearchHit hit : hits) {
res.pageStart++;
final String dtype = hit.getType();
if (dtype == null) {
throw new CalFacadeException("org.bedework.index.noitemtype");
}
final String kval = hit.getId();
if (kval == null) {
throw new CalFacadeException("org.bedework.index.noitemkey");
}
final EntityBuilder eb = getEntityBuilder(hit.sourceAsMap());
Object entity = null;
switch(dtype) {
case docTypeCollection:
entity = eb.makeCollection();
break;
case docTypeCategory:
entity = eb.makeCat();
break;
case docTypeContact:
entity = eb.makeContact();
break;
case docTypeLocation:
entity = eb.makeLocation();
break;
case docTypeEvent:
case docTypePoll:
entity = eb.makeEvent(kval, res.recurRetrieval.mode == Rmode.expanded);
final EventInfo ei = (EventInfo) entity;
final BwEvent ev = ei.getEvent();
final Response evrestResp = new Response();
restoreEvProps(evrestResp, ei);
if (evrestResp.getStatus() != ok) {
warn("Failed restore of ev props: " + evrestResp);
}
final Acl.CurrentAccess ca = res.accessCheck.checkAccess(ev, desiredAccess, true);
if ((ca == null) || !ca.getAccessAllowed()) {
continue;
}
ei.setCurrentAccess(ca);
if (ev instanceof BwEventAnnotation) {
if (excluded.contains(ev.getUid())) {
continue;
}
// Treat as override
final Collection<BwEventAnnotation> ov = overrides.computeIfAbsent(ev.getHref(), k -> new TreeSet<>());
ov.add((BwEventAnnotation) ev);
continue;
}
if (checkTimeRange && dtype.equals(docTypeEvent) && ev.getRecurring()) {
if (Util.isEmpty(RecurUtil.getPeriods(ev, 99, 1, res.latestStart, res.earliestEnd).instances)) {
excluded.add(ev.getUid());
continue;
}
}
masters.add(ei);
break;
}
entities.add(new SearchResultEntry(entity, dtype, hit.getScore()));
}
for (final EventInfo ei : masters) {
final BwEvent ev = ei.getEvent();
if (ev.getRecurring()) {
final Collection<BwEventAnnotation> ov = overrides.get(ev.getHref());
if (ov != null) {
for (final BwEventAnnotation ann : ov) {
final BwEvent proxy = new BwEventProxy(ann);
ann.setTarget(ev);
ann.setMaster(ev);
final EventInfo oei = new EventInfo(proxy);
ei.addOverride(oei);
}
}
}
}
return entities;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.SortOrder in project vertexium by visallo.
the class ElasticsearchSearchQueryBase method applySort.
protected void applySort(SearchRequestBuilder q) {
boolean sortedById = false;
for (SortContainer sortContainer : getParameters().getSortContainers()) {
SortOrder esOrder = sortContainer.direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC;
if (Element.ID_PROPERTY_NAME.equals(sortContainer.propertyName)) {
q.addSort(Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME, esOrder);
sortedById = true;
} else if (Edge.LABEL_PROPERTY_NAME.equals(sortContainer.propertyName)) {
q.addSort(SortBuilders.fieldSort(Elasticsearch5SearchIndex.EDGE_LABEL_FIELD_NAME).unmappedType(KEYWORD_UNMAPPED_TYPE).order(esOrder));
} else if (ExtendedDataRow.TABLE_NAME.equals(sortContainer.propertyName)) {
q.addSort(SortBuilders.fieldSort(Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME).unmappedType(KEYWORD_UNMAPPED_TYPE).order(esOrder));
} else if (ExtendedDataRow.ROW_ID.equals(sortContainer.propertyName)) {
q.addSort(SortBuilders.fieldSort(Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME).unmappedType(KEYWORD_UNMAPPED_TYPE).order(esOrder));
} else {
PropertyDefinition propertyDefinition = getGraph().getPropertyDefinition(sortContainer.propertyName);
if (propertyDefinition == null) {
continue;
}
if (!getSearchIndex().isPropertyInIndex(getGraph(), sortContainer.propertyName)) {
continue;
}
if (!propertyDefinition.isSortable()) {
throw new VertexiumException("Cannot sort on non-sortable fields");
}
String[] propertyNames = getPropertyNames(propertyDefinition.getPropertyName());
if (propertyNames.length > 1) {
String scriptSrc = "def fieldValues = []; for (def fieldName : params.fieldNames) { fieldValues.addAll(doc[fieldName].values); } " + "if (params.esOrder == 'asc') { Collections.sort(fieldValues); } else { Collections.sort(fieldValues, Collections.reverseOrder()); }" + "if (params.dataType == 'String') { return fieldValues; } else { return fieldValues.length > 0 ? fieldValues[0] : (params.esOrder == 'asc' ? Integer.MAX_VALUE : Integer.MIN_VALUE); }";
List<String> fieldNames = Arrays.stream(propertyNames).map(propertyName -> propertyName + (propertyDefinition.getDataType() == String.class ? Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX : "")).collect(Collectors.toList());
HashMap<String, Object> scriptParams = new HashMap<>();
scriptParams.put("fieldNames", fieldNames);
scriptParams.put("esOrder", esOrder == SortOrder.DESC ? "desc" : "asc");
scriptParams.put("dataType", propertyDefinition.getDataType().getSimpleName());
Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
ScriptSortBuilder.ScriptSortType sortType = propertyDefinition.getDataType() == String.class ? ScriptSortBuilder.ScriptSortType.STRING : ScriptSortBuilder.ScriptSortType.NUMBER;
q.addSort(SortBuilders.scriptSort(script, sortType).order(esOrder).sortMode(esOrder == SortOrder.DESC ? SortMode.MAX : SortMode.MIN));
} else {
String sortField = propertyNames[0];
String unmappedType = ElasticsearchTypes.fromJavaClass(propertyDefinition.getDataType());
if (propertyDefinition.getDataType() == String.class) {
sortField += Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX;
unmappedType = KEYWORD_UNMAPPED_TYPE;
}
q.addSort(SortBuilders.fieldSort(sortField).unmappedType(unmappedType).order(esOrder));
}
}
}
q.addSort("_score", SortOrder.DESC);
if (!sortedById) {
// If an id sort isn't specified, default is to sort by score and then sort id by ascending order after specified sorts
q.addSort("_uid", SortOrder.ASC);
}
}
Aggregations