Search in sources :

Example 76 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project api-core by ca-cwds.

the class ElasticsearchDao method buildBoolQueryFromSearchTerms.

/**
 * Builds an Elasticsearch compound query by combining multiple <b>should</b> clauses into a
 * Boolean Query.
 *
 * @param searchTerm the user entered values to search for separated by space
 * @return the Elasticsearch compound query
 */
public BoolQueryBuilder buildBoolQueryFromSearchTerms(String searchTerm) {
    final String s = searchTerm.trim().toLowerCase();
    final String[] searchTerms = s.split("\\s+");
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    for (String term : searchTerms) {
        term = term.trim();
        if (StringUtils.isNotBlank(term)) {
            queryBuilder = ElasticSearchQuery.getQueryFromTerm(term).buildQuery(queryBuilder, term);
        }
    }
    return queryBuilder;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 77 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project api-core by ca-cwds.

the class ElasticsearchDao method searchPerson.

/**
 * The Intake Auto-complete for Person takes a single search term, used to query Elasticsearch
 * Person documents on specific fields.
 *
 * <p>
 * For example, search strings consisting of only digits could be phone numbers, social security
 * numbers, or street address numbers. Search strings consisting of only letters could be last
 * name, first name, city, state, language, and so forth.
 * </p>
 *
 * <p>
 * This method calls Elasticsearch's <a href=
 * "https://www.elastic.co/guide/en/elasticsearch/guide/current/_best_fields.html#dis-max-query" >
 * "dis max"</a> query feature.
 * </p>
 *
 * @param searchTerm ES search String
 * @param alias index alias
 * @param docType document type
 * @return array of ElasticSearchPerson
 * @throws ApiElasticSearchException unable to connect, disconnect, bad hair day, etc.
 * @deprecated obsolete API, scheduled for removal
 */
@Deprecated
public ElasticSearchPerson[] searchPerson(final String searchTerm, final String alias, final String docType) throws ApiElasticSearchException {
    checkArgument(!Strings.isNullOrEmpty(searchTerm), "searchTerm cannot be Null or empty");
    final BoolQueryBuilder queryBuilder = buildBoolQueryFromSearchTerms(searchTerm);
    if (!queryBuilder.hasClauses()) {
        return new ElasticSearchPerson[0];
    }
    final SearchRequestBuilder builder = client.prepareSearch(alias).setTypes(docType).setQuery(queryBuilder).setFrom(0).setSize(DEFAULT_MAX_RESULTS).setExplain(true);
    LOGGER.warn("ES QUERY: {}", builder);
    final SearchHit[] hits = builder.execute().actionGet().getHits().getHits();
    final ElasticSearchPerson[] ret = new ElasticSearchPerson[hits.length];
    int counter = -1;
    for (SearchHit hit : hits) {
        ret[++counter] = ElasticSearchPerson.makeESPerson(hit);
    }
    return ret;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 78 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project incubator-sdap-mudrod by apache.

the class Dispatcher method createSemQuery.

/**
 * Method of creating semantic query based on Threshold
 *
 * @param input          regular query
 * @param T              threshold raning from 0 to 1
 * @param query_operator query mode
 * @return a multiMatch query builder
 */
public BoolQueryBuilder createSemQuery(String input, double T, String query_operator) {
    Map<String, Double> selected_Map = getRelatedTermsByT(input, T);
    selected_Map.put(input, (double) 1);
    String[] fieldsList = { "Dataset-Metadata", "Dataset-ShortName", "Dataset-LongName", "DatasetParameter-Topic", "DatasetParameter-VariableDetail", "DatasetParameter-Category", "DatasetParameter-Variable", "DatasetParameter-Term", "DatasetSource-Source-LongName", "DatasetSource-Source-LongName-Full", "DatasetSource-Source-ShortName", "DatasetSource-Source-ShortName-Full", "DatasetSource-Sensor-LongName", "DatasetSource-Sensor-LongName-Full", "DatasetSource-Sensor-ShortName", "DatasetSource-Sensor-ShortName-Full" };
    BoolQueryBuilder qb = new BoolQueryBuilder();
    for (Entry<String, Double> entry : selected_Map.entrySet()) {
        if (query_operator.toLowerCase().trim().equals("phrase")) {
            // when
            qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).type(MultiMatchQueryBuilder.Type.PHRASE).tieBreaker((float) 0.5));
        // set
        // to
        // 1.0,
        // it
        // would
        // be
        // equal
        // to
        // "most
        // fields"
        // query
        } else if (query_operator.toLowerCase().trim().equals("and")) {
            qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).operator(MatchQueryBuilder.DEFAULT_OPERATOR.AND).tieBreaker((float) 0.5));
        } else {
            qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).operator(MatchQueryBuilder.DEFAULT_OPERATOR.OR).tieBreaker((float) 0.5));
        }
    }
    // LOG.info(qb.toString());
    return qb;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 79 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project alien4cloud by alien4cloud.

