Java Code Examples for org.apache.lucene.search.BooleanQuery#Builder
The following examples show how to use
org.apache.lucene.search.BooleanQuery#Builder .
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: TestQueryBuilder.java From lucene-solr with Apache License 2.0 | 6 votes |
/** more complex synonyms with default AND operator */ public void testCJKSynonymsAND2() throws Exception { BooleanQuery.Builder expected = new BooleanQuery.Builder(); expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.MUST); SynonymQuery inner = new SynonymQuery.Builder("field") .addTerm(new Term("field", "国")) .addTerm(new Term("field", "國")) .build(); expected.add(inner, BooleanClause.Occur.MUST); SynonymQuery inner2 = new SynonymQuery.Builder("field") .addTerm(new Term("field", "国")) .addTerm(new Term("field", "國")) .build(); expected.add(inner2, BooleanClause.Occur.MUST); QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer()); assertEquals(expected.build(), builder.createBooleanQuery("field", "中国国", BooleanClause.Occur.MUST)); }
Example 2
Source File: TestSimilarity2.java From lucene-solr with Apache License 2.0 | 6 votes |
/** similar to the above, but ORs the query with a real field */ public void testEmptyField() throws Exception { Directory dir = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), dir); Document doc = new Document(); doc.add(newTextField("foo", "bar", Field.Store.NO)); iw.addDocument(doc); IndexReader ir = iw.getReader(); iw.close(); IndexSearcher is = newSearcher(ir); for (Similarity sim : sims) { is.setSimilarity(sim); BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("bar", "baz")), BooleanClause.Occur.SHOULD); assertEquals(1, is.search(query.build(), 10).totalHits.value); } ir.close(); dir.close(); }
Example 3
Source File: KNearestNeighborClassifier.java From lucene-solr with Apache License 2.0 | 6 votes |
private TopDocs knnSearch(String text) throws IOException { BooleanQuery.Builder mltQuery = new BooleanQuery.Builder(); for (String fieldName : textFieldNames) { String boost = null; mlt.setBoost(true); //terms boost actually helps in MLT queries if (fieldName.contains("^")) { String[] field2boost = fieldName.split("\\^"); fieldName = field2boost[0]; boost = field2boost[1]; } if (boost != null) { mlt.setBoostFactor(Float.parseFloat(boost));//if we have a field boost, we add it } mltQuery.add(new BooleanClause(mlt.like(fieldName, new StringReader(text)), BooleanClause.Occur.SHOULD)); mlt.setBoostFactor(1);// restore neutral boost for next field } Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*")); mltQuery.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST)); if (query != null) { mltQuery.add(query, BooleanClause.Occur.MUST); } return indexSearcher.search(mltQuery.build(), k); }
Example 4
Source File: HighlighterTest.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testGetBestFragmentsFilteredQuery() throws Exception { TestHighlightRunner helper = new TestHighlightRunner() { @Override public void run() throws Exception { numHighlights = 0; SpanQuery clauses[] = { new SpanTermQuery(new Term("contents", "john")), new SpanTermQuery(new Term("contents", "kennedy")), }; SpanNearQuery snq = new SpanNearQuery(clauses, 1, true); BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(snq, Occur.MUST); bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER); doSearching(bq.build()); doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this); // Currently highlights "John" and "Kennedy" separately assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 2); } }; helper.start(); }
Example 5
Source File: TestStandardQP.java From lucene-solr with Apache License 2.0 | 6 votes |
@Override public void testNewFieldQuery() throws Exception { /** ordinary behavior, synonyms form uncoordinated boolean query */ StandardQueryParser dumb = getParser(new Analyzer1()); BooleanQuery.Builder expanded = new BooleanQuery.Builder(); expanded.add(new TermQuery(new Term("field", "dogs")), BooleanClause.Occur.SHOULD); expanded.add(new TermQuery(new Term("field", "dog")), BooleanClause.Occur.SHOULD); assertEquals(expanded.build(), dumb.parse("\"dogs\"","field")); /** even with the phrase operator the behavior is the same */ assertEquals(expanded.build(), dumb.parse("dogs","field")); /** * custom behavior, the synonyms are expanded, unless you use quote operator */ //TODO test something like "SmartQueryParser()" }
Example 6
Source File: ExtendedDismaxQParser.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Parses an escaped version of the user's query. This method is called * in the event that the original query encounters exceptions during parsing. * * @param up parser used * @param escapedUserQuery query that is parsed, should already be escaped so that no trivial parse errors are encountered * @param config Configuration options for this parse request * @return the resulting query (flattened if needed) with "min should match" rules applied as specified in the config. * @see #parseOriginalQuery * @see SolrPluginUtils#flattenBooleanQuery */ protected Query parseEscapedQuery(ExtendedSolrQueryParser up, String escapedUserQuery, ExtendedDismaxConfiguration config) throws SyntaxError { Query query = up.parse(escapedUserQuery); if (query instanceof BooleanQuery) { BooleanQuery.Builder t = new BooleanQuery.Builder(); SolrPluginUtils.flattenBooleanQuery(t, (BooleanQuery)query); SolrPluginUtils.setMinShouldMatch(t, config.minShouldMatch, config.mmAutoRelax); query = QueryUtils.build(t, this); } return query; }
Example 7
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 8
Source File: KNearestFuzzyClassifier.java From lucene-solr with Apache License 2.0 | 5 votes |
private TopDocs knnSearch(String text) throws IOException { BooleanQuery.Builder bq = new BooleanQuery.Builder(); NearestFuzzyQuery nearestFuzzyQuery = new NearestFuzzyQuery(analyzer); for (String fieldName : textFieldNames) { nearestFuzzyQuery.addTerms(text, fieldName); } bq.add(nearestFuzzyQuery, BooleanClause.Occur.MUST); Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*")); bq.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST)); if (query != null) { bq.add(query, BooleanClause.Occur.MUST); } return indexSearcher.search(bq.build(), k); }
Example 9
Source File: LuceneOrderedDocCollector.java From crate with Apache License 2.0 | 5 votes |
private Query query(FieldDoc lastDoc) { Query optimizedQuery = searchAfterQueryOptimize.apply(lastDoc); if (optimizedQuery == null) { return this.query; } BooleanQuery.Builder searchAfterQuery = new BooleanQuery.Builder(); searchAfterQuery.add(this.query, BooleanClause.Occur.MUST); searchAfterQuery.add(optimizedQuery, BooleanClause.Occur.MUST_NOT); return searchAfterQuery.build(); }
Example 10
Source File: EqQuery.java From crate with Apache License 2.0 | 5 votes |
/** * Query for object columns that tries to utilize efficient termQueries for the objects children. * <pre> * {@code * // If x and y are known columns * o = {x=10, y=20} -> o.x=10 and o.y=20 * * // Only x is known: * o = {x=10, y=20} -> o.x=10 and generic(o == {x=10, y=20}) * * // No column is known: * o = {x=10, y=20} -> generic(o == {x=10, y=20}) * } * </pre> */ private static Query refEqObject(Function eq, Reference reference, Map<String, Object> value, LuceneQueryBuilder.Context context) { BooleanQuery.Builder boolBuilder = new BooleanQuery.Builder(); int preFilters = 0; for (Map.Entry<String, Object> entry : value.entrySet()) { String nestedColumn = entry.getKey(); MappedFieldType fieldType = context.getFieldTypeOrNull(reference.column().fqn() + "." + nestedColumn); if (fieldType == null) { // could be a nested object; skip pre-filtering continue; } preFilters++; boolBuilder.add(fieldType.termQuery(entry.getValue(), context.queryShardContext), BooleanClause.Occur.MUST); } Query genericFilter = genericFunctionFilter(eq, context); if (preFilters == 0) { return genericFilter; } else if (preFilters == value.size()) { return boolBuilder.build(); } else { boolBuilder.add(genericFilter, BooleanClause.Occur.FILTER); return boolBuilder.build(); } }
Example 11
Source File: TestSimpleQueryParser.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testDisableSlop() { PhraseQuery expectedPhrase = new PhraseQuery("field", "foo", "bar"); BooleanQuery.Builder expected = new BooleanQuery.Builder(); expected.add(expectedPhrase, Occur.MUST); expected.add(new TermQuery(new Term("field", "~2")), Occur.MUST); assertEquals(expected.build(), parse("\"foo bar\"~2", ~NEAR_OPERATOR)); }
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: DefaultSearchContext.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query searchFilter(String[] types) { Query filter = mapperService().searchFilter(types); if (filter == null && aliasFilter == null) { return null; } BooleanQuery.Builder bq = new BooleanQuery.Builder(); if (filter != null) { bq.add(filter, Occur.MUST); } if (aliasFilter != null) { bq.add(aliasFilter, Occur.MUST); } return new ConstantScoreQuery(bq.build()); }
Example 14
Source File: LumongoIndex.java From lumongo with Apache License 2.0 | 5 votes |
public void handleCosineSimQuery(QueryWithFilters queryWithFilters, Lumongo.CosineSimRequest cosineSimRequest) { indexLock.readLock().lock(); try { double vector[] = new double[cosineSimRequest.getVectorCount()]; for (int i = 0; i < cosineSimRequest.getVectorCount(); i++) { vector[i] = cosineSimRequest.getVector(i); } SuperBit superBit = indexConfig.getSuperBitForField(cosineSimRequest.getField()); boolean[] signature = superBit.signature(vector); int mm = (int) ((1 - (Math.acos(cosineSimRequest.getSimilarity()) / Math.PI)) * signature.length); BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); booleanQueryBuilder.setMinimumNumberShouldMatch(mm); for (int i = 0; i < signature.length; i++) { String fieldName = LumongoConstants.SUPERBIT_PREFIX + "." + cosineSimRequest.getField() + "." + i; booleanQueryBuilder.add(new BooleanClause(new TermQuery(new org.apache.lucene.index.Term(fieldName, signature[i] ? "1" : "0")), BooleanClause.Occur.SHOULD)); queryWithFilters.addSimilarityOverride(Lumongo.FieldSimilarity.newBuilder().setField(fieldName).setSimilarity(Similarity.CONSTANT).build()); } BooleanQuery query = booleanQueryBuilder.build(); queryWithFilters.addScoredFilterQuery(query); } finally { indexLock.readLock().unlock(); } }
Example 15
Source File: LuceneIndex.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Evaluates the given query only for the given resource. * * @param resource * @param query * @return top documents * @throws IOException */ public synchronized TopDocs search(Resource resource, Query query) throws IOException { // rewrite the query TermQuery idQuery = new TermQuery(new Term(SearchFields.URI_FIELD_NAME, SearchFields.getResourceID(resource))); BooleanQuery.Builder combinedQuery = new BooleanQuery.Builder(); combinedQuery.add(idQuery, Occur.MUST); combinedQuery.add(query, Occur.MUST); return search(combinedQuery.build()); }
Example 16
Source File: CollationTestBase.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testFarsiRangeFilterCollating(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd) throws Exception { Directory dir = newDirectory(); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer)); Document doc = new Document(); doc.add(new TextField("content", "\u0633\u0627\u0628", Field.Store.YES)); doc.add(new StringField("body", "body", Field.Store.YES)); writer.addDocument(doc); writer.close(); IndexReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); Query query = new TermQuery(new Term("body","body")); // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi // orders the U+0698 character before the U+0633 character, so the single // index Term below should NOT be returned by a TermRangeFilter with a Farsi // Collator (or an Arabic one for the case when Farsi searcher not // supported). BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(query, Occur.MUST); bq.add(new TermRangeQuery("content", firstBeg, firstEnd, true, true), Occur.FILTER); ScoreDoc[] result = searcher.search(bq.build(), 1).scoreDocs; assertEquals("The index Term should not be included.", 0, result.length); bq = new BooleanQuery.Builder(); bq.add(query, Occur.MUST); bq.add(new TermRangeQuery("content", secondBeg, secondEnd, true, true), Occur.FILTER); result = searcher.search(bq.build(), 1).scoreDocs; assertEquals("The index Term should be included.", 1, result.length); reader.close(); dir.close(); }
Example 17
Source File: MtasJoinQParser.java From mtas with Apache License 2.0 | 5 votes |
@Override public Query parse() throws SyntaxError { if (id == null) { throw new SyntaxError("no " + MTAS_JOIN_QPARSER_COLLECTION); } else if (fields == null) { throw new SyntaxError("no " + MTAS_JOIN_QPARSER_FIELD); } else { BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); MtasSolrCollectionCache mtasSolrJoinCache = null; for (PluginHolder<SearchComponent> item : req.getCore() .getSearchComponents().getRegistry().values()) { if (item.get() instanceof MtasSolrSearchComponent) { mtasSolrJoinCache = ((MtasSolrSearchComponent) item.get()) .getCollectionCache(); } } if (mtasSolrJoinCache != null) { Automaton automaton; try { automaton = mtasSolrJoinCache.getAutomatonById(id); if (automaton != null) { for (String field : fields) { booleanQueryBuilder.add( new AutomatonQuery(new Term(field), automaton), Occur.SHOULD); } } else { throw new IOException("no data for collection '" + id + "'"); } } catch (IOException e) { throw new SyntaxError( "could not construct automaton: " + e.getMessage(), e); } return booleanQueryBuilder.build(); } else { throw new SyntaxError("no MtasSolrSearchComponent found"); } } }
Example 18
Source File: PathQuery.java From onedev with MIT License | 4 votes |
@Override protected void applyConstraints(BooleanQuery.Builder builder) { builder.add(new WildcardQuery(new Term(BLOB_PATH.name(), "*"+match+"*")), Occur.MUST); }
Example 19
Source File: CachingNaiveBayesClassifier.java From lucene-solr with Apache License 2.0 | 4 votes |
private Map<BytesRef, Integer> getWordFreqForClassess(String word) throws IOException { Map<BytesRef, Integer> insertPoint; insertPoint = termCClassHitCache.get(word); // if we get the answer from the cache if (insertPoint != null) { if (!insertPoint.isEmpty()) { return insertPoint; } } Map<BytesRef, Integer> searched = new ConcurrentHashMap<>(); // if we dont get the answer, but it's relevant we must search it and insert to the cache if (insertPoint != null || !justCachedTerms) { for (BytesRef cclass : cclasses) { BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); BooleanQuery.Builder subQuery = new BooleanQuery.Builder(); for (String textFieldName : textFieldNames) { subQuery.add(new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD)); } booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST)); booleanQuery.add(new BooleanClause(new TermQuery(new Term(classFieldName, cclass)), BooleanClause.Occur.MUST)); if (query != null) { booleanQuery.add(query, BooleanClause.Occur.MUST); } TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector(); indexSearcher.search(booleanQuery.build(), totalHitCountCollector); int ret = totalHitCountCollector.getTotalHits(); if (ret != 0) { searched.put(cclass, ret); } } if (insertPoint != null) { // threadsafe and concurrent write termCClassHitCache.put(word, searched); } } return searched; }
Example 20
Source File: SimpleQueryParser.java From lucene-solr with Apache License 2.0 | 4 votes |
private void buildQueryTree(State state, Query branch) { if (branch != null) { // modify our branch to a BooleanQuery wrapper for not // this is necessary any time a term, phrase, or subquery is negated if (state.not % 2 == 1) { BooleanQuery.Builder nq = new BooleanQuery.Builder(); nq.add(branch, BooleanClause.Occur.MUST_NOT); nq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD); branch = nq.build(); } // first term (or phrase or subquery) found and will begin our query tree if (state.top == null) { state.top = branch; } else { // more than one term (or phrase or subquery) found // set currentOperation to the default if no other operation is explicitly set if (state.currentOperation == null) { state.currentOperation = defaultOperator; } // operational change requiring a new parent node // this occurs if the previous operation is not the same as current operation // because the previous operation must be evaluated separately to preserve // the proper precedence and the current operation will take over as the top of the tree if (state.previousOperation != state.currentOperation) { BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(state.top, state.currentOperation); state.top = bq.build(); } // reset all of the state for reuse state.top = addClause((BooleanQuery) state.top, branch, state.currentOperation); state.previousOperation = state.currentOperation; } // reset the current operation as it was intended to be applied to // the incoming term (or phrase or subquery) even if branch was null // due to other possible errors state.currentOperation = null; } }