Package org.apache.lucene.queries

Examples of org.apache.lucene.queries.FilterClause


        Element clauseElem = (Element) node;
        BooleanClause.Occur occurs = BooleanQueryBuilder.getOccursValue(clauseElem);

        Element clauseFilter = DOMUtils.getFirstChildOrFail(clauseElem);
        Filter f = factory.getFilter(clauseFilter);
        bf.add(new FilterClause(f, occurs));
      }
    }

    return bf;
  }
View Full Code Here


        FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
        XBooleanFilter booleanFilter = (XBooleanFilter) filteredQuery.getFilter();

        Iterator<FilterClause> iterator = booleanFilter.iterator();
        assertThat(iterator.hasNext(), equalTo(true));
        FilterClause clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay1")));

        assertThat(iterator.hasNext(), equalTo(true));
        clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay4")));

        assertThat(iterator.hasNext(), equalTo(true));
        clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay2")));

        assertThat(iterator.hasNext(), equalTo(true));
        clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay3")));

        assertThat(iterator.hasNext(), equalTo(false));
    }
View Full Code Here

        FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
        XBooleanFilter booleanFilter = (XBooleanFilter) filteredQuery.getFilter();

        Iterator<FilterClause> iterator = booleanFilter.iterator();
        assertThat(iterator.hasNext(), equalTo(true));
        FilterClause clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay1")));

        assertThat(iterator.hasNext(), equalTo(true));
        clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay4")));

        assertThat(iterator.hasNext(), equalTo(true));
        clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay2")));

        assertThat(iterator.hasNext(), equalTo(true));
        clause = iterator.next();
        assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
        assertThat(((TermFilter) clause.getFilter()).getTerm(), equalTo(new Term("name.first", "shay3")));

        assertThat(iterator.hasNext(), equalTo(false));
    }
View Full Code Here

            // Current bool filter requires that at least one should clause matches, even with a must clause.
            XBooleanFilter bool = new XBooleanFilter();
            for (String type : types) {
                DocumentMapper docMapper = documentMapper(type);
                if (docMapper == null) {
                    bool.add(new FilterClause(new TermFilter(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.SHOULD));
                } else {
                    bool.add(new FilterClause(docMapper.typeFilter(), BooleanClause.Occur.SHOULD));
                }
            }
            if (filterPercolateType) {
                bool.add(excludePercolatorType, BooleanClause.Occur.MUST);
            }
View Full Code Here

            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("must".equals(currentFieldName)) {
                    hasAnyFilter = true;
                    Filter filter = parseContext.parseInnerFilter();
                    if (filter != null) {
                        boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));
                    }
                } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
                    hasAnyFilter = true;
                    Filter filter = parseContext.parseInnerFilter();
                    if (filter != null) {
                        boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT));
                    }
                } else if ("should".equals(currentFieldName)) {
                    hasAnyFilter = true;
                    Filter filter = parseContext.parseInnerFilter();
                    if (filter != null) {
                        boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD));
                    }
                } else {
                    throw new QueryParsingException(parseContext.index(), "[bool] filter does not support [" + currentFieldName + "]");
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                if ("must".equals(currentFieldName)) {
                    hasAnyFilter = true;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        Filter filter = parseContext.parseInnerFilter();
                        if (filter != null) {
                            boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));
                        }
                    }
                } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
                    hasAnyFilter = true;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        Filter filter = parseContext.parseInnerFilter();
                        if (filter != null) {
                            boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT));
                        }
                    }
                } else if ("should".equals(currentFieldName)) {
                    hasAnyFilter = true;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        Filter filter = parseContext.parseInnerFilter();
                        if (filter != null) {
                            boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD));
                        }
                    }
                } else {
                    throw new QueryParsingException(parseContext.index(), "[bool] filter does not support [" + currentFieldName + "]");
                }
