Search in sources :

Example 6 with SortOrder

use of org.graylog.shaded.elasticsearch7.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);
    }
}
Also used : SortOrder(org.elasticsearch.search.sort.SortOrder)

Example 7 with SortOrder

use of org.graylog.shaded.elasticsearch7.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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SortOrder(org.elasticsearch.search.sort.SortOrder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ScoreSortBuilder(org.elasticsearch.search.sort.ScoreSortBuilder)

Example 8 with SortOrder

use of org.graylog.shaded.elasticsearch7.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;
}
Also used : SortTerm(org.bedework.calfacade.filter.SortTerm) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) SortOrder(org.elasticsearch.search.sort.SortOrder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 9 with SortOrder

use of org.graylog.shaded.elasticsearch7.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;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) EventInfo(org.bedework.calfacade.svc.EventInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BwEvent(org.bedework.calfacade.BwEvent) BwEventProxy(org.bedework.calfacade.BwEventProxy) TreeSet(java.util.TreeSet) SearchHits(org.elasticsearch.search.SearchHits) SortTerm(org.bedework.calfacade.filter.SortTerm) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SortOrder(org.elasticsearch.search.sort.SortOrder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Acl(org.bedework.access.Acl) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) SearchResponse(org.elasticsearch.action.search.SearchResponse) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Response(org.bedework.calfacade.responses.Response) IndicesAliasesResponse(org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse) GetEntityResponse(org.bedework.calfacade.responses.GetEntityResponse) IndicesStatusResponse(org.elasticsearch.action.admin.indices.status.IndicesStatusResponse) DeleteByQueryResponse(org.elasticsearch.action.deletebyquery.DeleteByQueryResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) IndexDeleteByQueryResponse(org.elasticsearch.action.deletebyquery.IndexDeleteByQueryResponse) ReindexResponse(org.bedework.calfacade.indexing.ReindexResponse) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexStatsResponse(org.bedework.calfacade.indexing.IndexStatsResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) BwEventAnnotation(org.bedework.calfacade.BwEventAnnotation) Collection(java.util.Collection) SearchResultEntry(org.bedework.calfacade.indexing.SearchResultEntry)

Example 10 with SortOrder

use of org.graylog.shaded.elasticsearch7.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);
    }
}
Also used : Script(org.elasticsearch.script.Script) SortOrder(org.elasticsearch.search.sort.SortOrder)

Aggregations

SortOrder (org.elasticsearch.search.sort.SortOrder)10 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)4 SearchHit (org.elasticsearch.search.SearchHit)4 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 SortTerm (org.bedework.calfacade.filter.SortTerm)2 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)2 SortBy (com.b2international.index.query.SortBy)1 MultiSortBy (com.b2international.index.query.SortBy.MultiSortBy)1 SortByField (com.b2international.index.query.SortBy.SortByField)1 SortByScript (com.b2international.index.query.SortBy.SortByScript)1 FutureHelper.error (com.codingchili.core.context.FutureHelper.error)1 FutureHelper.result (com.codingchili.core.context.FutureHelper.result)1 StorageContext (com.codingchili.core.context.StorageContext)1