List of usage examples for org.apache.lucene.search BooleanQuery clauses
List clauses
To view the source code for org.apache.lucene.search BooleanQuery clauses.
Click Source Link
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexAugmentorFactoryTest.java
License:Apache License
void validateComposedQueryTerms(String type, String... expected) { FulltextQueryTermsProvider compositeQueryTermsProvider = indexAugmentorFactory .getFulltextQueryTermsProvider(type); if (expected.length > 0) { assertTrue("Composed query terms provider doesn't declare correct supported type", compositeQueryTermsProvider.getSupportedTypes().contains(type)); }//w w w . ja v a 2 s. co m Query q = compositeQueryTermsProvider.getQueryTerm(null, null, null); if (q == null) { assertEquals("No query terms generated for " + type + ".", 0, expected.length); } else { Set<String> ids = Sets.newHashSet(); if (q instanceof BooleanQuery) { BooleanQuery query = (BooleanQuery) q; List<BooleanClause> clauses = query.clauses(); for (BooleanClause clause : clauses) { assertEquals(SHOULD, clause.getOccur()); Query subQuery = clause.getQuery(); String subQueryStr = subQuery.toString(); ids.add(subQueryStr.substring(0, subQueryStr.indexOf(":1"))); } } else { String subQueryStr = q.toString(); ids.add(subQueryStr.substring(0, subQueryStr.indexOf(":1"))); } assertEquals(expected.length, Iterables.size(ids)); assertThat(ids, CoreMatchers.hasItems(expected)); } }
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LucenePropertyIndex.java
License:Apache License
private static void addNodeTypeConstraints(IndexingRule defn, List<Query> qs, Filter filter) { BooleanQuery bq = new BooleanQuery(); PropertyDefinition primaryType = defn.getConfig(JCR_PRIMARYTYPE); //TODO OAK-2198 Add proper nodeType query support if (primaryType != null && primaryType.propertyIndex) { for (String type : filter.getPrimaryTypes()) { bq.add(new TermQuery(new Term(JCR_PRIMARYTYPE, type)), SHOULD); }/*from w ww.j a va 2s. co m*/ } PropertyDefinition mixinType = defn.getConfig(JCR_MIXINTYPES); if (mixinType != null && mixinType.propertyIndex) { for (String type : filter.getMixinTypes()) { bq.add(new TermQuery(new Term(JCR_MIXINTYPES, type)), SHOULD); } } if (bq.clauses().size() != 0) { qs.add(bq); } }
From source file:org.apache.solr.handler.component.WinWinQueryComponent.java
License:Apache License
private Query combine(Query q1, float impact1, Query q2, float impact2) { BooleanQuery newQuery = new BooleanQuery(); List newQueryClauses = newQuery.clauses(); absorb(newQuery, q1, impact1);//from w w w. j av a 2 s . c o m absorb(newQuery, q2, impact2); return newQuery; }
From source file:org.apache.solr.handler.component.WinWinQueryComponent.java
License:Apache License
private void absorb(BooleanQuery query, TermQuery absorbedQuery, float impact) { List queryClauses = query.clauses(); Query tempQuery = absorbedQuery.clone(); float boost = tempQuery.getBoost() * impact; if (boost > 0.1) { tempQuery.setBoost(boost);/* ww w . ja v a 2 s . co m*/ BooleanClause newClause = new BooleanClause(tempQuery, Occur.SHOULD); queryClauses.add(newClause); } }
From source file:org.apache.solr.handler.component.WinWinQueryComponent.java
License:Apache License
private void absorb(BooleanQuery query, BooleanQuery absorbedQuery, float impact) { List newQueryClauses = query.clauses(); List absorbedClauses = absorbedQuery.clauses(); for (int i = 0; i < absorbedClauses.size(); i++) { BooleanClause absorbedClause = (BooleanClause) absorbedClauses.get(i); if (absorbedClause.getOccur().equals(BooleanClause.Occur.SHOULD) || absorbedClause.getOccur().equals(BooleanClause.Occur.MUST)) { Query tempQuery = absorbedClause.getQuery().clone(); float boost = tempQuery.getBoost() * impact; if (boost > 0.1) { tempQuery.setBoost(boost); BooleanClause newClause = new BooleanClause(tempQuery, Occur.SHOULD); newQueryClauses.add(newClause); }//w w w .ja v a2 s . co m } } }
From source file:org.apache.solr.search.LuceneQueryOptimizer.java
License:Apache License
public TopDocs optimize(BooleanQuery original, SolrIndexSearcher searcher, int numHits, Query[] queryOut, Filter[] filterOut) throws IOException { BooleanQuery query = new BooleanQuery(); BooleanQuery filterQuery = null;/*from w w w . j a va 2s . co m*/ for (BooleanClause c : original.clauses()) { /*** System.out.println("required="+c.required); System.out.println("boost="+c.query.getBoost()); System.out.println("isTermQuery="+(c.query instanceof TermQuery)); if (c.query instanceof TermQuery) { System.out.println("term="+((TermQuery)c.query).getTerm()); System.out.println("docFreq="+searcher.docFreq(((TermQuery)c.query).getTerm())); } ***/ Query q = c.getQuery(); if (c.isRequired() // required && q.getBoost() == 0.0f // boost is zero && q instanceof TermQuery // TermQuery && (searcher.docFreq(((TermQuery) q).getTerm()) / (float) searcher.maxDoc()) >= threshold) { // check threshold if (filterQuery == null) filterQuery = new BooleanQuery(); filterQuery.add(q, BooleanClause.Occur.MUST); // filter it //System.out.println("WooHoo... qualified to be hoisted to a filter!"); } else { query.add(c); // query it } } Filter filter = null; if (filterQuery != null) { synchronized (cache) { // check cache filter = (Filter) cache.get(filterQuery); } if (filter == null) { // miss filter = new CachingWrapperFilter(new QueryWrapperFilter(filterQuery)); // construct new entry synchronized (cache) { cache.put(filterQuery, filter); // cache it } } } // YCS: added code to pass out optimized query and filter // so they can be used with Hits if (queryOut != null && filterOut != null) { queryOut[0] = query; filterOut[0] = filter; return null; } else { return searcher.search(query, filter, numHits); } }
From source file:org.apache.solr.search.QueryUtils.java
License:Apache License
/** return true if this query has no positive components */ public static boolean isNegative(Query q) { if (!(q instanceof BooleanQuery)) return false; BooleanQuery bq = (BooleanQuery) q; List<BooleanClause> clauses = bq.clauses(); if (clauses.size() == 0) return false; for (BooleanClause clause : clauses) { if (!clause.isProhibited()) return false; }//from w ww .j a v a 2 s . c om return true; }
From source file:org.apache.solr.search.QueryUtils.java
License:Apache License
/** Returns the original query if it was already a positive query, otherwise * return the negative of the query (i.e., a positive query). * <p>/*from ww w. j a v a 2 s . co m*/ * Example: both id:10 and id:-10 will return id:10 * <p> * The caller can tell the sign of the original by a reference comparison between * the original and returned query. * @param q Query to create the absolute version of * @return Absolute version of the Query */ public static Query getAbs(Query q) { if (q instanceof WrappedQuery) { Query subQ = ((WrappedQuery) q).getWrappedQuery(); Query absSubQ = getAbs(subQ); if (absSubQ == subQ) return q; WrappedQuery newQ = (WrappedQuery) q.clone(); newQ.setWrappedQuery(absSubQ); return newQ; } if (!(q instanceof BooleanQuery)) return q; BooleanQuery bq = (BooleanQuery) q; List<BooleanClause> clauses = bq.clauses(); if (clauses.size() == 0) return q; for (BooleanClause clause : clauses) { if (!clause.isProhibited()) return q; } if (clauses.size() == 1) { // if only one clause, dispense with the wrapping BooleanQuery Query negClause = clauses.get(0).getQuery(); // we shouldn't need to worry about adjusting the boosts since the negative // clause would have never been selected in a positive query, and hence would // not contribute to a score. return negClause; } else { BooleanQuery newBq = new BooleanQuery(bq.isCoordDisabled()); newBq.setBoost(bq.getBoost()); // ignore minNrShouldMatch... it doesn't make sense for a negative query // the inverse of -a -b is a OR b for (BooleanClause clause : clauses) { newBq.add(clause.getQuery(), BooleanClause.Occur.SHOULD); } return newBq; } }
From source file:org.apache.solr.search.TestSolrCoreParser.java
License:Apache License
public void testHandyQuery() throws IOException, ParserException { final String lhsXml = "<HelloQuery/>"; final String rhsXml = "<GoodbyeQuery/>"; final Query query = parseHandyQuery(lhsXml, rhsXml); assertTrue(query instanceof BooleanQuery); final BooleanQuery bq = (BooleanQuery) query; assertEquals(2, bq.clauses().size()); assertTrue(bq.clauses().get(0).getQuery() instanceof MatchAllDocsQuery); assertTrue(bq.clauses().get(1).getQuery() instanceof MatchNoDocsQuery); }
From source file:org.apache.solr.search.TestSolrCoreParser.java
License:Apache License
public void testHandySpanQuery() throws IOException, ParserException { final String lhsXml = "<SpanOr fieldName='contents'>" + "<SpanTerm>rain</SpanTerm>" + "<SpanTerm>spain</SpanTerm>" + "<SpanTerm>plain</SpanTerm>" + "</SpanOr>"; final String rhsXml = "<SpanNear fieldName='contents' slop='2' inOrder='true'>" + "<SpanTerm>sunny</SpanTerm>" + "<SpanTerm>sky</SpanTerm>" + "</SpanNear>"; final Query query = parseHandyQuery(lhsXml, rhsXml); final BooleanQuery bq = (BooleanQuery) query; assertEquals(2, bq.clauses().size()); for (int ii = 0; ii < bq.clauses().size(); ++ii) { final Query clauseQuery = bq.clauses().get(ii).getQuery(); switch (ii) { case 0:/* w w w . ja v a 2 s . c o m*/ assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanOrQuery); break; case 1: assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanNearQuery); break; default: fail("unexpected clause index " + ii); } } }