the class ToscaTypeSearchService method getDependencyQuery.

/**
 * Build an elasticsearch query to get data tosca elements based on a set of dependencies.
 *
 * @param dependencies The set of dependencies.
 * @param keyValueFilters List of key1, value1, key2, value2 to add term filters to the query for each dependency.
 * @return
 */
private BoolQueryBuilder getDependencyQuery(Set<CSARDependency> dependencies, String... keyValueFilters) {
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (CSARDependency dependency : dependencies) {
        BoolQueryBuilder dependencyQuery = QueryBuilders.boolQuery();
        dependencyQuery.must(QueryBuilders.termQuery("archiveName", dependency.getName())).must(QueryBuilders.termQuery("archiveVersion", dependency.getVersion()));
        if (keyValueFilters != null) {
            for (int i = 0; i < keyValueFilters.length; i += 2) {
                dependencyQuery.must(QueryBuilders.termQuery(keyValueFilters[i], keyValueFilters[i + 1]));
            }
        }
        boolQueryBuilder.should(dependencyQuery);
    }
    return boolQueryBuilder;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 80 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project alien4cloud by alien4cloud.

the class SuggestionController method nodeTypeSuggest.

@ApiIgnore
@RequestMapping(value = "/nodetypes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public RestResponse<String[]> nodeTypeSuggest(@RequestParam("text") String searchText, @RequestParam(value = "isAbstract", required = false) Boolean isAbstract) {
    if (searchText == null || searchText.trim().isEmpty()) {
        return RestResponseBuilder.<String[]>builder().data(new String[0]).build();
    }
    searchText = StringUtils.lowerCase(searchText);
    QueryBuilder queryOnText = QueryBuilders.regexpQuery("elementId", ".*?" + searchText + ".*");
    // FIXME the way of getting the highest version of a component has changed
    // QueryBuilder queryOnHighest = QueryBuilders.termQuery("highestVersion", true);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(queryOnText);
    QueryBuilder query = boolQueryBuilder;
    if (isAbstract != null) {
        query = boolQueryBuilder.must(QueryBuilders.termQuery("abstract", isAbstract));
    }
    return RestResponseBuilder.<String[]>builder().data(dao.selectPath(dao.getIndexForType(NodeType.class), new String[] { MappingBuilder.indexTypeFromClass(NodeType.class) }, query, SortOrder.ASC, "elementId", 0, 10)).build();
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) NodeType(org.alien4cloud.tosca.model.types.NodeType) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiIgnore(springfox.documentation.annotations.ApiIgnore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)201 SearchResponse (org.elasticsearch.action.search.SearchResponse)73 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)58 SearchHit (org.elasticsearch.search.SearchHit)41 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)38 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)30 Map (java.util.Map)29 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 List (java.util.List)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)21 Test (org.junit.Test)18 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)16 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 LinkedList (java.util.LinkedList)11 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)11