Search in sources :

Example 1 with QueryStringQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryStringQueryBuilder in project elasticsearch by elastic.

the class RestActions method urlParamsToQueryBuilder.

public static QueryBuilder urlParamsToQueryBuilder(RestRequest request) {
    String queryString = request.param("q");
    if (queryString == null) {
        return null;
    }
    QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString);
    queryBuilder.defaultField(request.param("df"));
    queryBuilder.analyzer(request.param("analyzer"));
    queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false));
    queryBuilder.lenient(request.paramAsBoolean("lenient", null));
    String defaultOperator = request.param("default_operator");
    if (defaultOperator != null) {
        queryBuilder.defaultOperator(Operator.fromString(defaultOperator));
    }
    return queryBuilder;
}
Also used : QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder)

Example 2 with QueryStringQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryStringQueryBuilder in project graylog2-server by Graylog2.

the class Searches method fieldHistogram.

public HistogramResult fieldHistogram(String query, String field, DateHistogramInterval interval, String filter, TimeRange range, boolean includeCardinality) throws FieldTypeException {
    final DateHistogramBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram(AGG_HISTOGRAM).field("timestamp").subAggregation(AggregationBuilders.stats(AGG_STATS).field(field)).interval(interval.toESInterval());
    if (includeCardinality) {
        dateHistogramBuilder.subAggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));
    }
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(dateHistogramBuilder).filter(standardAggregationFilters(range, filter));
    QueryStringQueryBuilder qs = queryStringQuery(query);
    qs.allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
    SearchRequestBuilder srb = c.prepareSearch();
    final Set<String> affectedIndices = determineAffectedIndices(range, filter);
    srb.setIndices(affectedIndices.toArray(new String[affectedIndices.size()]));
    srb.setQuery(qs);
    srb.addAggregation(builder);
    SearchResponse r;
    final SearchRequest request = srb.request();
    try {
        r = c.search(request).actionGet();
    } catch (org.elasticsearch.action.search.SearchPhaseExecutionException e) {
        throw new FieldTypeException(e);
    }
    checkForFailedShards(r);
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new FieldHistogramResult(f.getAggregations().get(AGG_HISTOGRAM), query, request.source(), interval, r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) FieldHistogramResult(org.graylog2.indexer.results.FieldHistogramResult) DateHistogramBuilder(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder)

Example 3 with QueryStringQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryStringQueryBuilder in project opencast by opencast.

the class AbstractElasticsearchQueryBuilder method createQuery.

/**
 * Create the actual query. We start with a query that matches everything, then move to the boolean conditions,
 * finally add filter queries.
 */
private void createQuery() {
    queryBuilder = new MatchAllQueryBuilder();
    // The boolean query builder
    BoolQueryBuilder booleanQuery = new BoolQueryBuilder();
    // Terms
    if (searchTerms != null) {
        for (Map.Entry<String, Set<Object>> entry : searchTerms.entrySet()) {
            Set<Object> values = entry.getValue();
            if (values.size() == 1)
                booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
            else
                booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
        }
        this.queryBuilder = booleanQuery;
    }
    // Negative terms
    if (negativeSearchTerms != null) {
        for (Map.Entry<String, Set<Object>> entry : negativeSearchTerms.entrySet()) {
            Set<Object> values = entry.getValue();
            if (values.size() == 1)
                booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
            else
                booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
        }
        this.queryBuilder = booleanQuery;
    }
    // Date ranges
    if (dateRanges != null) {
        for (DateRange dr : dateRanges) {
            booleanQuery.must(dr.getQueryBuilder());
        }
        this.queryBuilder = booleanQuery;
    }
    // Text
    if (text != null) {
        QueryStringQueryBuilder queryBuilder = QueryBuilders.queryString(text).field(TEXT);
        booleanQuery.must(queryBuilder);
        this.queryBuilder = booleanQuery;
    }
    // Fuzzy text
    if (fuzzyText != null) {
        FuzzyLikeThisQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyLikeThisQuery(TEXT_FUZZY).likeText(fuzzyText);
        booleanQuery.must(fuzzyQueryBuilder);
        this.queryBuilder = booleanQuery;
    }
    QueryBuilder unfilteredQuery = queryBuilder;
    List<FilterBuilder> filters = new ArrayList<FilterBuilder>();
    // Add filtering for AND terms
    if (groups != null) {
        for (ValueGroup group : groups) {
            filters.addAll(group.getFilterBuilders());
        }
    }
    // Non-Empty fields
    if (nonEmptyFields != null) {
        for (String field : nonEmptyFields) {
            filters.add(FilterBuilders.existsFilter(field));
        }
    }
    // Empty fields
    if (emptyFields != null) {
        for (String field : emptyFields) {
            filters.add(FilterBuilders.missingFilter(field));
        }
    }
    // Filter expressions
    if (filter != null) {
        filters.add(FilterBuilders.termFilter(IndexSchema.TEXT, filter));
    }
    // Apply the filters
    if (filters.size() == 1) {
        this.queryBuilder = QueryBuilders.filteredQuery(unfilteredQuery, filters.get(0));
    } else if (filters.size() > 1) {
        FilterBuilder andFilter = FilterBuilders.andFilter(filters.toArray(new FilterBuilder[filters.size()]));
        this.queryBuilder = QueryBuilders.filteredQuery(unfilteredQuery, andFilter);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) FuzzyLikeThisQueryBuilder(org.elasticsearch.index.query.FuzzyLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) HashMap(java.util.HashMap) Map(java.util.Map) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) FuzzyLikeThisQueryBuilder(org.elasticsearch.index.query.FuzzyLikeThisQueryBuilder)

