Java Code Examples for org.apache.lucene.search.BooleanQuery#clauses()
The following examples show how to use
org.apache.lucene.search.BooleanQuery#clauses() .
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: QueryHelper.java From fess with Apache License 2.0 | 7 votes |
protected QueryBuilder convertBooleanQuery(final QueryContext context, final BooleanQuery booleanQuery, final float boost) { final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); for (final BooleanClause clause : booleanQuery.clauses()) { final QueryBuilder queryBuilder = convertQuery(context, clause.getQuery(), boost); if (queryBuilder != null) { switch (clause.getOccur()) { case MUST: boolQuery.must(queryBuilder); break; case SHOULD: boolQuery.should(queryBuilder); break; case MUST_NOT: boolQuery.mustNot(queryBuilder); break; default: break; } } } if (boolQuery.hasClauses()) { return boolQuery; } return null; }
Example 2
Source File: QueryTransformer.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 6 votes |
@Override public Query handle(Query query, QueryTransformer queryTransformer) { BooleanQuery booleanQuery = (BooleanQuery) query; boolean changed = false; BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (BooleanClause clause: booleanQuery.clauses()) { Query newClauseQuery = queryTransformer.transform(clause.getQuery()); if (newClauseQuery != clause.getQuery()) { changed = true; builder.add(new BooleanClause(newClauseQuery, clause.getOccur())); } else { builder.add(clause); } } if (changed) { builder.setMinimumNumberShouldMatch(booleanQuery.getMinimumNumberShouldMatch()); return builder.build(); } return query; }
Example 3
Source File: SuperParser.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
private String findFirstGroupName(Query query) { if (query instanceof BooleanQuery) { BooleanQuery booleanQuery = (BooleanQuery) query; for (BooleanClause clause : booleanQuery.clauses()) { String groupName = findFirstGroupName(clause.getQuery()); if (groupName != null) { return groupName; } } return null; } else if (query instanceof SuperQuery) { SuperQuery sq = (SuperQuery) query; return findFirstGroupName(sq.getQuery()); } else { String fieldName = _fieldNames.get(query); return getGroupName(fieldName); } }
Example 4
Source File: TestMoreLikeThis.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testSeedDocumentMap_minTermFrequencySet_shouldBuildQueryAccordingToCorrectTermFrequencies() throws Exception { mlt.setMinTermFreq(3); String mltField1 = "text"; String mltField2 = "text2"; mlt.setFieldNames(new String[]{mltField1, mltField2}); Map<String, Collection<Object>> seedDocument = new HashMap<>(); String textValue = "apache apache lucene lucene lucene"; seedDocument.put(mltField1, Arrays.asList(textValue)); seedDocument.put(mltField2, Arrays.asList(textValue)); BooleanQuery query = (BooleanQuery) mlt.like(seedDocument); Collection<BooleanClause> clauses = query.clauses(); assertEquals("Expected 1 clauses only!", 1, clauses.size()); for (BooleanClause clause : clauses) { Term term = ((TermQuery) clause.getQuery()).getTerm(); assertThat(term, is(new Term(mltField1, "lucene"))); } analyzer.close(); }
Example 5
Source File: SuperParser.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
private boolean isSameGroupName(Query query, String groupName) { if (query instanceof BooleanQuery) { BooleanQuery booleanQuery = (BooleanQuery) query; for (BooleanClause clause : booleanQuery.clauses()) { if (!isSameGroupName(clause.getQuery(), groupName)) { return false; } } return true; } else { String fieldName = _fieldNames.get(query); String currentGroupName = getGroupName(fieldName); if (groupName.equals(currentGroupName) || isSystemField(fieldName)) { return true; } return false; } }
Example 6
Source File: SolrPluginUtils.java From lucene-solr with Apache License 2.0 | 6 votes |
private static void flattenBooleanQuery(BooleanQuery.Builder to, BooleanQuery from, float fromBoost) { for (BooleanClause clause : from.clauses()) { Query cq = clause.getQuery(); float boost = fromBoost; while (cq instanceof BoostQuery) { BoostQuery bq = (BoostQuery) cq; cq = bq.getQuery(); boost *= bq.getBoost(); } if (cq instanceof BooleanQuery && !clause.isRequired() && !clause.isProhibited()) { /* we can recurse */ flattenBooleanQuery(to, (BooleanQuery)cq, boost); } else { to.add(clause); } } }
Example 7
Source File: TestMinHashQParser.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testBandsWrap() throws SyntaxError { NamedList<Object> par = new NamedList<>(); par.add("sim", "0.8"); par.add("tp", "0.694"); par.add("sep", ","); par.add("debug", "false"); QParser qparser = h.getCore().getQueryPlugin("minhash").createParser("1, 2, 3, 4, 5, 6, 7, 8, 9, 10", SolrParams.toSolrParams(par), null, null); Query query = qparser.getQuery(); BooleanQuery bq = (BooleanQuery)query; assertEquals(4, bq.clauses().size()); for(BooleanClause clause : bq.clauses()) { assertEquals(3, ((BooleanQuery)((ConstantScoreQuery)clause.getQuery()).getQuery()) .clauses().size()); } }
Example 8
Source File: DismaxSearchEngineRequestAdapter.java From querqy with Apache License 2.0 | 6 votes |
@Override public Query applyMinimumShouldMatch(final BooleanQuery query) { final List<BooleanClause> clauses = query.clauses(); if (clauses.size() < 2) { return query; } for (final BooleanClause clause : clauses) { if ((clause.getQuery() instanceof BooleanQuery) && (clause.getOccur() != BooleanClause.Occur.MUST)) { return query; // seems to be a complex query with sub queries - do not // apply mm } } return SolrPluginUtils.setMinShouldMatch(query, minShouldMatch); }
Example 9
Source File: Queries.java From crate with Apache License 2.0 | 6 votes |
public static Query applyMinimumShouldMatch(BooleanQuery query, @Nullable String minimumShouldMatch) { if (minimumShouldMatch == null) { return query; } int optionalClauses = 0; for (BooleanClause c : query.clauses()) { if (c.getOccur() == BooleanClause.Occur.SHOULD) { optionalClauses++; } } int msm = calculateMinShouldMatch(optionalClauses, minimumShouldMatch); if (0 < msm) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (BooleanClause clause : query) { builder.add(clause); } builder.setMinimumNumberShouldMatch(msm); return builder.build(); } else { return query; } }
Example 10
Source File: AliasBlurFilterCache.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
private Filter buildNewFilter(Query query, ConcurrentMap<String, String> filterAlias, FilterParser filterParser) throws ParseException { if (query instanceof BooleanQuery) { BooleanQuery booleanQuery = (BooleanQuery) query; BooleanFilter booleanFilter = new BooleanFilter(); for (BooleanClause clause : booleanQuery.clauses()) { booleanFilter.add(buildNewFilter(clause.getQuery(), filterAlias, filterParser), clause.getOccur()); } return booleanFilter; } else if (query instanceof TermQuery) { TermQuery termQuery = (TermQuery) query; Term term = termQuery.getTerm(); String key = term.toString(); String queryStr = filterAlias.get(key); if (queryStr == null) { return new QueryWrapperFilter(termQuery); } String id = getId(key); return new FilterCache(id, new QueryWrapperFilter(filterParser.parse(queryStr))); } else { return new QueryWrapperFilter(query); } }
Example 11
Source File: SuperParser.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
private Query fixNegatives(Query query) { if (query instanceof SuperQuery) { SuperQuery superQuery = (SuperQuery) query; fixNegatives(superQuery.getQuery()); } else if (query instanceof BooleanQuery) { BooleanQuery booleanQuery = (BooleanQuery) query; for (BooleanClause clause : booleanQuery.clauses()) { fixNegatives(clause.getQuery()); } if (containsAllNegativeQueries(booleanQuery)) { if (containsSuperQueries(booleanQuery)) { booleanQuery.add(new TermQuery(_defaultPrimeDocTerm), Occur.SHOULD); } else { booleanQuery.add(new MatchAllDocsQuery(), Occur.SHOULD); } } } return query; }
Example 12
Source File: BooleanQueryExtractor.java From solr-redis with Apache License 2.0 | 5 votes |
@Override public void extractSubQueriesFields(final BooleanQuery q, final Iterable<QueryExtractor<? extends Query>> extractors, final Set<String> extractedFields) throws UnsupportedOperationException { final List<BooleanClause> clauses = q.clauses(); for (final BooleanClause clause : clauses) { extractFields(clause.getQuery(), extractors, extractedFields); } }
Example 13
Source File: SuperParser.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private boolean allFieldQueriesAreSystemFields(Query query) { if (query instanceof BooleanQuery) { BooleanQuery booleanQuery = (BooleanQuery) query; for (BooleanClause clause : booleanQuery.clauses()) { if (!allFieldQueriesAreSystemFields(clause.getQuery())) { return false; } } return true; } else if (query instanceof SuperQuery) { return allFieldQueriesAreSystemFields(((SuperQuery) query).getQuery()); } else { return isSystemField(_fieldNames.get(query)); } }
Example 14
Source File: SuperParser.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private boolean containsAllNegativeQueries(BooleanQuery booleanQuery) { for (BooleanClause clause : booleanQuery.clauses()) { if (clause.getOccur() == Occur.MUST || clause.getOccur() == Occur.SHOULD) { return false; } } return true; }
Example 15
Source File: MatchQuery.java From crate with Apache License 2.0 | 5 votes |
private Query boolToExtendedCommonTermsQuery(BooleanQuery bq, Occur highFreqOccur, Occur lowFreqOccur, float maxTermFrequency) { ExtendedCommonTermsQuery query = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, maxTermFrequency); for (BooleanClause clause : bq.clauses()) { if ((clause.getQuery() instanceof TermQuery) == false) { return bq; } query.add(((TermQuery) clause.getQuery()).getTerm()); } return query; }
Example 16
Source File: BooleanQueryExtractor.java From solr-redis with Apache License 2.0 | 5 votes |
@Override public void extract(final BooleanQuery q, final Iterable<QueryExtractor<? extends Query>> extractors, final List<Query> extractedQueries) throws UnsupportedOperationException { final List<BooleanClause> clauses = q.clauses(); for (final BooleanClause clause : clauses) { extractQuery(clause.getQuery(), extractors, extractedQueries); } }
Example 17
Source File: QueryUtils.java From lucene-solr with Apache License 2.0 | 5 votes |
/** 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; Collection<BooleanClause> clauses = bq.clauses(); if (clauses.size()==0) return false; for (BooleanClause clause : clauses) { if (!clause.isProhibited()) return false; } return true; }
Example 18
Source File: TestMoreLikeThis.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testMultiValues() throws Exception { Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.KEYWORD, false); mlt.setAnalyzer(analyzer); mlt.setFieldNames(new String[] {"text"}); BooleanQuery query = (BooleanQuery) mlt.like("text", new StringReader("lucene"), new StringReader("lucene release"), new StringReader("apache"), new StringReader("apache lucene")); Collection<BooleanClause> clauses = query.clauses(); assertEquals("Expected 2 clauses only!", 2, clauses.size()); for (BooleanClause clause : clauses) { Term term = ((TermQuery) clause.getQuery()).getTerm(); assertTrue(Arrays.asList(new Term("text", "lucene"), new Term("text", "apache")).contains(term)); } }
Example 19
Source File: LumongoIndex.java From lumongo with Apache License 2.0 | 5 votes |
/** From org.apache.solr.search.QueryUtils **/ public static boolean isNegative(Query q) { if (!(q instanceof BooleanQuery)) return false; BooleanQuery bq = (BooleanQuery) q; Collection<BooleanClause> clauses = bq.clauses(); if (clauses.size() == 0) return false; for (BooleanClause clause : clauses) { if (!clause.isProhibited()) return false; } return true; }
Example 20
Source File: SuperParserTest.java From incubator-retired-blur with Apache License 2.0 | 4 votes |
public static void assertEqualsBooleanQuery(BooleanQuery expected, BooleanQuery actual) { List<BooleanClause> clauses1 = expected.clauses(); List<BooleanClause> clauses2 = actual.clauses(); assertEqualsBooleanClause(clauses1, clauses2); }