Search in sources :

Example 1 with SortOrder

use of org.graylog.shaded.elasticsearch6.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;
        }
    };
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) TransportClient(org.elasticsearch.client.transport.TransportClient) RegexpFlag(org.elasticsearch.index.query.RegexpFlag) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ArrayList(java.util.ArrayList) Logger(com.codingchili.core.logging.Logger) InetAddress(java.net.InetAddress) StorageContext(com.codingchili.core.context.StorageContext) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) FutureHelper.result(com.codingchili.core.context.FutureHelper.result) AsyncResult(io.vertx.core.AsyncResult) Validator(com.codingchili.core.security.Validator) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) SearchHit(org.elasticsearch.search.SearchHit) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) Collection(java.util.Collection) com.codingchili.core.storage.exception(com.codingchili.core.storage.exception) IOException(java.io.IOException) FutureHelper.error(com.codingchili.core.context.FutureHelper.error) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) BytesReference(org.elasticsearch.common.bytes.BytesReference) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Future(io.vertx.core.Future) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) TransportAddress(org.elasticsearch.common.transport.TransportAddress) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) RestStatus(org.elasticsearch.rest.RestStatus) SortOrder(org.elasticsearch.search.sort.SortOrder) Handler(io.vertx.core.Handler) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ActionListener(org.elasticsearch.action.ActionListener) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Handler(io.vertx.core.Handler) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with SortOrder

use of org.graylog.shaded.elasticsearch6.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;
}
Also used : ObjectValue(graphql.language.ObjectValue) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) LoggerFactory(org.slf4j.LoggerFactory) FragmentSpread(graphql.language.FragmentSpread) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) StringUtils(org.apache.commons.lang3.StringUtils) ElasticsearchWrapper(org.craftercms.search.elasticsearch.ElasticsearchWrapper) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) VariableReference(graphql.language.VariableReference) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) DataFetcher(graphql.schema.DataFetcher) LinkedList(java.util.LinkedList) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) MapUtils(org.apache.commons.collections.MapUtils) ObjectField(graphql.language.ObjectField) StopWatch(org.springframework.util.StopWatch) Field(graphql.language.Field) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Argument(graphql.language.Argument) List(java.util.List) StringValue(graphql.language.StringValue) ArrayValue(graphql.language.ArrayValue) IntValue(graphql.language.IntValue) SortOrder(org.elasticsearch.search.sort.SortOrder) Optional(java.util.Optional) FragmentDefinition(graphql.language.FragmentDefinition) Required(org.springframework.beans.factory.annotation.Required) InlineFragment(graphql.language.InlineFragment) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collections(java.util.Collections) SchemaUtils(org.craftercms.engine.graphql.SchemaUtils) BooleanValue(graphql.language.BooleanValue) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) LinkedList(java.util.LinkedList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) StopWatch(org.springframework.util.StopWatch) SearchResponse(org.elasticsearch.action.search.SearchResponse) ObjectField(graphql.language.ObjectField) Field(graphql.language.Field) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with SortOrder

use of org.graylog.shaded.elasticsearch6.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);
        }
    }
}
Also used : SortOrder(org.elasticsearch.search.sort.SortOrder) SortByScript(com.b2international.index.query.SortBy.SortByScript) SortByField(com.b2international.index.query.SortBy.SortByField) SortBy(com.b2international.index.query.SortBy) MultiSortBy(com.b2international.index.query.SortBy.MultiSortBy) SortOrder(org.elasticsearch.search.sort.SortOrder)

Example 4 with SortOrder

use of org.graylog.shaded.elasticsearch6.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));
    }
}
Also used : Query(org.apache.lucene.search.Query) Constants(org.codelibs.fess.Constants) SortBuilders(org.elasticsearch.search.sort.SortBuilders) OptionalThing(org.dbflute.optional.OptionalThing) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ScoreFunctionBuilders(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders) GeoInfo(org.codelibs.fess.entity.GeoInfo) QueryContext(org.codelibs.fess.entity.QueryContext) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Fuzziness(org.elasticsearch.common.unit.Fuzziness) Locale(java.util.Locale) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Resource(javax.annotation.Resource) Set(java.util.Set) PrefixQuery(org.apache.lucene.search.PrefixQuery) UUID(java.util.UUID) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) WildcardQuery(org.apache.lucene.search.WildcardQuery) List(java.util.List) Stream(java.util.stream.Stream) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) SortOrder(org.elasticsearch.search.sort.SortOrder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ParseException(org.apache.lucene.queryparser.classic.ParseException) HashMap(java.util.HashMap) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FacetInfo(org.codelibs.fess.entity.FacetInfo) SortBuilder(org.elasticsearch.search.sort.SortBuilder) UserMessages(org.lastaflute.core.message.UserMessages) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) StringUtil(org.codelibs.core.lang.StringUtil) BooleanClause(org.apache.lucene.search.BooleanClause) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) PrefixQuery(org.apache.lucene.search.PrefixQuery) SortOrder(org.elasticsearch.search.sort.SortOrder) Term(org.apache.lucene.index.Term) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException)

Example 5 with SortOrder

use of org.graylog.shaded.elasticsearch6.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;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SResult(org.apache.sdap.mudrod.ssearch.structure.SResult) SortOrder(org.elasticsearch.search.sort.SortOrder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) JsonObject(com.google.gson.JsonObject) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

SortOrder (org.elasticsearch.search.sort.SortOrder)9 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)4 SearchHit (org.elasticsearch.search.SearchHit)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 SortTerm (org.bedework.calfacade.filter.SortTerm)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)2 SortBy (com.b2international.index.query.SortBy)1 MultiSortBy (com.b2international.index.query.SortBy.MultiSortBy)1 SortByField (com.b2international.index.query.SortBy.SortByField)1 SortByScript (com.b2international.index.query.SortBy.SortByScript)1 FutureHelper.error (com.codingchili.core.context.FutureHelper.error)1 FutureHelper.result (com.codingchili.core.context.FutureHelper.result)1 StorageContext (com.codingchili.core.context.StorageContext)1