Search in sources :

Example 1 with SortBuilder

use of org.elasticsearch.search.sort.SortBuilder in project elasticsearch by elastic.

the class DuelScrollIT method create.

private TestContext create(SearchType... searchTypes) throws Exception {
    assertAcked(prepareCreate("index").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "long").endObject().startObject("field2").field("type", "keyword").endObject().startObject("nested").field("type", "nested").startObject("properties").startObject("field3").field("type", "long").endObject().startObject("field4").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject()));
    int numDocs = 2 + randomInt(512);
    int scrollRequestSize = randomIntBetween(1, rarely() ? numDocs : numDocs / 2);
    boolean unevenRouting = randomBoolean();
    int numMissingDocs = scaledRandomIntBetween(0, numDocs / 100);
    IntHashSet missingDocs = new IntHashSet(numMissingDocs);
    for (int i = 0; i < numMissingDocs; i++) {
        while (!missingDocs.add(randomInt(numDocs))) {
        }
    }
    for (int i = 1; i <= numDocs; i++) {
        IndexRequestBuilder indexRequestBuilder = client().prepareIndex("index", "type", String.valueOf(i));
        if (missingDocs.contains(i)) {
            indexRequestBuilder.setSource("x", "y");
        } else {
            indexRequestBuilder.setSource(jsonBuilder().startObject().field("field1", i).field("field2", String.valueOf(i)).startObject("nested").field("field3", i).field("field4", String.valueOf(i)).endObject().endObject());
        }
        if (unevenRouting && randomInt(3) <= 2) {
            indexRequestBuilder.setRouting("a");
        }
        indexRandom(false, indexRequestBuilder);
    }
    refresh();
    final SortBuilder sort;
    if (randomBoolean()) {
        if (randomBoolean()) {
            sort = SortBuilders.fieldSort("field1").missing(1);
        } else {
            sort = SortBuilders.fieldSort("field2").missing("1");
        }
    } else {
        if (randomBoolean()) {
            sort = SortBuilders.fieldSort("nested.field3").missing(1);
        } else {
            sort = SortBuilders.fieldSort("nested.field4").missing("1");
        }
    }
    sort.order(randomBoolean() ? SortOrder.ASC : SortOrder.DESC);
    SearchType searchType = RandomPicks.randomFrom(random(), Arrays.asList(searchTypes));
    logger.info("numDocs={}, scrollRequestSize={}, sort={}, searchType={}", numDocs, scrollRequestSize, sort, searchType);
    return new TestContext(numDocs, scrollRequestSize, sort, searchType);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) IntHashSet(com.carrotsearch.hppc.IntHashSet) SearchType(org.elasticsearch.action.search.SearchType)

Example 2 with SortBuilder

use of org.elasticsearch.search.sort.SortBuilder in project sonarqube by SonarSource.

the class SortingTest method descending_sort_on_single_field.

