当前位置: 首页>>代码示例>>Java>>正文


Java SortOrder.ASC属性代码示例

本文整理汇总了Java中org.elasticsearch.search.sort.SortOrder.ASC属性的典型用法代码示例。如果您正苦于以下问题:Java SortOrder.ASC属性的具体用法?Java SortOrder.ASC怎么用?Java SortOrder.ASC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在org.elasticsearch.search.sort.SortOrder的用法示例。


在下文中一共展示了SortOrder.ASC属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: applySort

protected void applySort(SearchRequestBuilder q) {
    for (SortContainer sortContainer : getParameters().getSortContainers()) {
        SortOrder esOrder = sortContainer.direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC;
        if (Element.ID_PROPERTY_NAME.equals(sortContainer.propertyName)) {
            q.addSort("_uid", esOrder);
        } else if (Edge.LABEL_PROPERTY_NAME.equals(sortContainer.propertyName)) {
            q.addSort(Elasticsearch5SearchIndex.EDGE_LABEL_FIELD_NAME, esOrder);
        } else {
            PropertyDefinition propertyDefinition = getGraph().getPropertyDefinition(sortContainer.propertyName);
            if (propertyDefinition == null) {
                continue;
            }
            if (!getSearchIndex().isPropertyInIndex(getGraph(), sortContainer.propertyName)) {
                continue;
            }
            if (!propertyDefinition.isSortable()) {
                throw new MemgraphException("Cannot sort on non-sortable fields");
            }

            String[] propertyNames = getPropertyNames(propertyDefinition.getPropertyName());
            if (propertyNames.length > 1) {
                String scriptSrc = "def fieldValues = []; for (def fieldName : params.fieldNames) { fieldValues.addAll(doc[fieldName].values); } " +
                        "if (params.esOrder == 'asc') { Collections.sort(fieldValues); } else { Collections.sort(fieldValues, Collections.reverseOrder()); }" +
                        "if (params.dataType == 'String') { return fieldValues; } else { return fieldValues.length > 0 ? fieldValues[0] : (params.esOrder == 'asc' ? Integer.MAX_VALUE : Integer.MIN_VALUE); }";

                List<String> fieldNames = Arrays.stream(propertyNames).map(propertyName ->
                        propertyName + (propertyDefinition.getDataType() == String.class ? Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX : "")
                ).collect(Collectors.toList());
                HashMap<String, Object> scriptParams = new HashMap<>();
                scriptParams.put("fieldNames", fieldNames);
                scriptParams.put("esOrder", esOrder == SortOrder.DESC ? "desc" : "asc");
                scriptParams.put("dataType", propertyDefinition.getDataType().getSimpleName());
                Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
                ScriptSortBuilder.ScriptSortType sortType = propertyDefinition.getDataType() == String.class ? ScriptSortBuilder.ScriptSortType.STRING : ScriptSortBuilder.ScriptSortType.NUMBER;
                q.addSort(SortBuilders.scriptSort(script, sortType)
                        .order(esOrder)
                        .sortMode(esOrder == SortOrder.DESC ? SortMode.MAX : SortMode.MIN));
            } else {
                String sortField = propertyNames[0];
                if (propertyDefinition.getDataType() == String.class) {
                    sortField += Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX;
                }
                q.addSort(sortField, esOrder);
            }
        }
    }
}
 
开发者ID:mware-solutions,项目名称:memory-graph,代码行数:47,代码来源:ElasticsearchSearchQueryBase.java

示例2: searchQueryRaw

/**
 * Executes an ElasticSearch query. This is the core method of the class.
 * @param appid name of the {@link com.erudika.para.core.App}
 * @param type type of object
 * @param query the search query builder
 * @param pager a {@link com.erudika.para.utils.Pager}
 * @return a list of search results
 */
protected SearchHits searchQueryRaw(String appid, String type, QueryBuilder query, Pager... pager) {
	if (StringUtils.isBlank(appid)) {
		return null;
	}
	Pager page = ElasticSearchUtils.getPager(pager);
	SortOrder order = page.isDesc() ? SortOrder.DESC : SortOrder.ASC;
	int max = page.getLimit();
	int pageNum = (int) page.getPage();
	int start = (pageNum < 1 || pageNum > Config.MAX_PAGES) ? 0 : (pageNum - 1) * max;

	if (query == null) {
		query = matchAllQuery();
	}
	if (!StringUtils.isBlank(type)) {
		query = boolQuery().must(query).must(termQuery(Config._TYPE, type));
	}

	SearchHits hits = null;

	try {
		SearchRequestBuilder srb = client().prepareSearch(getIndexName(appid)).
				setSearchType(SearchType.DFS_QUERY_THEN_FETCH).
				setQuery(query).
				setSize(max);

		if (pageNum <= 1 && !StringUtils.isBlank(page.getLastKey())) {
			srb.searchAfter(new Object[]{NumberUtils.toLong(page.getLastKey())});
			srb.setFrom(0);
			srb.addSort(SortBuilders.fieldSort("_docid").order(order));
		} else {
			srb.setFrom(start);
			for (SortBuilder<?> sortField : ElasticSearchUtils.getSortFieldsFromPager(page)) {
				srb.addSort(sortField);
			}
		}

		logger.debug("Elasticsearch query: {}", srb.toString());

		hits = srb.execute().actionGet().getHits();
		page.setCount(hits.getTotalHits());
		if (hits.getHits().length > 0) {
			Object id = hits.getAt(hits.getHits().length - 1).getSourceAsMap().get("_docid");
			if (id != null) {
				page.setLastKey(id.toString());
			}
		}
	} catch (Exception e) {
		Throwable cause = e.getCause();
		String msg = cause != null ? cause.getMessage() : e.getMessage();
		logger.warn("No search results for type '{}' in app '{}': {}.", type, appid, msg);
	}

	return hits;
}
 
开发者ID:Erudika,项目名称:para-search-elasticsearch,代码行数:62,代码来源:ElasticSearch.java

示例3: getSortOrder

/**
 * Method to return the sorting order on basis of string param .
 */
private static SortOrder getSortOrder(String value) {
  return value.equalsIgnoreCase(ASC_ORDER) ? SortOrder.ASC : SortOrder.DESC;
}
 
开发者ID:project-sunbird,项目名称:sunbird-utils,代码行数:6,代码来源:ElasticSearchUtil.java


注:本文中的org.elasticsearch.search.sort.SortOrder.ASC属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。