本文整理汇总了Java中org.elasticsearch.search.sort.SortBuilders类的典型用法代码示例。如果您正苦于以下问题:Java SortBuilders类的具体用法?Java SortBuilders怎么用?Java SortBuilders使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SortBuilders类属于org.elasticsearch.search.sort包,在下文中一共展示了SortBuilders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sortAdapter
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
/**
*
* @param sort_fileds
* @return
*/
public static List<SortBuilder> sortAdapter(List<String> sort_fileds) {
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
for (String sort : sort_fileds) {
String[] items = StringUtils.split(sort, " ");
if (items.length > 2 || items.length < 2) {
throw new RuntimeException("排序参数格式不正确,必须为:filed desc|asc,多个filed以逗号分隔!");
}
String[] fileds = items[0].split(",");
for (String filed : fileds) {
SortBuilder sortb = null;
if (items[0].equalsIgnoreCase("desc")) {
sortb = SortBuilders.fieldSort(filed).order(SortOrder.DESC);
} else {
sortb = SortBuilders.fieldSort(filed).order(SortOrder.ASC);
}
sortBuilders.add(sortb);
}
}
return sortBuilders;
}
示例2: fetchReleaseRecord
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
private CmsRelease fetchReleaseRecord(String nsPath, Date ts, int genTime) throws InterruptedException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CmsConstants.SEARCH_TS_PATTERN);
Thread.sleep(3000);
SearchQuery latestRelease = new NativeSearchQueryBuilder()
.withIndices("cms-*")
.withTypes("release").withFilter(
FilterBuilders.andFilter(
FilterBuilders.queryFilter(QueryBuilders.termQuery("nsPath.keyword", nsPath)),
FilterBuilders.queryFilter(QueryBuilders.rangeQuery("created").
from(simpleDateFormat.format(DateUtils.addMinutes(ts, -(genTime + 10)))).
to(simpleDateFormat.format(ts))))).
withSort(SortBuilders.fieldSort("created").order(SortOrder.DESC)).build();
List<CmsReleaseSearch> ciList = indexer.getTemplate().queryForList(latestRelease, CmsReleaseSearch.class);
if (!ciList.isEmpty()) {
return ciList.get(0);
}
throw new RuntimeException("Cant find bom release for deployment plan generation event");
}
示例3: executeRequest
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
/**
* Execute query request with filter and aggregations.
*
* @param query
* search query
* @return ES search response
* @throws ODataApplicationException
* if any error appeared during executing request
*/
public SearchResponse executeRequest(SearchQuery query) throws ODataApplicationException {
Pagination pagination = query.getPagination();
SearchRequestBuilder requestBuilder = client.prepareSearch(query.getIndex())
.setTypes(query.getTypes()).setQuery(query.getQueryBuilder());
if (pagination != null) {
List<Sort> orderBy = pagination.getOrderBy();
for (Sort sort : orderBy) {
FieldSortBuilder sortQuery = SortBuilders.fieldSort(sort.getProperty())
.order(SortOrder.valueOf(sort.getDirection().toString()));
requestBuilder.addSort(sortQuery);
}
requestBuilder.setSize(pagination.getTop()).setFrom(pagination.getSkip());
}
Set<String> fields = query.getFields();
if (fields != null && !fields.isEmpty()) {
requestBuilder.setFetchSource(fields.toArray(new String[fields.size()]), null);
}
return executeRequest(requestBuilder);
}
示例4: search
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
@Test
void search() {
TestDocument document = createDocument("1", "value1", 1);
elasticSearch.flush("document");
SearchRequest request = new SearchRequest();
request.query = QueryBuilders.matchQuery("string_field", document.stringField);
request.sorts.add(SortBuilders.scriptSort(new Script("doc['num_field'].value * 3"), ScriptSortBuilder.ScriptSortType.NUMBER));
SearchResponse<TestDocument> response = documentType.search(request);
assertEquals(1, response.totalHits);
TestDocument returnedDocument = response.hits.get(0);
assertEquals(document.stringField, returnedDocument.stringField);
}
示例5: testDocIdSort
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
public void testDocIdSort() throws Exception {
int numShards = setupIndex(true);
SearchResponse sr = client().prepareSearch("test")
.setQuery(matchAllQuery())
.setSize(0)
.get();
int numDocs = (int) sr.getHits().getTotalHits();
assertThat(numDocs, equalTo(NUM_DOCS));
int max = randomIntBetween(2, numShards*3);
for (String field : new String[]{"_uid", "random_int", "static_int"}) {
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("test")
.setQuery(matchAllQuery())
.setScroll(new Scroll(TimeValue.timeValueSeconds(10)))
.setSize(fetchSize)
.addSort(SortBuilders.fieldSort("_doc"));
assertSearchSlicesWithScroll(request, field, max);
}
}
示例6: testNumericSort
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
public void testNumericSort() throws Exception {
int numShards = setupIndex(true);
SearchResponse sr = client().prepareSearch("test")
.setQuery(matchAllQuery())
.setSize(0)
.get();
int numDocs = (int) sr.getHits().getTotalHits();
assertThat(numDocs, equalTo(NUM_DOCS));
int max = randomIntBetween(2, numShards*3);
for (String field : new String[]{"_uid", "random_int", "static_int"}) {
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("test")
.setQuery(matchAllQuery())
.setScroll(new Scroll(TimeValue.timeValueSeconds(10)))
.addSort(SortBuilders.fieldSort("random_int"))
.setSize(fetchSize);
assertSearchSlicesWithScroll(request, field, max);
}
}
示例7: getContributionsByCandName
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
public void getContributionsByCandName(Client client, String candName, int i)
{
QueryBuilder matchQuery = QueryBuilders.matchQuery("candNm", candName);
FilterBuilder contribRangeFilter = FilterBuilders.rangeFilter("contbReceiptAmt").gte(i);
StatisticalFacetBuilder facet = FacetBuilders.statisticalFacet("stat1").field("contbReceiptAmt");
SearchRequestBuilder request = client.prepareSearch("contributions")
.addSort(SortBuilders.fieldSort("contbReceiptAmt").order(SortOrder.DESC))
.setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(matchQuery).setPostFilter(contribRangeFilter)
.addFacet(facet).setFrom(0).setSize(100)
.addFields("contbrNm", "candNm", "contbrEmployer", "contbReceiptAmt");
System.out.println("SEARCH QUERY: " + request.toString());
SearchResponse response = request.execute().actionGet();
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits)
{
Map<String, SearchHitField> fields = hit.getFields();
System.out.println(hit.getId() + ", contbrEmployer=" + fields.get("contbrEmployer").getValue().toString());
}
}
示例8: findPropertiesByLocation
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
@Override
public List<Property> findPropertiesByLocation(final GeoPoint geoPoint, Double distance, final SortOrder sortOrder) {
GeoDistanceFilterBuilder filter = FilterBuilders.geoDistanceFilter("location").point(geoPoint.getLat(), geoPoint.getLon())
.distance(distance, DistanceUnit.KILOMETERS);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withFilter(filter)
.withSort(
SortBuilders.geoDistanceSort("location").point(geoPoint.getLat(), geoPoint.getLon())
.order(sortOrder == null ? SortOrder.ASC : sortOrder)).build();
searchQuery.addIndices("searchahouse");
searchQuery.addTypes("property");
List<Property> properties = this.elasticsearchOperations.queryForList(searchQuery, Property.class);
return properties;
}
示例9: getMostRecentEntry
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
/**
* Gets the creation data of the single newest entry.
*
* @return ISO8601 formatted time of most recent entry, or null on empty or error.
*/
private String getMostRecentEntry() {
long totalEntries = client.prepareCount(index).setQuery(matchAllQuery()).execute().actionGet().getCount();
if (totalEntries > 0) {
FilteredQueryBuilder updatedAtQuery = QueryBuilders
.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("created_at"));
FieldSortBuilder updatedAtSort = SortBuilders.fieldSort("created_at").order(SortOrder.DESC);
SearchResponse response = client.prepareSearch(index)
.setQuery(updatedAtQuery)
.addSort(updatedAtSort)
.setSize(1)
.execute()
.actionGet();
String createdAt = (String) response.getHits().getAt(0).getSource().get("created_at");
logger.debug("Most recent event was created at {}", createdAt);
return createdAt;
} else {
// getData will get all data on a null.
logger.info("No existing entries, assuming first run");
return null;
}
}
示例10: mostRecentDocument
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
public Long mostRecentDocument(String index) {
if (client() == null) {
return null;
}
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
SortBuilder sort = SortBuilders.fieldSort("_timestamp").order(SortOrder.DESC);
SearchResponse searchResponse = searchRequestBuilder.setIndices(index).addField("_timestamp").setSize(1).addSort(sort).execute().actionGet();
if (searchResponse.getHits().getHits().length == 1) {
SearchHit hit = searchResponse.getHits().getHits()[0];
if (hit.getFields().get("_timestamp") != null) {
return hit.getFields().get("_timestamp").getValue();
} else {
return 0L;
}
}
return null;
}
示例11: applyDefaultSortPredicates
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
/**
* Apply our default sort predicate logic
*/
private void applyDefaultSortPredicates( final SearchRequestBuilder srb, final GeoSortFields geoFields ) {
//we have geo fields, sort through them in visit order
for ( String geoField : geoFields.fields() ) {
final GeoDistanceSortBuilder geoSort = geoFields.applyOrder( geoField, SortOrder.ASC );
srb.addSort( geoSort );
}
//now sort by edge timestamp, then entity id
//sort by the edge timestamp
srb.addSort( SortBuilders.fieldSort( IndexingUtils.EDGE_TIMESTAMP_FIELDNAME ).order( SortOrder.DESC ) );
// removing secondary sort by entity ID -- takes ES resources and provides no benefit
//sort by the entity id if our times are equal
//srb.addSort( SortBuilders.fieldSort( IndexingUtils.ENTITY_ID_FIELDNAME ).order( SortOrder.ASC ) );
return;
}
示例12: fieldSort
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
@Override
public QueryBuilderHelper fieldSort(String fieldName, boolean desc) {
if (fieldName == null) {
return this;
}
fieldSort = true;
FieldSortBuilder sortBuilder = SortBuilders.fieldSort(fieldName);
if (desc) {
sortBuilder.order(SortOrder.DESC);
} else {
sortBuilder.order(SortOrder.ASC);
}
// TODO: change to use sortBuilder.unmappedType
sortBuilder.ignoreUnmapped(true);
searchRequestBuilder.addSort(sortBuilder);
return this;
}
示例13: addFuzzyQuery
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
private QueryBuilder addFuzzyQuery(
IndexQueryParams queryParams,
QueryBuilder queryBuilder,
SearchRequestBuilder reqBuilder
) {
if (queryParams.getFuzzyQueryParams().isPresent()) {
queryBuilder = addTitleQuery(queryParams, queryBuilder);
if (queryParams.getBroadcastWeighting().isPresent()) {
queryBuilder = BroadcastQueryBuilder.build(
queryBuilder,
queryParams.getBroadcastWeighting().get()
);
} else {
queryBuilder = BroadcastQueryBuilder.build(queryBuilder, 5f);
}
reqBuilder.addSort(SortBuilders.scoreSort().order(SortOrder.DESC));
}
return queryBuilder;
}
示例14: ShellScope
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
/**
* Creates a new <code>ShellScope</code> given the actual scope object
* @param scope the actual scope object that depends on the engine in use
*/
ShellScope(Scope scope, ResourceRegistry resourceRegistry) {
this.scope = scope;
this.resourceRegistry = resourceRegistry;
registerJavaClass(Requests.class);
registerJavaClass(SearchSourceBuilder.class);
registerJavaClass(QueryBuilders.class);
registerJavaClass(FilterBuilders.class);
registerJavaClass(SortBuilders.class);
registerJavaClass(FacetBuilders.class);
registerJavaClass(RescoreBuilder.class);
registerJavaClass(SuggestBuilder.class);
registerJavaClass(AliasAction.class);
registerJavaClass(HttpParameters.class);
registerJavaClass(AllocateAllocationCommand.class);
registerJavaClass(CancelAllocationCommand.class);
registerJavaClass(MoveAllocationCommand.class);
registerJavaClass(ShardId.class);
}
示例15: buildRequest
import org.elasticsearch.search.sort.SortBuilders; //导入依赖的package包/类
private SearchRequestBuilder buildRequest(String script, Object... params) {
ensureGreen("test");
Map<String, Object> paramsMap = new HashMap<>();
assert (params.length % 2 == 0);
for (int i = 0; i < params.length; i += 2) {
paramsMap.put(params[i].toString(), params[i + 1]);
}
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.fieldSort("_uid")
.order(SortOrder.ASC))
.addScriptField("foo", new Script(ScriptType.INLINE, "expression", script, paramsMap));
return req;
}