Search in sources :

Example 6 with MatchAllQueryBuilder

use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.

the class FunctionScoreIT method testMinScoreFunctionScoreManyDocsAndRandomMinScore.

public void testMinScoreFunctionScoreManyDocsAndRandomMinScore() throws IOException, ExecutionException, InterruptedException {
    List<IndexRequestBuilder> docs = new ArrayList<>();
    int numDocs = randomIntBetween(1, 100);
    int scoreOffset = randomIntBetween(-2 * numDocs, 2 * numDocs);
    int minScore = randomIntBetween(-2 * numDocs, 2 * numDocs);
    for (int i = 0; i < numDocs; i++) {
        docs.add(client().prepareIndex(INDEX, TYPE, Integer.toString(i)).setSource("num", i + scoreOffset));
    }
    indexRandom(true, docs);
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "return (doc['num'].value)", Collections.emptyMap());
    int numMatchingDocs = numDocs + scoreOffset - minScore;
    if (numMatchingDocs < 0) {
        numMatchingDocs = 0;
    }
    if (numMatchingDocs > numDocs) {
        numMatchingDocs = numDocs;
    }
    SearchResponse searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)).size(numDocs))).actionGet();
    assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
    searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(scriptFunction(script)), new FilterFunctionBuilder(scriptFunction(script)) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)).size(numDocs))).actionGet();
    assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 7 with MatchAllQueryBuilder

use of org.elasticsearch.index.query.MatchAllQueryBuilder 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 8 with MatchAllQueryBuilder

use of org.elasticsearch.index.query.MatchAllQueryBuilder in project yacy_grid_mcp by yacy.

the class YaCyQuery method simpleQueryBuilder.

public static QueryBuilder simpleQueryBuilder(String q, boolean or, Boosts boosts) {
    if (q.equals("yacyall"))
        return new MatchAllQueryBuilder();
    final MultiMatchQueryBuilder qb = QueryBuilders.multiMatchQuery(q).operator(or ? Operator.OR : Operator.AND).zeroTermsQuery(ZeroTermsQuery.ALL);
    boosts.forEach((mapping, boost) -> qb.field(mapping.getMapping().name(), boost));
    return qb;
}
Also used : MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 9 with MatchAllQueryBuilder

use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.

the class FunctionScoreQueryBuilder method fromXContent.

public static FunctionScoreQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    QueryBuilder query = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    FiltersFunctionScoreQuery.ScoreMode scoreMode = FunctionScoreQueryBuilder.DEFAULT_SCORE_MODE;
    float maxBoost = FunctionScoreQuery.DEFAULT_MAX_BOOST;
    Float minScore = null;
    String currentFieldName = null;
    XContentParser.Token token;
    CombineFunction combineFunction = null;
    // Either define array of functions and filters or only one function
    boolean functionArrayFound = false;
    boolean singleFunctionFound = false;
    String singleFunctionName = null;
    List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName)) {
                if (query != null) {
                    throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. [query] is already defined.", NAME);
                }
                query = parseContext.parseInnerQueryBuilder();
            } else {
                if (singleFunctionFound) {
                    throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. already found function [{}], now encountering [{}]. use [functions] " + "array if you want to define several functions.", NAME, singleFunctionName, currentFieldName);
                }
                if (functionArrayFound) {
                    String errorString = "already found [functions] array, now encountering [" + currentFieldName + "].";
                    handleMisplacedFunctionsDeclaration(parser.getTokenLocation(), errorString);
                }
                singleFunctionFound = true;
                singleFunctionName = currentFieldName;
                ScoreFunctionBuilder<?> scoreFunction = parser.namedObject(ScoreFunctionBuilder.class, currentFieldName, parseContext);
                filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(scoreFunction));
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (FUNCTIONS_FIELD.match(currentFieldName)) {
                if (singleFunctionFound) {
                    String errorString = "already found [" + singleFunctionName + "], now encountering [functions].";
                    handleMisplacedFunctionsDeclaration(parser.getTokenLocation(), errorString);
                }
                functionArrayFound = true;
                currentFieldName = parseFiltersAndFunctions(parseContext, filterFunctionBuilders);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. array [{}] is not supported", NAME, currentFieldName);
            }
        } else if (token.isValue()) {
            if (SCORE_MODE_FIELD.match(currentFieldName)) {
                scoreMode = FiltersFunctionScoreQuery.ScoreMode.fromString(parser.text());
            } else if (BOOST_MODE_FIELD.match(currentFieldName)) {
                combineFunction = CombineFunction.fromString(parser.text());
            } else if (MAX_BOOST_FIELD.match(currentFieldName)) {
                maxBoost = parser.floatValue();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (MIN_SCORE_FIELD.match(currentFieldName)) {
                minScore = parser.floatValue();
            } else {
                if (singleFunctionFound) {
                    throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. already found function [{}], now encountering [{}]. use [functions] array " + "if you want to define several functions.", NAME, singleFunctionName, currentFieldName);
                }
                if (functionArrayFound) {
                    String errorString = "already found [functions] array, now encountering [" + currentFieldName + "].";
                    handleMisplacedFunctionsDeclaration(parser.getTokenLocation(), errorString);
                }
                if (WEIGHT_FIELD.match(currentFieldName)) {
                    filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(new WeightBuilder().setWeight(parser.floatValue())));
                    singleFunctionFound = true;
                    singleFunctionName = currentFieldName;
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. field [{}] is not supported", NAME, currentFieldName);
                }
            }
        }
    }
    if (query == null) {
        query = new MatchAllQueryBuilder();
    }
    FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(query, filterFunctionBuilders.toArray(new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()]));
    if (combineFunction != null) {
        functionScoreQueryBuilder.boostMode(combineFunction);
    }
    functionScoreQueryBuilder.scoreMode(scoreMode);
    functionScoreQueryBuilder.maxBoost(maxBoost);
    if (minScore != null) {
        functionScoreQueryBuilder.setMinScore(minScore);
    }
    functionScoreQueryBuilder.boost(boost);
    functionScoreQueryBuilder.queryName(queryName);
    return functionScoreQueryBuilder;
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 10 with MatchAllQueryBuilder

