Search in sources :

Example 26 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder in project elastest-torm by elastest.

the class ElasticsearchService method getFindMessageSearchRequest.

/* ************** */
/* *** Search *** */
/* ************** */
public SearchRequest getFindMessageSearchRequest(String index, String msg, String component) {
    BoolQueryBuilder componentStreamBoolBuilder = QueryBuilders.boolQuery();
    TermQueryBuilder componentTerm = QueryBuilders.termQuery("component", component);
    TermQueryBuilder streamTerm = QueryBuilders.termQuery("stream", "default_log");
    componentStreamBoolBuilder.must(componentTerm);
    componentStreamBoolBuilder.must(streamTerm);
    TermQueryBuilder streamTypeTerm = QueryBuilders.termQuery("stream_type", "log");
    MatchPhrasePrefixQueryBuilder messageMatchTerm = QueryBuilders.matchPhrasePrefixQuery("message", msg);
    BoolQueryBuilder shouldBoolBuilder = QueryBuilders.boolQuery();
    shouldBoolBuilder.should(componentStreamBoolBuilder);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(streamTypeTerm);
    boolQueryBuilder.must(messageMatchTerm);
    boolQueryBuilder.must(shouldBoolBuilder);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(boolQueryBuilder);
    sourceBuilder.size(10000);
    sourceBuilder.sort(new FieldSortBuilder("@timestamp").order(SortOrder.ASC));
    sourceBuilder.sort(new FieldSortBuilder("_id").order(SortOrder.ASC));
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.source(sourceBuilder);
    return searchRequest;
}
Also used : MatchPhrasePrefixQueryBuilder(org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder) SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 27 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder in project Anserini by castorini.

the class SearchElastic method search.

public <K> ScoredDocuments search(String queryString) {
    SearchHits results = null;
    String specials = "+-=&|><!(){}[]^\"~*?:\\/";
    for (int i = 0; i < specials.length(); i++) {
        char c = specials.charAt(i);
        queryString = queryString.replace(String.valueOf(c), " ");
    }
    QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(queryString).defaultField("contents").analyzer("english");
    SearchRequest searchRequest = new SearchRequest(args.esIndex);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(query);
    sourceBuilder.size(args.hits);
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    sourceBuilder.sort(new FieldSortBuilder(IndexArgs.ID).order(SortOrder.ASC));
    searchRequest.source(sourceBuilder);
    try {
        SearchResponse searchResponse = client.search(searchRequest, COMMON_OPTIONS);
        results = searchResponse.getHits();
    } catch (Exception e) {
        LOG.error("Exception during ES query: ", e);
    }
    ScoreTiesAdjusterReranker reranker = new ScoreTiesAdjusterReranker();
    return reranker.rerank(ScoredDocuments.fromESDocs(results), null);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ScoreSortBuilder(org.elasticsearch.search.sort.ScoreSortBuilder) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 28 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder in project metron by apache.

the class ElasticsearchSearchDao method buildSearchRequest.

/**
 * Builds an Elasticsearch search request.
 * @param searchRequest The Metron search request.
 * @param queryBuilder
 * @return An Elasticsearch search request.
 */
private org.elasticsearch.action.search.SearchRequest buildSearchRequest(SearchRequest searchRequest, QueryBuilder queryBuilder) throws InvalidSearchException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Got search request; request={}", ElasticsearchUtils.toJSON(searchRequest).orElse("???"));
    }
    SearchSourceBuilder searchBuilder = new SearchSourceBuilder().size(searchRequest.getSize()).from(searchRequest.getFrom()).query(queryBuilder).trackScores(true);
    List<String> fields = searchRequest.getFields();
    // column metadata needed to understand the type of each sort field
    Map<String, FieldType> meta;
    try {
        meta = columnMetadataDao.getColumnMetadata(searchRequest.getIndices());
    } catch (IOException e) {
        throw new InvalidSearchException("Unable to get column metadata", e);
    }
    // handle sort fields
    for (SortField sortField : searchRequest.getSort()) {
        // what type is the sort field?
        FieldType sortFieldType = meta.getOrDefault(sortField.getField(), FieldType.OTHER);
        // sort order - if ascending missing values sorted last. otherwise, missing values sorted first
        org.elasticsearch.search.sort.SortOrder sortOrder = getElasticsearchSortOrder(sortField.getSortOrder());
        String missingSortOrder;
        if (sortOrder == org.elasticsearch.search.sort.SortOrder.DESC) {
            missingSortOrder = SORT_MISSING_LAST;
        } else {
            missingSortOrder = SORT_MISSING_FIRST;
        }
        // sort by the field - missing fields always last
        FieldSortBuilder sortBy = new FieldSortBuilder(sortField.getField()).order(sortOrder).missing(missingSortOrder).unmappedType(sortFieldType.getFieldType());
        searchBuilder.sort(sortBy);
    }
    // handle search fields
    if (fields != null) {
        searchBuilder.fetchSource("*", null);
    } else {
        searchBuilder.fetchSource(true);
    }
    List<String> facetFields = searchRequest.getFacetFields();
    // handle facet fields
    if (facetFields != null) {
        // https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_bucket_aggregations.html
        for (String field : facetFields) {
            String name = getFacetAggregationName(field);
            TermsAggregationBuilder terms = AggregationBuilders.terms(name).field(field);
            // new TermsBuilder(name).field(field);
            searchBuilder.aggregation(terms);
        }
    }
    // return the search request
    String[] indices = wildcardIndices(searchRequest.getIndices());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Built Elasticsearch request; indices={}, request={}", indices, searchBuilder.toString());
    }
    return new org.elasticsearch.action.search.SearchRequest().indices(indices).source(searchBuilder);
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) SortField(org.apache.metron.indexing.dao.search.SortField) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) FieldType(org.apache.metron.indexing.dao.search.FieldType) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) InvalidSearchException(org.apache.metron.indexing.dao.search.InvalidSearchException)

Example 29 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder 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 30 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder 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)

Aggregations

FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)32 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 ArrayList (java.util.ArrayList)10 SearchHits (org.elasticsearch.search.SearchHits)7 SearchHit (org.elasticsearch.search.SearchHit)6 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)6 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)5 SortBuilder (org.elasticsearch.search.sort.SortBuilder)5 IOException (java.io.IOException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 ScoreSortBuilder (org.elasticsearch.search.sort.ScoreSortBuilder)4 HashMap (java.util.HashMap)3 SortOrder (org.elasticsearch.search.sort.SortOrder)3 ScoreTiesAdjusterReranker (io.anserini.rerank.lib.ScoreTiesAdjusterReranker)2 FieldType (org.apache.metron.indexing.dao.search.FieldType)2 InvalidSearchException (org.apache.metron.indexing.dao.search.InvalidSearchException)2 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)2 SortField (org.apache.metron.indexing.dao.search.SortField)2 SortTerm (org.bedework.calfacade.filter.SortTerm)2