use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.SortOrder in project chili-core by codingchili.
the class ElasticMap method query.
@Override
public QueryBuilder<Value> query(String field) {
return new AbstractQueryBuilder<Value>(this, field) {
List<BoolQueryBuilder> statements = new ArrayList<>();
BoolQueryBuilder builder = new BoolQueryBuilder();
@Override
public QueryBuilder<Value> and(String attribute) {
setAttribute(attribute);
return this;
}
@Override
public QueryBuilder<Value> or(String attribute) {
setAttribute(attribute);
statements.add(builder);
builder = new BoolQueryBuilder();
return this;
}
@Override
public QueryBuilder<Value> between(Long minimum, Long maximum) {
builder.must(QueryBuilders.rangeQuery(attribute()).gte(minimum).lte(maximum));
return this;
}
@Override
public QueryBuilder<Value> like(String text) {
text = Validator.toPlainText(text).toLowerCase();
builder.must(QueryBuilders.wildcardQuery(attribute(), "*" + text + "*"));
return this;
}
@Override
public QueryBuilder<Value> startsWith(String text) {
builder.must(QueryBuilders.matchPhrasePrefixQuery(attribute(), text));
return this;
}
@Override
public QueryBuilder<Value> in(Comparable... list) {
BoolQueryBuilder bool = new BoolQueryBuilder().minimumShouldMatch(1);
for (Comparable item : list) {
bool.should(QueryBuilders.matchPhraseQuery(attribute(), item));
}
builder.must(bool);
return this;
}
@Override
public QueryBuilder<Value> equalTo(Comparable match) {
builder.must(QueryBuilders.matchPhraseQuery(attribute(), match));
return this;
}
@Override
public QueryBuilder<Value> matches(String regex) {
if (regex.contains("^") || regex.contains("$")) {
// remove unsupported characters in ElasticSearch query.
regex = regex.replaceAll("[\\^$]", "");
}
builder.must(QueryBuilders.regexpQuery(attribute(), regex.toLowerCase()).flags(RegexpFlag.ALL));
return this;
}
@Override
public void execute(Handler<AsyncResult<Collection<Value>>> handler) {
if (!builder.equals(new BoolQueryBuilder())) {
statements.add(builder);
}
BoolQueryBuilder query = new BoolQueryBuilder().minimumShouldMatch(1);
for (BoolQueryBuilder statement : statements) {
query.should(statement);
}
getRequestWithOptions().setQuery(query).execute(new ElasticHandler<>(response -> {
handler.handle(result(listFrom(response.getHits().getHits())));
}, exception -> handler.handle(error(exception))));
}
private SearchRequestBuilder getRequestWithOptions() {
SearchRequestBuilder request = client.prepareSearch(context.database()).setTypes(context.collection());
if (isOrdered) {
switch(sortOrder) {
case ASCENDING:
request.addSort(getOrderByAttribute(), SortOrder.ASC);
break;
case DESCENDING:
request.addSort(getOrderByAttribute(), SortOrder.DESC);
}
}
request.setSize(pageSize);
request.setFrom(pageSize * page);
return request;
}
};
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.SortOrder in project engine by craftercms.
the class ContentTypeBasedDataFetcher method doGet.
/**
* {@inheritDoc}
*/
@Override
public Object doGet(final DataFetchingEnvironment env) {
Field field = env.getMergedField().getSingleField();
String fieldName = field.getName();
// Get arguments for pagination & sorting
int offset = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_OFFSET)).orElse(0);
int limit = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_LIMIT)).orElse(defaultLimit);
String sortBy = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_BY)).orElse(defaultSortField);
String sortOrder = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_ORDER)).orElse(defaultSortOrder);
List<String> queryFieldIncludes = new LinkedList<>();
// Add content-type to includes, we might need it for a GraphQL TypeResolver
queryFieldIncludes.add(QUERY_FIELD_NAME_CONTENT_TYPE);
List<Map<String, Object>> items = new LinkedList<>();
Map<String, Object> result = new HashMap<>(2);
result.put(FIELD_NAME_ITEMS, items);
// Setup the ES query
SearchSourceBuilder source = new SearchSourceBuilder();
BoolQueryBuilder query = boolQuery();
source.query(query).from(offset).size(limit).sort(sortBy, SortOrder.fromString(sortOrder));
StopWatch watch = new StopWatch(field.getName() + " - " + field.getAlias());
watch.start("build filters");
// Filter by the content-type
switch(fieldName) {
case FIELD_NAME_CONTENT_ITEMS:
query.filter(existsQuery(QUERY_FIELD_NAME_CONTENT_TYPE));
break;
case FIELD_NAME_PAGES:
query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_PAGE));
break;
case FIELD_NAME_COMPONENTS:
query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_COMPONENT));
break;
default:
// Get the content-type name from the field name
query.filter(termQuery(QUERY_FIELD_NAME_CONTENT_TYPE, getOriginalName(fieldName)));
break;
}
// Check the selected fields to build the ES query
Optional<Field> itemsField = field.getSelectionSet().getSelections().stream().map(f -> (Field) f).filter(f -> f.getName().equals(FIELD_NAME_ITEMS)).findFirst();
if (itemsField.isPresent()) {
List<Selection> selections = itemsField.get().getSelectionSet().getSelections();
selections.forEach(selection -> processSelection(StringUtils.EMPTY, selection, query, queryFieldIncludes, env));
}
// Only fetch the selected fields for better performance
source.fetchSource(queryFieldIncludes.toArray(new String[0]), new String[0]);
watch.stop();
logger.debug("Executing query: {}", source);
watch.start("searching items");
SearchResponse response = elasticsearch.search(new SearchRequest().source(source));
watch.stop();
watch.start("processing items");
result.put(FIELD_NAME_TOTAL, response.getHits().totalHits);
if (response.getHits().totalHits > 0) {
for (SearchHit hit : response.getHits().getHits()) {
items.add(fixItems(hit.getSourceAsMap()));
}
}
watch.stop();
if (logger.isTraceEnabled()) {
logger.trace(watch.prettyPrint());
}
return result;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.SortOrder in project snow-owl by b2ihealthcare.
the class EsDocumentSearcher method addSort.
private void addSort(DocumentMapping mapping, SearchSourceBuilder reqSource, SortBy sortBy) {
for (final SortBy item : getSortFields(sortBy)) {
if (item instanceof SortByField) {
SortByField sortByField = (SortByField) item;
String field = sortByField.getField();
SortBy.Order order = sortByField.getOrder();
SortOrder sortOrder = order == SortBy.Order.ASC ? SortOrder.ASC : SortOrder.DESC;
switch(field) {
case SortBy.FIELD_SCORE:
// XXX: default order for scores is *descending*
reqSource.sort(SortBuilders.scoreSort().order(sortOrder));
break;
case SortBy.FIELD_DEFAULT:
// Replace special field with default sort field from mapping
reqSource.sort(SortBuilders.fieldSort(mapping.getDefaultSortField()).order(sortOrder));
break;
default:
// Use field name directly otherwise
reqSource.sort(SortBuilders.fieldSort(field).order(sortOrder));
break;
}
} else if (item instanceof SortByScript) {
SortByScript sortByScript = (SortByScript) item;
SortBy.Order order = sortByScript.getOrder();
SortOrder sortOrder = order == SortBy.Order.ASC ? SortOrder.ASC : SortOrder.DESC;
reqSource.sort(SortBuilders.scriptSort(sortByScript.toEsScript(mapping), sortByScript.getSortType()).order(sortOrder));
} else {
throw new UnsupportedOperationException("Unsupported SortBy implementation " + item);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.SortOrder in project fess by codelibs.
the class QueryHelper method convertTermQuery.
protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery, final float boost) {
final String field = termQuery.getTerm().field();
final String text = termQuery.getTerm().text();
if (fessConfig.getQueryReplaceTermWithPrefixQueryAsBoolean() && text.length() > 1 && text.endsWith("*")) {
return convertPrefixQuery(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))), boost);
} else if (Constants.DEFAULT_FIELD.equals(field)) {
context.addFieldLog(field, text);
context.addHighlightedQuery(text);
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b * boost));
} else if ("sort".equals(field)) {
final String[] values = text.split("\\.");
if (values.length > 2) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQuerySortValue(UserMessages.GLOBAL_PROPERTY_KEY, text), "Invalid sort field: " + termQuery);
}
final String sortField = values[0];
if (!isSortField(sortField)) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnsupportedSortField(UserMessages.GLOBAL_PROPERTY_KEY, sortField), "Unsupported sort field: " + termQuery);
}
SortOrder sortOrder;
if (values.length == 2) {
sortOrder = SortOrder.DESC.toString().equalsIgnoreCase(values[1]) ? SortOrder.DESC : SortOrder.ASC;
if (sortOrder == null) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnsupportedSortOrder(UserMessages.GLOBAL_PROPERTY_KEY, values[1]), "Invalid sort order: " + termQuery);
}
} else {
sortOrder = SortOrder.ASC;
}
context.addSorts(createFieldSortBuilder(sortField, sortOrder));
return null;
} else if (INURL_FIELD.equals(field)) {
return QueryBuilders.wildcardQuery(fessConfig.getIndexFieldUrl(), "*" + text + "*").boost(boost);
} else if (isSearchField(field)) {
context.addFieldLog(field, text);
context.addHighlightedQuery(text);
if (notAnalyzedFieldSet.contains(field)) {
return QueryBuilders.termQuery(field, text).boost(boost);
} else {
return QueryBuilders.matchPhraseQuery(field, text).boost(boost);
}
} else {
final String origQuery = termQuery.toString();
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
context.addHighlightedQuery(origQuery);
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b * boost));
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.SortOrder in project incubator-sdap-mudrod by apache.
the class Searcher method searchByQuery.
/**
* Main method of semantic search
*
* @param index index name in Elasticsearch
* @param type type name in Elasticsearch
* @param query regular query string
* @param queryOperator query mode- query, or, and
* @param rankOption a keyword used to dertermine the ElasticSearch SortOrder
* @return a list of search result
*/
@SuppressWarnings("unchecked")
public List<SResult> searchByQuery(String index, String type, String query, String queryOperator, String rankOption) {
boolean exists = es.getClient().admin().indices().prepareExists(index).execute().actionGet().isExists();
if (!exists) {
return new ArrayList<>();
}
SortOrder order = null;
String sortFiled = "";
switch(rankOption) {
case "Rank-AllTimePopularity":
sortFiled = "Dataset-AllTimePopularity";
order = SortOrder.DESC;
break;
case "Rank-MonthlyPopularity":
sortFiled = "Dataset-MonthlyPopularity";
order = SortOrder.DESC;
break;
case "Rank-UserPopularity":
sortFiled = "Dataset-UserPopularity";
order = SortOrder.DESC;
break;
case "Rank-LongName-Full":
sortFiled = "Dataset-LongName.raw";
order = SortOrder.ASC;
break;
case "Rank-ShortName-Full":
sortFiled = "Dataset-ShortName.raw";
order = SortOrder.ASC;
break;
case "Rank-GridSpatialResolution":
sortFiled = "Dataset-GridSpatialResolution";
order = SortOrder.DESC;
break;
case "Rank-SatelliteSpatialResolution":
sortFiled = "Dataset-SatelliteSpatialResolution";
order = SortOrder.DESC;
break;
case "Rank-StartTimeLong-Long":
sortFiled = "DatasetCoverage-StartTimeLong-Long";
order = SortOrder.ASC;
break;
case "Rank-StopTimeLong-Long":
sortFiled = "DatasetCoverage-StopTimeLong-Long";
order = SortOrder.DESC;
break;
default:
sortFiled = "Dataset-ShortName.raw";
order = SortOrder.ASC;
break;
}
Dispatcher dp = new Dispatcher(this.getConfig(), this.getES(), null);
BoolQueryBuilder qb = dp.createSemQuery(query, 1.0, queryOperator);
List<SResult> resultList = new ArrayList<>();
SearchRequestBuilder builder = es.getClient().prepareSearch(index).setTypes(type).setQuery(qb).addSort(sortFiled, order).setSize(500).setTrackScores(true);
SearchResponse response = builder.execute().actionGet();
for (SearchHit hit : response.getHits().getHits()) {
Map<String, Object> result = hit.getSource();
Double relevance = Double.valueOf(NDForm.format(hit.getScore()));
String shortName = (String) result.get("Dataset-ShortName");
String longName = (String) result.get("Dataset-LongName");
ArrayList<String> topicList = (ArrayList<String>) result.get("DatasetParameter-Variable");
String topic = "";
if (null != topicList) {
topic = String.join(", ", topicList);
}
String content = (String) result.get("Dataset-Description");
if (!"".equals(content)) {
int maxLength = (content.length() < MAX_CHAR) ? content.length() : MAX_CHAR;
content = content.trim().substring(0, maxLength - 1) + "...";
}
ArrayList<String> longdate = (ArrayList<String>) result.get("DatasetCitation-ReleaseDateLong");
Date date = new Date(Long.valueOf(longdate.get(0)));
SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
String dateText = df2.format(date);
// start date
Long start = (Long) result.get("DatasetCoverage-StartTimeLong-Long");
Date startDate = new Date(start);
String startDateTxt = df2.format(startDate);
// end date
String end = (String) result.get("Dataset-DatasetCoverage-StopTimeLong");
String endDateTxt = "";
if ("".equals(end)) {
endDateTxt = "Present";
} else {
Date endDate = new Date(Long.valueOf(end));
endDateTxt = df2.format(endDate);
}
String processingLevel = (String) result.get("Dataset-ProcessingLevel");
Double proNum = getProLevelNum(processingLevel);
Double userPop = getPop(((Integer) result.get("Dataset-UserPopularity")).doubleValue());
Double allPop = getPop(((Integer) result.get("Dataset-AllTimePopularity")).doubleValue());
Double monthPop = getPop(((Integer) result.get("Dataset-MonthlyPopularity")).doubleValue());
List<String> sensors = (List<String>) result.get("DatasetSource-Sensor-ShortName");
SResult re = new SResult(shortName, longName, topic, content, dateText);
SResult.set(re, "term", relevance);
SResult.set(re, "releaseDate", Long.valueOf(longdate.get(0)).doubleValue());
SResult.set(re, "processingLevel", processingLevel);
SResult.set(re, "processingL", proNum);
SResult.set(re, "userPop", userPop);
SResult.set(re, "allPop", allPop);
SResult.set(re, "monthPop", monthPop);
SResult.set(re, "startDate", startDateTxt);
SResult.set(re, "endDate", endDateTxt);
SResult.set(re, "sensors", String.join(", ", sensors));
QueryBuilder queryLabelSearch = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("query", query)).must(QueryBuilders.termQuery("dataID", shortName));
SearchResponse labelRes = es.getClient().prepareSearch(index).setTypes("trainingranking").setQuery(queryLabelSearch).setSize(5).execute().actionGet();
String labelString = null;
for (SearchHit label : labelRes.getHits().getHits()) {
Map<String, Object> labelItem = label.getSource();
labelString = (String) labelItem.get("label");
}
SResult.set(re, "label", labelString);
resultList.add(re);
}
return resultList;
}
Aggregations