Search in sources :

Example 1 with FuzzyLikeThisQueryBuilder

use of org.elasticsearch.index.query.FuzzyLikeThisQueryBuilder 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)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)1 FuzzyLikeThisQueryBuilder (org.elasticsearch.index.query.FuzzyLikeThisQueryBuilder)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)1 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)1