use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.

the class GeoDistanceSortBuilderTests method randomGeoDistanceSortBuilder.

public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() {
    String fieldName = randomAsciiOfLengthBetween(1, 10);
    GeoDistanceSortBuilder result = null;
    int id = randomIntBetween(0, 2);
    switch(id) {
        case 0:
            int count = randomIntBetween(1, 10);
            String[] geohashes = new String[count];
            for (int i = 0; i < count; i++) {
                geohashes[i] = RandomGeoGenerator.randomPoint(random()).geohash();
            }
            result = new GeoDistanceSortBuilder(fieldName, geohashes);
            break;
        case 1:
            GeoPoint pt = RandomGeoGenerator.randomPoint(random());
            result = new GeoDistanceSortBuilder(fieldName, pt.getLat(), pt.getLon());
            break;
        case 2:
            result = new GeoDistanceSortBuilder(fieldName, points(new GeoPoint[0]));
            break;
        default:
            throw new IllegalStateException("one of three geo initialisation strategies must be used");
    }
    if (randomBoolean()) {
        result.geoDistance(geoDistance(result.geoDistance()));
    }
    if (randomBoolean()) {
        result.unit(randomValueOtherThan(result.unit(), () -> randomFrom(DistanceUnit.values())));
    }
    if (randomBoolean()) {
        result.order(randomFrom(SortOrder.values()));
    }
    if (randomBoolean()) {
        result.sortMode(randomValueOtherThan(SortMode.SUM, () -> randomFrom(SortMode.values())));
    }
    if (randomBoolean()) {
        result.setNestedFilter(new MatchAllQueryBuilder());
    }
    if (randomBoolean()) {
        result.setNestedPath(randomValueOtherThan(result.getNestedPath(), () -> randomAsciiOfLengthBetween(1, 10)));
    }
    if (randomBoolean()) {
        result.validation(randomValueOtherThan(result.validation(), () -> randomFrom(GeoValidationMethod.values())));
    }
    return result;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Aggregations

MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)7 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)6 ArrayList (java.util.ArrayList)4 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)4 Script (org.elasticsearch.script.Script)4 HashMap (java.util.HashMap)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Map (java.util.Map)2 ParsingException (org.elasticsearch.common.ParsingException)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)2 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)2 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)2 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)2 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)2 FilterFunctionBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder)2 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)2