Java Code Examples for org.apache.lucene.search.BooleanClause#Occur
The following examples show how to use
org.apache.lucene.search.BooleanClause#Occur .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BooleanQueryExpression.java From incubator-atlas with Apache License 2.0 | 6 votes |
private BooleanClause.Occur resolveClauseOccur(BooleanClause clause) { BooleanClause.Occur occur = clause.getOccur(); if (negate) { switch (occur) { case SHOULD: occur = BooleanClause.Occur.MUST_NOT; break; case MUST: occur = BooleanClause.Occur.SHOULD; break; case MUST_NOT: occur = BooleanClause.Occur.SHOULD; break; } } return occur; }
Example 2
Source File: TestMultiFieldQPHelper.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testStaticMethod2Old() throws QueryNodeException { String[] fields = { "b", "t" }; BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT }; StandardQueryParser parser = new StandardQueryParser(); parser.setMultiFields(fields); parser.setAnalyzer(new MockAnalyzer(random())); Query q = QueryParserUtil.parse("one", fields, flags, new MockAnalyzer(random()));// , fields, flags, new // MockAnalyzer()); assertEquals("+b:one -t:one", q.toString()); q = QueryParserUtil.parse("one two", fields, flags, new MockAnalyzer(random())); assertEquals("+(b:one b:two) -(t:one t:two)", q.toString()); // expected exception, array length differs expectThrows(IllegalArgumentException.class, () -> { BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST }; QueryParserUtil.parse("blah", fields, flags2, new MockAnalyzer(random())); }); }
Example 3
Source File: TestMultiFieldQPHelper.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testStaticMethod3Old() throws QueryNodeException { String[] queries = { "one", "two" }; String[] fields = { "b", "t" }; BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT }; Query q = QueryParserUtil.parse(queries, fields, flags, new MockAnalyzer(random())); assertEquals("+b:one -t:two", q.toString()); // expected exception, array length differs expectThrows(IllegalArgumentException.class, () -> { BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST }; QueryParserUtil .parse(queries, fields, flags2, new MockAnalyzer(random())); }); }
Example 4
Source File: BooleanQueryExpression.java From incubator-atlas with Apache License 2.0 | 6 votes |
private Collection<Pipe> processOrClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) { Collection<BooleanClause> shouldClauses = groupedClauses.get(BooleanClause.Occur.SHOULD); Collection<Pipe> orPipes = new ArrayList<>(); if (shouldClauses != null) { for (BooleanClause shouldClause : shouldClauses) { QueryExpression queryExpression = queryFactory.create(shouldClause.getQuery(), resourceDefinition); // don't negate expression if we negated MUST_NOT -> SHOULD if (negate && shouldClause.getOccur() != BooleanClause.Occur.MUST_NOT) { queryExpression.setNegate(); } properties.addAll(queryExpression.getProperties()); orPipes.add(queryExpression.asPipe()); } } return orPipes; }
Example 5
Source File: BooleanQueryExpression.java From incubator-atlas with Apache License 2.0 | 5 votes |
private Collection<Pipe> processAndClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) { Collection<BooleanClause> andClauses = groupedClauses.get(BooleanClause.Occur.MUST); Collection<Pipe> andPipes = new ArrayList<>(); if (andClauses != null) { for (BooleanClause andClause : andClauses) { QueryExpression queryExpression = queryFactory.create(andClause.getQuery(), resourceDefinition); properties.addAll(queryExpression.getProperties()); andPipes.add(queryExpression.asPipe()); } } return andPipes; }
Example 6
Source File: SrndBooleanQuery.java From lucene-solr with Apache License 2.0 | 5 votes |
public static void addQueriesToBoolean( BooleanQuery.Builder bq, List<Query> queries, BooleanClause.Occur occur) { for (int i = 0; i < queries.size(); i++) { bq.add( queries.get(i), occur); } }
Example 7
Source File: SimpleQueryParser.java From lucene-solr with Apache License 2.0 | 5 votes |
private static BooleanQuery addClause(BooleanQuery bq, Query query, BooleanClause.Occur occur) { BooleanQuery.Builder newBq = new BooleanQuery.Builder(); newBq.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch()); for (BooleanClause clause : bq) { newBq.add(clause); } newBq.add(query, occur); return newBq.build(); }
Example 8
Source File: BBoxStrategy.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Makes a boolean query based upon a collection of queries and a logical operator. * * @param occur the logical operator * @param queries the query collection * @return the query */ BooleanQuery makeQuery(BooleanClause.Occur occur, Query... queries) { BooleanQuery.Builder bq = new BooleanQuery.Builder(); for (Query query : queries) { if (query != null) bq.add(query, occur); } return bq.build(); }
Example 9
Source File: TestMultiFieldQueryParser.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testStaticMethod2() throws ParseException { String[] fields = {"b", "t"}; BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT}; Query q = MultiFieldQueryParser.parse("one", fields, flags, new MockAnalyzer(random())); assertEquals("+b:one -t:one", q.toString()); q = MultiFieldQueryParser.parse("one two", fields, flags, new MockAnalyzer(random())); assertEquals("+(b:one b:two) -(t:one t:two)", q.toString()); // expected exception, array length differs expectThrows(IllegalArgumentException.class, () -> { BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST}; MultiFieldQueryParser.parse("blah", fields, flags2, new MockAnalyzer(random())); }); }
Example 10
Source File: BooleanQueryExpression.java From incubator-atlas with Apache License 2.0 | 5 votes |
private Map<BooleanClause.Occur, Collection<BooleanClause>> groupClauses() { Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses = new HashMap<>(); for (BooleanClause clause : clauses) { BooleanClause.Occur occur = resolveClauseOccur(clause); Collection<BooleanClause> clauseGrouping = groupedClauses.get(occur); if (clauseGrouping == null) { clauseGrouping = new ArrayList<>(); groupedClauses.put(occur, clauseGrouping); } clauseGrouping.add(clause); } return groupedClauses; }
Example 11
Source File: NGramQuery.java From spacewalk with GNU General Public License v2.0 | 5 votes |
/** * * @param pq PhraseQuery to break up and convert to NGramQuery * Forms a BooleanQuery with each term in the original PhraseQuery OR'd. * Note: Assumes that each term has already been tokenized into a ngram, * this method will not re-tokenize terms. * @param useMust controls if BooleanClause.Occur SHOULD or MUST is used. */ public NGramQuery(PhraseQuery pq, boolean useMust) { Term[] terms = pq.getTerms(); for (int i = 0; i < terms.length; i++) { BooleanClause.Occur occur = BooleanClause.Occur.SHOULD; if (useMust) { occur = BooleanClause.Occur.MUST; } add(new TermQuery(terms[i]), occur); } }
Example 12
Source File: PointVectorStrategy.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Constructs a query to retrieve documents that fully contain the input envelope. */ private Query makeWithin(Rectangle bbox) { BooleanQuery.Builder bq = new BooleanQuery.Builder(); BooleanClause.Occur MUST = BooleanClause.Occur.MUST; if (bbox.getCrossesDateLine()) { //use null as performance trick since no data will be beyond the world bounds bq.add(rangeQuery(fieldNameX, null/*-180*/, bbox.getMaxX()), BooleanClause.Occur.SHOULD ); bq.add(rangeQuery(fieldNameX, bbox.getMinX(), null/*+180*/), BooleanClause.Occur.SHOULD ); bq.setMinimumNumberShouldMatch(1);//must match at least one of the SHOULD } else { bq.add(rangeQuery(fieldNameX, bbox.getMinX(), bbox.getMaxX()), MUST); } bq.add(rangeQuery(fieldNameY, bbox.getMinY(), bbox.getMaxY()), MUST); return bq.build(); }
Example 13
Source File: BooleanQueryExpression.java From incubator-atlas with Apache License 2.0 | 5 votes |
private Collection<Pipe> processNotClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) { Collection<BooleanClause> notClauses = groupedClauses.get(BooleanClause.Occur.MUST_NOT); Collection<Pipe> notPipes = new ArrayList<>(); if (notClauses != null) { for (BooleanClause notClause : notClauses) { QueryExpression queryExpression = queryFactory.create(notClause.getQuery(), resourceDefinition); queryExpression.setNegate(); properties.addAll(queryExpression.getProperties()); notPipes.add(queryExpression.asPipe()); } } return notPipes; }
Example 14
Source File: MultiTermHighlighting.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public QueryVisitor getSubVisitor(BooleanClause.Occur occur, Query parent) { if (lookInSpan == false && parent instanceof SpanQuery) { return QueryVisitor.EMPTY_VISITOR; } return super.getSubVisitor(occur, parent); }
Example 15
Source File: QueryTermExtractor.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public QueryVisitor getSubVisitor(BooleanClause.Occur occur, Query parent) { if (parent instanceof BoostQuery) { float newboost = boost * ((BoostQuery)parent).getBoost(); return new BoostedTermExtractor(newboost, terms, includeProhibited, fieldSelector); } if (occur == BooleanClause.Occur.MUST_NOT && includeProhibited == false) { return QueryVisitor.EMPTY_VISITOR; } return this; }
Example 16
Source File: QueryBuilder.java From lucene-solr with Apache License 2.0 | 4 votes |
/** * Creates a query from a token stream. * * @param source the token stream to create the query from * @param operator default boolean operator used for this query * @param field field to create queries against * @param quoted true if phrases should be generated when terms occur at more than one position * @param phraseSlop slop factor for phrase/multiphrase queries */ protected Query createFieldQuery(TokenStream source, BooleanClause.Occur operator, String field, boolean quoted, int phraseSlop) { assert operator == BooleanClause.Occur.SHOULD || operator == BooleanClause.Occur.MUST; // Build an appropriate query based on the analysis chain. try (CachingTokenFilter stream = new CachingTokenFilter(source)) { TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class); PositionIncrementAttribute posIncAtt = stream.addAttribute(PositionIncrementAttribute.class); PositionLengthAttribute posLenAtt = stream.addAttribute(PositionLengthAttribute.class); if (termAtt == null) { return null; } // phase 1: read through the stream and assess the situation: // counting the number of tokens/positions and marking if we have any synonyms. int numTokens = 0; int positionCount = 0; boolean hasSynonyms = false; boolean isGraph = false; stream.reset(); while (stream.incrementToken()) { numTokens++; int positionIncrement = posIncAtt.getPositionIncrement(); if (positionIncrement != 0) { positionCount += positionIncrement; } else { hasSynonyms = true; } int positionLength = posLenAtt.getPositionLength(); if (enableGraphQueries && positionLength > 1) { isGraph = true; } } // phase 2: based on token count, presence of synonyms, and options // formulate a single term, boolean, or phrase. if (numTokens == 0) { return null; } else if (numTokens == 1) { // single term return analyzeTerm(field, stream); } else if (isGraph) { // graph if (quoted) { return analyzeGraphPhrase(stream, field, phraseSlop); } else { return analyzeGraphBoolean(field, stream, operator); } } else if (quoted && positionCount > 1) { // phrase if (hasSynonyms) { // complex phrase with synonyms return analyzeMultiPhrase(field, stream, phraseSlop); } else { // simple phrase return analyzePhrase(field, stream, phraseSlop); } } else { // boolean if (positionCount == 1) { // only one position, with synonyms return analyzeBoolean(field, stream); } else { // complex case: multiple positions return analyzeMultiBoolean(field, stream, operator); } } } catch (IOException e) { throw new RuntimeException("Error analyzing query text", e); } }
Example 17
Source File: MatchQuery.java From crate with Apache License 2.0 | 4 votes |
public void setOccur(BooleanClause.Occur occur) { this.occur = occur; }
Example 18
Source File: AnalyzingInfixSuggester.java From lucene-solr with Apache License 2.0 | 4 votes |
/** Retrieve suggestions, specifying whether all terms * must match ({@code allTermsRequired}) and whether the hits * should be highlighted ({@code doHighlight}). */ public List<LookupResult> lookup(CharSequence key, Map<BytesRef, BooleanClause.Occur> contextInfo, int num, boolean allTermsRequired, boolean doHighlight) throws IOException { return lookup(key, toQuery(contextInfo), num, allTermsRequired, doHighlight); }
Example 19
Source File: QueryBuilder.java From lucene-solr with Apache License 2.0 | 4 votes |
/** * Creates a boolean query from a graph token stream. The articulation points of the graph are visited in order and the queries * created at each point are merged in the returned boolean query. */ protected Query analyzeGraphBoolean(String field, TokenStream source, BooleanClause.Occur operator) throws IOException { source.reset(); GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source); BooleanQuery.Builder builder = new BooleanQuery.Builder(); int[] articulationPoints = graph.articulationPoints(); int lastState = 0; for (int i = 0; i <= articulationPoints.length; i++) { int start = lastState; int end = -1; if (i < articulationPoints.length) { end = articulationPoints[i]; } lastState = end; final Query positionalQuery; if (graph.hasSidePath(start)) { final Iterator<TokenStream> sidePathsIterator = graph.getFiniteStrings(start, end); Iterator<Query> queries = new Iterator<Query>() { @Override public boolean hasNext() { return sidePathsIterator.hasNext(); } @Override public Query next() { TokenStream sidePath = sidePathsIterator.next(); return createFieldQuery(sidePath, BooleanClause.Occur.MUST, field, getAutoGenerateMultiTermSynonymsPhraseQuery(), 0); } }; positionalQuery = newGraphSynonymQuery(queries); } else { List<AttributeSource> attributes = graph.getTerms(start); TermAndBoost[] terms = attributes.stream() .map(s -> { TermToBytesRefAttribute t = s.addAttribute(TermToBytesRefAttribute.class); BoostAttribute b = s.addAttribute(BoostAttribute.class); return new TermAndBoost(new Term(field, t.getBytesRef()), b.getBoost()); }) .toArray(TermAndBoost[]::new); assert terms.length > 0; if (terms.length == 1) { positionalQuery = newTermQuery(terms[0].term, terms[0].boost); } else { positionalQuery = newSynonymQuery(terms); } } if (positionalQuery != null) { builder.add(positionalQuery, operator); } } return builder.build(); }
Example 20
Source File: LuceneQueryVisitor.java From cxf with Apache License 2.0 | 3 votes |
private Query createCompositeQuery(List<Query> queries, boolean orCondition) { BooleanClause.Occur clause = orCondition ? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST; BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (Query query : queries) { builder.add(query, clause); } return builder.build(); }