Search in sources :

Example 6 with FieldSortBuilder

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

the class InnerHitsIT method testRandomNested.

public void testRandomNested() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("type", "field1", "type=nested", "field2", "type=nested"));
    int numDocs = scaledRandomIntBetween(25, 100);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int[] field1InnerObjects = new int[numDocs];
    int[] field2InnerObjects = new int[numDocs];
    for (int i = 0; i < numDocs; i++) {
        int numInnerObjects = field1InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        XContentBuilder source = jsonBuilder().startObject().startArray("field1");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        numInnerObjects = field2InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        source.endArray().startArray("field2");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        source.endArray().endObject();
        requestBuilders.add(client().prepareIndex("idx", "type", String.format(Locale.ENGLISH, "%03d", i)).setSource(source));
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a").setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)), false));
    boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size), false));
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(boolQuery).setSize(numDocs).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) field1InnerObjects[i]));
        for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) field2InnerObjects[i]));
        for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with FieldSortBuilder

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

the class SearchScrollIT method testStringSortMissingAscTerminates.

public void testStringSortMissingAscTerminates() throws Exception {
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("test", "no_field", "type=keyword", "some_field", "type=keyword"));
    client().prepareIndex("test", "test", "1").setSource("some_field", "test").get();
    refresh();
    SearchResponse response = client().prepareSearch("test").setTypes("test").addSort(new FieldSortBuilder("no_field").order(SortOrder.ASC).missing("_last")).setScroll("1m").get();
    assertHitCount(response, 1);
    assertSearchHits(response, "1");
    response = client().prepareSearchScroll(response.getScrollId()).get();
    assertSearchResponse(response);
    assertHitCount(response, 1);
    assertNoSearchHits(response);
    response = client().prepareSearch("test").setTypes("test").addSort(new FieldSortBuilder("no_field").order(SortOrder.ASC).missing("_first")).setScroll("1m").get();
    assertHitCount(response, 1);
    assertSearchHits(response, "1");
    response = client().prepareSearchScroll(response.getScrollId()).get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getHits().length, equalTo(0));
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with FieldSortBuilder

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

the class ElasticsearchDao 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 = 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 9 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest2 method test.

private static void test() throws IOException {
    // TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("ecid").field("ecid").size(99999);
    // List<FieldSortBuilder> fieldSorts=new ArrayList<>();
    // fieldSorts.add(new FieldSortBuilder("sum_field").order(SortOrder.DESC));
    // termsBuilder.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", fieldSorts).from(6000).size(10));
    // 
    AggregationBuilder aggregation = AggregationBuilders.terms("ecid").field("ecid");
    List<FieldSortBuilder> fieldSorts = new ArrayList<>();
    fieldSorts.add(new FieldSortBuilder("id_").order(SortOrder.DESC));
    aggregation.subAggregation(new BucketSortPipelineAggregationBuilder("id", fieldSorts).from(0).size(10));
    SearchResponse searchResponse = search(aggregation);
    // 获取聚合结果
    Aggregations aggregations = searchResponse.getAggregations();
    Map<String, Object> map = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    agg(map, list, aggregations);
    logger.info("聚合查询结果:" + list);
    logger.info("------------------------------------");
}
Also used : BucketSortPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketsort.BucketSortPipelineAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) BucketSortPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketsort.BucketSortPipelineAggregationBuilder) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with FieldSortBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder in project uavstack by uavorg.

the class InvokeChainQueryHandler method buildSort.

@SuppressWarnings("rawtypes")
private SortBuilder[] buildSort(UAVHttpMessage data) {
    SortBuilder[] sorts = null;
    String sort = data.getRequest("sort");
    if (sort != null) {
        String[] sortFieldStrs = sort.split(",");
        List<SortBuilder> ls = new ArrayList<SortBuilder>();
        for (String sortFieldStr : sortFieldStrs) {
            String[] sortExp = sortFieldStr.split("=");
            SortBuilder stimeSort = new FieldSortBuilder(sortExp[0]);
            stimeSort.order(SortOrder.fromString(sortExp[1]));
            ls.add(stimeSort);
        }
        sorts = new SortBuilder[ls.size()];
        sorts = ls.toArray(sorts);
    } else {
        SortBuilder stimeSort = new FieldSortBuilder("stime");
        stimeSort.order(SortOrder.DESC);
        sorts = new SortBuilder[] { stimeSort };
    }
    return sorts;
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) ArrayList(java.util.ArrayList) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

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