use of 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());
}
}
}
use of 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));
}
use of 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);
}
use of 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("------------------------------------");
}
use of 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;
}
Aggregations