use of org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.
the class HasChildQueryBuilderTests method testFromJson.
public void testFromJson() throws IOException {
String query = "{\n" + " \"has_child\" : {\n" + " \"query\" : {\n" + " \"range\" : {\n" + " \"mapped_string\" : {\n" + " \"from\" : \"agJhRET\",\n" + " \"to\" : \"zvqIq\",\n" + " \"include_lower\" : true,\n" + " \"include_upper\" : true,\n" + " \"boost\" : 1.0\n" + " }\n" + " }\n" + " },\n" + " \"type\" : \"child\",\n" + " \"score_mode\" : \"avg\",\n" + " \"min_children\" : 883170873,\n" + " \"max_children\" : 1217235442,\n" + " \"ignore_unmapped\" : false,\n" + " \"boost\" : 2.0,\n" + " \"_name\" : \"WNzYMJKRwePuRBh\",\n" + " \"inner_hits\" : {\n" + " \"name\" : \"inner_hits_name\",\n" + " \"ignore_unmapped\" : false,\n" + " \"from\" : 0,\n" + " \"size\" : 100,\n" + " \"version\" : false,\n" + " \"explain\" : false,\n" + " \"track_scores\" : false,\n" + " \"sort\" : [ {\n" + " \"mapped_string\" : {\n" + " \"order\" : \"asc\"\n" + " }\n" + " } ]\n" + " }\n" + " }\n" + "}";
HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query);
checkGeneratedJson(query, queryBuilder);
assertEquals(query, queryBuilder.maxChildren(), 1217235442);
assertEquals(query, queryBuilder.minChildren(), 883170873);
assertEquals(query, queryBuilder.boost(), 2.0f, 0.0f);
assertEquals(query, queryBuilder.queryName(), "WNzYMJKRwePuRBh");
assertEquals(query, queryBuilder.childType(), "child");
assertEquals(query, queryBuilder.scoreMode(), ScoreMode.Avg);
assertNotNull(query, queryBuilder.innerHit());
InnerHitBuilder expected = new InnerHitBuilder(new InnerHitBuilder(), queryBuilder.query(), "child", false).setName("inner_hits_name").setSize(100).addSort(new FieldSortBuilder("mapped_string").order(SortOrder.ASC));
assertEquals(query, queryBuilder.innerHit(), expected);
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.
the class HasParentQueryBuilderTests method doCreateTestQueryBuilder.
/**
* @return a {@link HasChildQueryBuilder} with random values all over the place
*/
@Override
protected HasParentQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
if (randomBoolean()) {
requiresRewrite = true;
innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
}
HasParentQueryBuilder hqb = new HasParentQueryBuilder(PARENT_TYPE, innerQueryBuilder, randomBoolean());
hqb.ignoreUnmapped(randomBoolean());
if (randomBoolean()) {
hqb.innerHit(new InnerHitBuilder().setName(randomAsciiOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)), hqb.ignoreUnmapped());
}
return hqb;
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project sonarqube by SonarSource.
the class Sorting method doFill.
private static void doFill(SearchRequestBuilder request, List<Field> fields, boolean asc) {
for (Field field : fields) {
FieldSortBuilder sortBuilder = SortBuilders.fieldSort(field.name);
boolean effectiveAsc = asc != field.reverse;
sortBuilder.order(effectiveAsc ? SortOrder.ASC : SortOrder.DESC);
boolean effectiveMissingLast = asc == field.missingLast;
sortBuilder.missing(effectiveMissingLast ? "_last" : "_first");
request.addSort(sortBuilder);
}
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project sonarqube by SonarSource.
the class RuleIndex method setSorting.
private static void setSorting(RuleQuery query, SearchRequestBuilder esSearch) {
/* integrate Query Sort */
String queryText = query.getQueryText();
if (query.getSortField() != null) {
FieldSortBuilder sort = SortBuilders.fieldSort(appendSortSuffixIfNeeded(query.getSortField()));
if (query.isAscendingSort()) {
sort.order(SortOrder.ASC);
} else {
sort.order(SortOrder.DESC);
}
esSearch.addSort(sort);
} else if (StringUtils.isNotEmpty(queryText)) {
esSearch.addSort(SortBuilders.scoreSort());
} else {
esSearch.addSort(appendSortSuffixIfNeeded(FIELD_RULE_UPDATED_AT), SortOrder.DESC);
// deterministic sort when exactly the same updated_at (same millisecond)
esSearch.addSort(appendSortSuffixIfNeeded(FIELD_RULE_KEY), SortOrder.ASC);
}
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testRandomParentChild.
public void testRandomParentChild() throws Exception {
assertAcked(prepareCreate("idx").addMapping("parent").addMapping("child1", "_parent", "type=parent").addMapping("child2", "_parent", "type=parent"));
int numDocs = scaledRandomIntBetween(5, 50);
List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
int child1 = 0;
int child2 = 0;
int[] child1InnerObjects = new int[numDocs];
int[] child2InnerObjects = new int[numDocs];
for (int parent = 0; parent < numDocs; parent++) {
String parentId = String.format(Locale.ENGLISH, "%03d", parent);
requestBuilders.add(client().prepareIndex("idx", "parent", parentId).setSource("{}", XContentType.JSON));
int numChildDocs = child1InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
int limit = child1 + numChildDocs;
for (; child1 < limit; child1++) {
requestBuilders.add(client().prepareIndex("idx", "child1", String.format(Locale.ENGLISH, "%04d", child1)).setParent(parentId).setSource("{}", XContentType.JSON));
}
numChildDocs = child2InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
limit = child2 + numChildDocs;
for (; child2 < limit; child2++) {
requestBuilders.add(client().prepareIndex("idx", "child2", String.format(Locale.ENGLISH, "%04d", child2)).setParent(parentId).setSource("{}", XContentType.JSON));
}
}
indexRandom(true, requestBuilders);
int size = randomIntBetween(0, numDocs);
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
SearchResponse searchResponse = client().prepareSearch("idx").setSize(numDocs).setTypes("parent").addSort("_uid", SortOrder.ASC).setQuery(boolQuery).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, numDocs);
assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
int offset1 = 0;
int offset2 = 0;
for (int parent = 0; parent < numDocs; parent++) {
SearchHit searchHit = searchResponse.getHits().getAt(parent);
assertThat(searchHit.getType(), equalTo("parent"));
assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "%03d", parent)));
assertThat(searchHit.getShard(), notNullValue());
SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.getTotalHits(), equalTo((long) child1InnerObjects[parent]));
for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
SearchHit innerHit = inner.getAt(child);
assertThat(innerHit.getType(), equalTo("child1"));
String childId = String.format(Locale.ENGLISH, "%04d", offset1 + child);
assertThat(innerHit.getId(), equalTo(childId));
assertThat(innerHit.getNestedIdentity(), nullValue());
}
offset1 += child1InnerObjects[parent];
inner = searchHit.getInnerHits().get("b");
assertThat(inner.getTotalHits(), equalTo((long) child2InnerObjects[parent]));
for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
SearchHit innerHit = inner.getAt(child);
assertThat(innerHit.getType(), equalTo("child2"));
String childId = String.format(Locale.ENGLISH, "%04d", offset2 + child);
assertThat(innerHit.getId(), equalTo(childId));
assertThat(innerHit.getNestedIdentity(), nullValue());
}
offset2 += child2InnerObjects[parent];
}
}
Aggregations