@Test
public void descending_sort_on_single_field() throws Exception {
    Sorting sorting = new Sorting();
    sorting.add("updatedAt");
    SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
    sorting.fill(request, "updatedAt", false);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
    expectField(fields.get(0), "updatedAt", "_last", SortOrder.DESC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 3 with SortBuilder

use of org.elasticsearch.search.sort.SortBuilder in project sonarqube by SonarSource.

the class SortingTest method descending_sort_on_single_field_with_missing_in_last_position.

@Test
public void descending_sort_on_single_field_with_missing_in_last_position() throws Exception {
    Sorting sorting = new Sorting();
    sorting.add("updatedAt").missingLast();
    SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
    sorting.fill(request, "updatedAt", false);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
    expectField(fields.get(0), "updatedAt", "_first", SortOrder.DESC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 4 with SortBuilder

use of org.elasticsearch.search.sort.SortBuilder in project elasticsearch by elastic.

the class InnerHitBuilderTests method mutate.

static InnerHitBuilder mutate(InnerHitBuilder original) throws IOException {
    final InnerHitBuilder copy = serializedCopy(original);
    List<Runnable> modifiers = new ArrayList<>(12);
    modifiers.add(() -> copy.setFrom(randomValueOtherThan(copy.getFrom(), () -> randomIntBetween(0, 128))));
    modifiers.add(() -> copy.setSize(randomValueOtherThan(copy.getSize(), () -> randomIntBetween(0, 128))));
    modifiers.add(() -> copy.setExplain(!copy.isExplain()));
    modifiers.add(() -> copy.setVersion(!copy.isVersion()));
    modifiers.add(() -> copy.setTrackScores(!copy.isTrackScores()));
    modifiers.add(() -> copy.setName(randomValueOtherThan(copy.getName(), () -> randomAsciiOfLengthBetween(1, 16))));
    modifiers.add(() -> {
        if (randomBoolean()) {
            copy.setDocValueFields(randomValueOtherThan(copy.getDocValueFields(), () -> {
                return randomListStuff(16, () -> randomAsciiOfLengthBetween(1, 16));
            }));
        } else {
            copy.addDocValueField(randomAsciiOfLengthBetween(1, 16));
        }
    });
    modifiers.add(() -> {
        if (randomBoolean()) {
            copy.setScriptFields(randomValueOtherThan(copy.getScriptFields(), () -> {
                return new HashSet<>(randomListStuff(16, InnerHitBuilderTests::randomScript));
            }));
        } else {
            SearchSourceBuilder.ScriptField script = randomScript();
            copy.addScriptField(script.fieldName(), script.script());
        }
    });
    modifiers.add(() -> copy.setFetchSourceContext(randomValueOtherThan(copy.getFetchSourceContext(), () -> {
        FetchSourceContext randomFetchSourceContext;
        if (randomBoolean()) {
            randomFetchSourceContext = new FetchSourceContext(randomBoolean());
        } else {
            randomFetchSourceContext = new FetchSourceContext(true, generateRandomStringArray(12, 16, false), generateRandomStringArray(12, 16, false));
        }
        return randomFetchSourceContext;
    })));
    modifiers.add(() -> {
        if (randomBoolean()) {
            final List<SortBuilder<?>> sortBuilders = randomValueOtherThan(copy.getSorts(), () -> {
                List<SortBuilder<?>> builders = randomListStuff(16, () -> SortBuilders.fieldSort(randomAsciiOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values())));
                return builders;
            });
            copy.setSorts(sortBuilders);
        } else {
            copy.addSort(SortBuilders.fieldSort(randomAsciiOfLengthBetween(5, 20)));
        }
    });
    modifiers.add(() -> copy.setHighlightBuilder(randomValueOtherThan(copy.getHighlightBuilder(), HighlightBuilderTests::randomHighlighterBuilder)));
    modifiers.add(() -> {
        if (copy.getStoredFieldsContext() == null || randomBoolean()) {
            List<String> previous = copy.getStoredFieldsContext() == null ? Collections.emptyList() : copy.getStoredFieldsContext().fieldNames();
            List<String> newValues = randomValueOtherThan(previous, () -> randomListStuff(1, 16, () -> randomAsciiOfLengthBetween(1, 16)));
            copy.setStoredFieldNames(newValues);
        } else {
            copy.getStoredFieldsContext().addFieldName(randomAsciiOfLengthBetween(1, 16));
        }
    });
    randomFrom(modifiers).run();
    return copy;
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) ArrayList(java.util.ArrayList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) HighlightBuilderTests(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilderTests) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 5 with SortBuilder

use of org.elasticsearch.search.sort.SortBuilder in project elasticsearch-jdbc by jprante.

the class NodeTestUtils method assertTimestampSort.

protected void assertTimestampSort(String id, int expectedHits) {
    client(id).admin().indices().prepareRefresh(index).execute().actionGet();
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    SortBuilder sortBuilder = SortBuilders.fieldSort("_timestamp").order(SortOrder.DESC);
    SearchHits hits = client(id).prepareSearch(index).setTypes(type).setQuery(queryBuilder).addSort(sortBuilder).addFields("_source", "_timestamp").setSize(expectedHits).execute().actionGet().getHits();
    Long prev = Long.MAX_VALUE;
    for (SearchHit hit : hits) {
        if (hit.getFields().get("_timestamp") == null) {
            logger.warn("type mapping was not correctly applied for _timestamp field");
        }
        Long curr = hit.getFields().get("_timestamp").getValue();
        logger.info("timestamp = {}", curr);
        assertTrue(curr <= prev);
        prev = curr;
    }
    logger.info("{}/{} = {} hits", index, type, hits.getTotalHits());
    assertEquals(hits.getTotalHits(), expectedHits);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchHits(org.elasticsearch.search.SearchHits)

Aggregations

SortBuilder (org.elasticsearch.search.sort.SortBuilder)9 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)6 Client (org.elasticsearch.client.Client)6 Test (org.junit.Test)6 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 ArrayList (java.util.ArrayList)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 SearchType (org.elasticsearch.action.search.SearchType)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)1 HighlightBuilderTests (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilderTests)1