View Full Code Here

                if (indexAlias == null) {
                    // This shouldn't happen unless alias disappeared after filteringAliases was called.
                    throw new InvalidAliasNameException(index, aliases[0], "Unknown alias name was passed to alias Filter");
                }
                if (indexAlias.parsedFilter() != null) {
                    combined.add(new FilterClause(indexAlias.parsedFilter(), BooleanClause.Occur.SHOULD));
                } else {
                    // The filter might be null only if filter was removed after filteringAliases was called
                    return null;
                }
            }
View Full Code Here

            return null;
        }

        // optimize single case...
        if (clauses.size() == 1) {
            FilterClause clause = clauses.get(0);
            DocIdSet set = clause.getFilter().getDocIdSet(context, acceptDocs);
            if (clause.getOccur() == Occur.MUST_NOT) {
                if (DocIdSets.isEmpty(set)) {
                    return new AllDocIdSet(maxDoc);
                } else {
                    return new NotDocIdSet(set, maxDoc);
                }
            }
            // SHOULD or MUST, just return the set...
            if (DocIdSets.isEmpty(set)) {
                return null;
            }
            return set;
        }

        // We have several clauses, try to organize things to make it easier to process
        List<DocIdSetIterator> shouldIterators = new ArrayList<>();
        List<Bits> shouldBits = new ArrayList<>();
        boolean hasShouldClauses = false;

        List<DocIdSetIterator> requiredIterators = new ArrayList<>();
        List<DocIdSetIterator> excludedIterators = new ArrayList<>();

        List<Bits> requiredBits = new ArrayList<>();
        List<Bits> excludedBits = new ArrayList<>();

        for (FilterClause clause : clauses) {
            DocIdSet set = clause.getFilter().getDocIdSet(context, null);
            DocIdSetIterator it = null;
            Bits bits = null;
            if (DocIdSets.isEmpty(set) == false) {
                it = set.iterator();
                if (it != null) {
                    bits = set.bits();
                }
            }

            switch (clause.getOccur()) {
            case SHOULD:
                hasShouldClauses = true;
                if (it == null) {
                    // continue, but we recorded that there is at least one should clause
                    // so that if all iterators are null we know that nothing matches this
View Full Code Here

    public void add(FilterClause filterClause) {
        clauses.add(filterClause);
    }

    public final void add(Filter filter, Occur occur) {
        add(new FilterClause(filter, occur));
    }
View Full Code Here

    @Test
    public void testToStringOfWrappedBooleanFilters() {
        XBooleanFilter orFilter = new XBooleanFilter();

        XBooleanFilter stockFilter = new XBooleanFilter();
        stockFilter.add(new FilterClause(getTermsFilter("inStock", "Y"), BooleanClause.Occur.MUST));
        stockFilter.add(new FilterClause(getTermsFilter("barCode", "12345678"), BooleanClause.Occur.MUST));

        orFilter.add(new FilterClause(stockFilter, BooleanClause.Occur.SHOULD));

        XBooleanFilter productPropertyFilter = new XBooleanFilter();
        productPropertyFilter.add(new FilterClause(getTermsFilter("isHeavy", "N"), BooleanClause.Occur.MUST));
        productPropertyFilter.add(new FilterClause(getTermsFilter("isDamaged", "Y"), BooleanClause.Occur.MUST));

        orFilter.add(new FilterClause(productPropertyFilter, BooleanClause.Occur.SHOULD));

        XBooleanFilter composedFilter = new XBooleanFilter();
        composedFilter.add(new FilterClause(orFilter, BooleanClause.Occur.MUST));

        assertThat(
                "BooleanFilter(+BooleanFilter(BooleanFilter(+inStock:Y +barCode:12345678) BooleanFilter(+isHeavy:N +isDamaged:Y)))",
                equalTo(composedFilter.toString())
        );
View Full Code Here

            filter = new PrettyPrintFieldCacheTermsFilter(String.valueOf(field), String.valueOf(character));
        } else {
            Term term = new Term(String.valueOf(field), String.valueOf(character));
            filter = new TermFilter(term);
        }
        return new FilterClause(filter, occur);
    }
View Full Code Here

TOP

Related Classes of org.apache.lucene.queries.FilterClause

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.