Example 4 with QueryStringQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryStringQueryBuilder in project Anserini by castorini.

the class SearchElastic method searchTweets.

public <K> ScoredDocuments searchTweets(String queryString, long t) {
    SearchHits results = null;
    String specials = "+-=&|><!(){}[]^\"~*?:\\/";
    for (int i = 0; i < specials.length(); i++) {
        char c = specials.charAt(i);
        queryString = queryString.replace(String.valueOf(c), " ");
    }
    // Do not consider the tweets with tweet ids that are beyond the queryTweetTime
    // <querytweettime> tag contains the timestamp of the query in terms of the
    // chronologically nearest tweet id within the corpus
    RangeQueryBuilder queryTweetTime = QueryBuilders.rangeQuery(TweetGenerator.TweetField.ID_LONG.name).from(0L).to(t);
    QueryStringQueryBuilder queryTerms = QueryBuilders.queryStringQuery(queryString).defaultField("contents").analyzer("english");
    BoolQueryBuilder query = QueryBuilders.boolQuery().filter(queryTweetTime).should(queryTerms);
    SearchRequest searchRequest = new SearchRequest(args.esIndex);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(query);
    sourceBuilder.size(args.hits);
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    sourceBuilder.sort(new FieldSortBuilder(TweetGenerator.TweetField.ID_LONG.name).order(SortOrder.DESC));
    searchRequest.source(sourceBuilder);
    try {
        SearchResponse searchResponse = client.search(searchRequest, COMMON_OPTIONS);
        results = searchResponse.getHits();
    } catch (Exception e) {
        LOG.error("Exception during ES query: ", e);
    }
    ScoreTiesAdjusterReranker reranker = new ScoreTiesAdjusterReranker();
    return reranker.rerank(ScoredDocuments.fromESDocs(results), null);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ScoreSortBuilder(org.elasticsearch.search.sort.ScoreSortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker) SearchHits(org.elasticsearch.search.SearchHits) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder)

Example 5 with QueryStringQueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryStringQueryBuilder in project graylog2-server by Graylog2.

the class ESMessageList method applyHighlightingIfActivated.

private void applyHighlightingIfActivated(SearchSourceBuilder searchSourceBuilder, SearchJob job, Query query) {
    if (!allowHighlighting) {
        return;
    }
    final QueryStringQueryBuilder highlightQuery = decoratedHighlightQuery(job, query);
    searchSourceBuilder.highlighter(new HighlightBuilder().requireFieldMatch(false).highlightQuery(highlightQuery).field("*").fragmentSize(0).numOfFragments(0));
}
Also used : QueryStringQueryBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryStringQueryBuilder) HighlightBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Aggregations

QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)8 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 IOException (java.io.IOException)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 SearchHits (org.elasticsearch.search.SearchHits)3 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)3 ScoreTiesAdjusterReranker (io.anserini.rerank.lib.ScoreTiesAdjusterReranker)2 ArrayList (java.util.ArrayList)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)2 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)2 ScoreSortBuilder (org.elasticsearch.search.sort.ScoreSortBuilder)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)1 Repository (com.atlassian.stash.repository.Repository)1