org.apache.lucene.search.BooleanClause.Occur Java Examples
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: QueryCollector.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (Lucene.exists(searcher, existsQuery)) { topDocsLeafCollector.collect(doc); postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
Example #2
Source File: DocumentUtil.java From netbeans with Apache License 2.0 | 6 votes |
private static BooleanQuery createFQNQuery(final String resourceName) { String pkgName; String sName; int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR); if (index < 0) { pkgName = ""; //NOI18N sName = resourceName; } else { pkgName = resourceName.substring(0, index); sName = resourceName.substring(index+1); } final BooleanQuery snQuery = new BooleanQuery(); snQuery.add (new WildcardQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + DocumentUtil.WILDCARD_QUERY_WILDCARD)),Occur.SHOULD); snQuery.add (new PrefixQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + '$')),Occur.SHOULD); //NOI18N if (pkgName.length() == 0) { return snQuery; } final BooleanQuery fqnQuery = new BooleanQuery(); fqnQuery.add(new TermQuery(new Term(DocumentUtil.FIELD_PACKAGE_NAME,pkgName)), Occur.MUST); fqnQuery.add(snQuery, Occur.MUST); return fqnQuery; }
Example #3
Source File: LuceneQueryBuilder.java From development with Apache License 2.0 | 6 votes |
private static BooleanQuery constructDefaultLocaleHandlingQuery( String fieldName, String locale, String defaultLocale, String searchPhrase) { BooleanQuery bq1 = new BooleanQuery(); TermQuery tq1 = new TermQuery( new Term(fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX, defaultLocale)); TermQuery tq2 = new TermQuery(new Term( fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX, locale)); bq1.add(tq1, Occur.MUST); bq1.add(tq2, Occur.MUST_NOT); BooleanQuery bq2 = new BooleanQuery(); WildcardQuery wq1 = new WildcardQuery( new Term(fieldName + defaultLocale, "*" + searchPhrase.toLowerCase() + "*")); bq2.add(wq1, Occur.SHOULD); BooleanQuery finalQuery = new BooleanQuery(); finalQuery.add(bq1, Occur.MUST); finalQuery.add(bq2, Occur.MUST); return finalQuery; }
Example #4
Source File: TestBooleanRewrites.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testDeduplicateMustClauses() throws IOException { IndexSearcher searcher = newSearcher(new MultiReader()); Query query = new BooleanQuery.Builder() .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .build(); Query expected = new BoostQuery(new TermQuery(new Term("foo", "bar")), 2); assertEquals(expected, searcher.rewrite(query)); query = new BooleanQuery.Builder() .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new BoostQuery(new TermQuery(new Term("foo", "bar")), 2), Occur.MUST) .add(new TermQuery(new Term("foo", "quux")), Occur.MUST) .build(); expected = new BooleanQuery.Builder() .add(new BoostQuery(new TermQuery(new Term("foo", "bar")), 3), Occur.MUST) .add(new TermQuery(new Term("foo", "quux")), Occur.MUST) .build(); assertEquals(expected, searcher.rewrite(query)); }
Example #5
Source File: LuceneAccessor.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public <K extends Comparable, T extends IdentityObject<K>> void iterateUnion(StorageIterator<T> iterator, Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) { LuceneMetadata metadata = metadatas.get(clazz); Query query = null; BooleanQuery.Builder buffer = new BooleanQuery.Builder(); for (Entry<String, Object> term : condition.entrySet()) { KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey()); Field key = keyValue.getKey(); IndexConverter value = keyValue.getValue(); query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue()); buffer.add(query, Occur.SHOULD); } query = buffer.build(); int offset = pagination == null ? 0 : pagination.getFirst(); int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize(); engine.iterateDocuments((document) -> { iterator.iterate((T) metadata.decodeDocument(document)); }, query, null, offset, size); }
Example #6
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 6 votes |
private Query createRegexQuery ( QueryDef qd ) { BooleanQuery query = new BooleanQuery(); List<SpanQuery> spanClausesList = new ArrayList<SpanQuery>(); String[] queryStrings; SpanQuery[] spanClausesArray; RegexQuery regexQuery; for ( String fld : qd.fields ) { spanClausesList.clear(); queryStrings = qd.query.split(" "); spanClausesArray = new SpanQuery[queryStrings.length]; for ( String subquery : queryStrings ) { regexQuery = new RegexQuery( new Term( fld, subquery ) ); regexQuery.setRegexImplementation( new JavaUtilRegexCapabilities() ); //if emptyable, like a query '(optional)?' or 'bla|a*', make span optional by wrapping it SpanOrQuery if(Pattern.matches(subquery, "")){ spanClausesList.add( new SpanOrQuery( new SpanMultiTermQueryWrapper<RegexQuery>( regexQuery ) ) ); } else { spanClausesList.add( new SpanMultiTermQueryWrapper<RegexQuery>( regexQuery ) ); } } spanClausesList.toArray( spanClausesArray ); query.add( new SpanNearQuery( spanClausesArray, 0, true), Occur.SHOULD ); } return query; }
Example #7
Source File: FieldQueryTest.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testGetTermSet() throws Exception { BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add(new TermQuery(new Term(F, "A")), Occur.MUST); query.add(new TermQuery(new Term(F, "B")), Occur.MUST); query.add(new TermQuery(new Term("x", "C")), Occur.SHOULD); BooleanQuery.Builder innerQuery = new BooleanQuery.Builder(); innerQuery.add(new TermQuery(new Term(F, "D")), Occur.MUST); innerQuery.add(new TermQuery(new Term(F, "E")), Occur.MUST); query.add(innerQuery.build(), Occur.MUST_NOT); FieldQuery fq = new FieldQuery( query.build(), true, true ); assertEquals( 2, fq.termSetMap.size() ); Set<String> termSet = fq.getTermSet( F ); assertEquals( 2, termSet.size() ); assertTrue( termSet.contains( "A" ) ); assertTrue( termSet.contains( "B" ) ); termSet = fq.getTermSet( "x" ); assertEquals( 1, termSet.size() ); assertTrue( termSet.contains( "C" ) ); termSet = fq.getTermSet( "y" ); assertNull( termSet ); }
Example #8
Source File: FieldTermStackTest.java From lucene-solr with Apache License 2.0 | 6 votes |
public void test2Terms() throws Exception { makeIndex(); BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add( tq( "b" ), Occur.SHOULD ); query.add( tq( "c" ), Occur.SHOULD ); FieldQuery fq = new FieldQuery( query.build(), true, true ); FieldTermStack stack = new FieldTermStack( reader, 0, F, fq ); assertEquals( 8, stack.termList.size() ); assertEquals( "b(6,7,3)", stack.pop().toString() ); assertEquals( "b(8,9,4)", stack.pop().toString() ); assertEquals( "c(10,11,5)", stack.pop().toString() ); assertEquals( "b(14,15,7)", stack.pop().toString() ); assertEquals( "b(16,17,8)", stack.pop().toString() ); assertEquals( "c(18,19,9)", stack.pop().toString() ); assertEquals( "b(26,27,13)", stack.pop().toString() ); assertEquals( "b(30,31,15)", stack.pop().toString() ); }
Example #9
Source File: LuceneAccessor.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public <K extends Comparable, T extends IdentityObject<K>> List<T> queryUnion(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) { LuceneMetadata metadata = metadatas.get(clazz); Query query = null; BooleanQuery.Builder buffer = new BooleanQuery.Builder(); for (Entry<String, Object> term : condition.entrySet()) { KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey()); Field key = keyValue.getKey(); IndexConverter value = keyValue.getValue(); query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue()); buffer.add(query, Occur.SHOULD); } query = buffer.build(); int offset = pagination == null ? 0 : pagination.getFirst(); int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize(); KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size); List<Document> documents = retrieve.getKey(); List<T> list = new ArrayList<>(BATCH_SIZE); for (Document document : documents) { list.add((T) metadata.decodeDocument(document)); } return list; }
Example #10
Source File: LuceneAccessor.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public <K extends Comparable, T extends IdentityObject<K>> List<T> queryIntersection(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) { LuceneMetadata metadata = metadatas.get(clazz); Query query = null; BooleanQuery.Builder buffer = new BooleanQuery.Builder(); for (Entry<String, Object> term : condition.entrySet()) { KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey()); Field key = keyValue.getKey(); IndexConverter value = keyValue.getValue(); query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue()); buffer.add(query, Occur.MUST); } query = buffer.build(); int offset = pagination == null ? 0 : pagination.getFirst(); int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize(); KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size); List<Document> documents = retrieve.getKey(); List<T> list = new ArrayList<>(BATCH_SIZE); for (Document document : documents) { list.add((T) metadata.decodeDocument(document)); } return list; }
Example #11
Source File: SearchServiceBean.java From development with Apache License 2.0 | 6 votes |
/** * Returns the Lucene query for the given locale and query text. * * @param searchString * the text query for the Lucene query parser * @param mId * the marketplace id * @param locale * the locale for the analyzer to use * @param isDefaultLocaleHandling * @return the Lucene query for the given locale and query text * @throws ParseException * in case the query cannot be parsed */ private org.apache.lucene.search.Query getLuceneQuery(String searchString, String mId, String locale, boolean isDefaultLocaleHandling) throws SyntaxError, QueryNodeException { // construct wildcard query for the actual search part org.apache.lucene.search.Query textQuery = LuceneQueryBuilder .getServiceQuery(searchString, locale, DEFAULT_LOCALE, isDefaultLocaleHandling); // build mId part TermQuery mIdQuery = new TermQuery(new Term(ProductClassBridge.MP_ID, QueryParser.escape(mId).toLowerCase())); // now construct final query BooleanQuery query = new BooleanQuery(); query.add(mIdQuery, Occur.MUST); query.add(textQuery, Occur.MUST); return query; }
Example #12
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 #13
Source File: RegexLiterals.java From onedev with MIT License | 6 votes |
/** * @param fieldName * @param gramSize * @return * @throws TooGeneralQueryException */ public Query asNGramQuery(String fieldName, int gramSize) throws TooGeneralQueryException { BooleanQuery.Builder orQueryBuilder = new BooleanQuery.Builder(); for (List<LeafLiterals> row: rows) { BooleanQuery.Builder andQueryBuilder = new BooleanQuery.Builder(); for (LeafLiterals literals: row) { if (literals.getLiteral() != null && literals.getLiteral().length()>=NGRAM_SIZE) andQueryBuilder.add(new NGramLuceneQuery(fieldName, literals.getLiteral(), gramSize), Occur.MUST); } BooleanQuery andQuery = andQueryBuilder.build(); if (andQuery.clauses().size() != 0) orQueryBuilder.add(andQuery, Occur.SHOULD); } BooleanQuery orQuery = orQueryBuilder.build(); if (orQuery.clauses().size() != 0) return orQuery; else throw new TooGeneralQueryException(); }
Example #14
Source File: TestSimpleQueryParser.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testComplex03() throws Exception { BooleanQuery.Builder expected = new BooleanQuery.Builder(); BooleanQuery.Builder inner = new BooleanQuery.Builder(); BooleanQuery.Builder inner2 = new BooleanQuery.Builder(); inner2.add(new TermQuery(new Term("field", "star")), Occur.MUST); inner2.add(new TermQuery(new Term("field", "wars")), Occur.MUST); inner.add(inner2.build(), Occur.SHOULD); inner.add(new TermQuery(new Term("field", "empire")), Occur.SHOULD); inner.add(new TermQuery(new Term("field", "strikes")), Occur.SHOULD); expected.add(inner.build(), Occur.MUST); expected.add(new TermQuery(new Term("field", "back")), Occur.MUST); assertEquals(expected.build(), parse("star wars | empire | strikes back")); assertEquals(expected.build(), parse("star + wars|empire | strikes + back")); assertEquals(expected.build(), parse("star + | wars | ----empire | + --strikes + | --back \\")); }
Example #15
Source File: TestBoolean2ScorerSupplier.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testProhibitedLeadCost() throws IOException { Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class); for (Occur occur : Occur.values()) { subs.put(occur, new ArrayList<>()); } // The MUST_NOT clause is called with the same lead cost as the MUST clause subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42)); subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 42)); new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect subs.get(Occur.MUST).clear(); subs.get(Occur.MUST_NOT).clear(); subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42)); subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(80, 42)); new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect subs.get(Occur.MUST).clear(); subs.get(Occur.MUST_NOT).clear(); subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 20)); subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 20)); new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(20); // triggers assertions as a side-effect }
Example #16
Source File: QueryUtils.java From lucene-solr with Apache License 2.0 | 6 votes |
/** * Combines a scoring query with a non-scoring (filter) query. * If both parameters are null then return a {@link MatchAllDocsQuery}. * If only {@code scoreQuery} is present then return it. * If only {@code filterQuery} is present then return it wrapped with constant scoring. * If neither are null then we combine with a BooleanQuery. */ public static Query combineQueryAndFilter(Query scoreQuery, Query filterQuery) { // check for *:* is simple and avoids needless BooleanQuery wrapper even though BQ.rewrite optimizes this away if (scoreQuery == null || scoreQuery instanceof MatchAllDocsQuery) { if (filterQuery == null) { return new MatchAllDocsQuery(); // default if nothing -- match everything } else { return new ConstantScoreQuery(filterQuery); } } else { if (filterQuery == null || filterQuery instanceof MatchAllDocsQuery) { return scoreQuery; } else { return new BooleanQuery.Builder() .add(scoreQuery, Occur.MUST) .add(filterQuery, Occur.FILTER) .build(); } } }
Example #17
Source File: TestBooleanRewrites.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testRemoveMatchAllFilter() throws IOException { IndexSearcher searcher = newSearcher(new MultiReader()); BooleanQuery bq = new BooleanQuery.Builder() .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new MatchAllDocsQuery(), Occur.FILTER) .build(); assertEquals(new TermQuery(new Term("foo", "bar")), searcher.rewrite(bq)); bq = new BooleanQuery.Builder() .setMinimumNumberShouldMatch(random().nextInt(5)) .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new TermQuery(new Term("foo", "baz")), Occur.MUST) .add(new MatchAllDocsQuery(), Occur.FILTER) .build(); BooleanQuery expected = new BooleanQuery.Builder() .setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch()) .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new TermQuery(new Term("foo", "baz")), Occur.MUST) .build(); assertEquals(expected, searcher.rewrite(bq)); }
Example #18
Source File: TestMultiFieldQueryParser.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testStaticMethod2Old() throws ParseException { String[] fields = {"b", "t"}; //int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD}; BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT}; Query q = MultiFieldQueryParser.parse("one", fields, flags, new MockAnalyzer(random()));//, 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 #19
Source File: TestBooleanRewrites.java From lucene-solr with Apache License 2.0 | 6 votes |
private Query randomBooleanQuery() { if (random().nextInt(10) == 0) { return new BoostQuery(randomBooleanQuery(), TestUtil.nextInt(random(), 1, 10)); } final int numClauses = random().nextInt(5); BooleanQuery.Builder b = new BooleanQuery.Builder(); int numShoulds = 0; for (int i = 0; i < numClauses; ++i) { final Occur occur = Occur.values()[random().nextInt(Occur.values().length)]; if (occur == Occur.SHOULD) { numShoulds++; } final Query query = randomQuery(); b.add(query, occur); } b.setMinimumNumberShouldMatch(random().nextBoolean() ? 0 : TestUtil.nextInt(random(), 0, numShoulds + 1)); return b.build(); }
Example #20
Source File: PercolatorService.java From Elasticsearch with Apache License 2.0 | 6 votes |
private void queryBasedPercolating(Engine.Searcher percolatorSearcher, PercolateContext context, QueryCollector percolateCollector) throws IOException { Query percolatorTypeFilter = context.indexService().mapperService().documentMapper(TYPE_NAME).typeFilter(); final Query filter; if (context.aliasFilter() != null) { BooleanQuery.Builder booleanFilter = new BooleanQuery.Builder(); booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST); booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST); filter = booleanFilter.build(); } else { filter = percolatorTypeFilter; } Query query = Queries.filtered(context.percolateQuery(), filter); percolatorSearcher.searcher().search(query, percolateCollector); percolateCollector.aggregatorCollector.postCollection(); if (context.aggregations() != null) { aggregationPhase.execute(context); } }
Example #21
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 #22
Source File: ZipscriptQueryExtension.java From drftpd with GNU General Public License v2.0 | 6 votes |
@Override public void addQueryTerms(BooleanQuery query, AdvancedSearchParams params) { try { ZipscriptQueryParams queryParams = params.getExtensionData(ZipscriptQueryParams.ZIPSCRIPTQUERYPARAMS); if (queryParams.getMinPresent() != null || queryParams.getMaxPresent() != null) { Query presentQuery = NumericRangeQuery.newIntRange("present", queryParams.getMinPresent(), queryParams.getMaxPresent(), true, true); query.add(presentQuery, Occur.MUST); } if (queryParams.getMinMissing() != null || queryParams.getMaxMissing() != null) { Query missingQuery = NumericRangeQuery.newIntRange("missing", queryParams.getMinMissing(), queryParams.getMaxMissing(), true, true); query.add(missingQuery, Occur.MUST); } if (queryParams.getMinPercent() != null || queryParams.getMaxPercent() != null) { Query percentQuery = NumericRangeQuery.newIntRange("percent", queryParams.getMinPercent(), queryParams.getMaxPercent(), true, true); query.add(percentQuery, Occur.MUST); } } catch (KeyNotFoundException e) { // No MP3 terms to include, return without amending query } }
Example #23
Source File: TestBooleanQuery.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testBooleanSpanQuery() throws Exception { boolean failed = false; int hits = 0; Directory directory = newDirectory(); Analyzer indexerAnalyzer = new MockAnalyzer(random()); IndexWriterConfig config = new IndexWriterConfig(indexerAnalyzer); IndexWriter writer = new IndexWriter(directory, config); String FIELD = "content"; Document d = new Document(); d.add(new TextField(FIELD, "clockwork orange", Field.Store.YES)); writer.addDocument(d); writer.close(); IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher searcher = newSearcher(indexReader); BooleanQuery.Builder query = new BooleanQuery.Builder(); SpanQuery sq1 = new SpanTermQuery(new Term(FIELD, "clockwork")); SpanQuery sq2 = new SpanTermQuery(new Term(FIELD, "clckwork")); query.add(sq1, BooleanClause.Occur.SHOULD); query.add(sq2, BooleanClause.Occur.SHOULD); TopScoreDocCollector collector = TopScoreDocCollector.create(1000, Integer.MAX_VALUE); searcher.search(query.build(), collector); hits = collector.topDocs().scoreDocs.length; for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs){ System.out.println(scoreDoc.doc); } indexReader.close(); assertEquals("Bug in boolean query composed of span queries", failed, false); assertEquals("Bug in boolean query composed of span queries", hits, 1); directory.close(); }
Example #24
Source File: SuperParserTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
@Test public void test2() throws ParseException { Query query = parser.parse("<a.c:c a.d:d>"); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(new TermQuery(new Term("a.c", "c")), Occur.SHOULD); booleanQuery.add(new TermQuery(new Term("a.d", "d")), Occur.SHOULD); SuperQuery superQuery = new SuperQuery(booleanQuery, ScoreType.SUPER, new Term("_primedoc_")); assertQuery(superQuery, query); }
Example #25
Source File: LuceneQueryBuilderTest.java From querqy with Apache License 2.0 | 5 votes |
@Test public void test03() throws Exception { float tie = (float) Math.random(); Query q = build("a b", tie, "f1"); assertThat(q, bq(1f, dtq(Occur.SHOULD, 1f, "f1", "a"), dtq(Occur.SHOULD, 1f, "f1", "b") )); }
Example #26
Source File: CurrencyFieldType.java From lucene-solr with Apache License 2.0 | 5 votes |
private Query getRangeQueryInternal(QParser parser, SchemaField field, final CurrencyValue p1, final CurrencyValue p2, final boolean minInclusive, final boolean maxInclusive) { String currencyCode = (p1 != null) ? p1.getCurrencyCode() : (p2 != null) ? p2.getCurrencyCode() : defaultCurrency; // ValueSourceRangeFilter doesn't check exists(), so we have to final Query docsWithValues = new DocValuesFieldExistsQuery(getAmountField(field).getName()); final Query vsRangeFilter = new ValueSourceRangeFilter (new RawCurrencyValueSource(field, currencyCode, parser), p1 == null ? null : p1.getAmount() + "", p2 == null ? null : p2.getAmount() + "", minInclusive, maxInclusive); return new ConstantScoreQuery(new BooleanQuery.Builder() .add(docsWithValues, Occur.FILTER) .add(vsRangeFilter, Occur.FILTER).build()); }
Example #27
Source File: TestLRUQueryCache.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testQueryNotSuitedForCaching() throws IOException { Directory dir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE); RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc); w.addDocument(new Document()); DirectoryReader reader = w.getReader(); IndexSearcher searcher = newSearcher(reader); searcher.setQueryCachingPolicy(ALWAYS_CACHE); LRUQueryCache cache = new LRUQueryCache(2, 10000, context -> true, Float.POSITIVE_INFINITY); searcher.setQueryCache(cache); assertEquals(0, searcher.count(new NoCacheQuery())); assertEquals(0, cache.getCacheCount()); // BooleanQuery wrapping an uncacheable query should also not be cached BooleanQuery bq = new BooleanQuery.Builder() .add(new NoCacheQuery(), Occur.MUST) .add(new TermQuery(new Term("field", "term")), Occur.MUST).build(); assertEquals(0, searcher.count(bq)); assertEquals(0, cache.getCacheCount()); reader.close(); w.close(); dir.close(); }
Example #28
Source File: TestBooleanQueryVisitSubscorers.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testMinShouldMatchMatches() throws IOException { BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(new TermQuery(new Term(F1, "lucene")), Occur.SHOULD); bq.add(new TermQuery(new Term(F2, "lucene")), Occur.SHOULD); bq.add(new PhraseQuery(F2, "search", "library"), Occur.SHOULD); bq.setMinimumNumberShouldMatch(2); Weight w = scorerSearcher.createWeight(scorerSearcher.rewrite(bq.build()), ScoreMode.COMPLETE, 1); Scorer s = w.scorer(reader.leaves().get(0)); assertEquals(0, s.iterator().nextDoc()); assertEquals(2, s.getChildren().size()); }
Example #29
Source File: SynonymQueryNodeBuilder.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public Query build(QueryNode queryNode) throws QueryNodeException { // TODO: use SynonymQuery instead SynonymQueryNode node = (SynonymQueryNode) queryNode; BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (QueryNode child : node.getChildren()) { Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); if (obj != null) { Query query = (Query) obj; builder.add(query, Occur.SHOULD); } } return builder.build(); }
Example #30
Source File: SearchEquivalenceTestBase.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Asserts that the documents returned by <code>q1</code> * are a subset of those returned by <code>q2</code>. * * Both queries will be filtered by <code>filter</code> */ protected void assertSubsetOf(Query q1, Query q2, Query filter) throws Exception { QueryUtils.check(q1); QueryUtils.check(q2); if (filter != null) { q1 = new BooleanQuery.Builder() .add(q1, Occur.MUST) .add(filter, Occur.FILTER) .build(); q2 = new BooleanQuery.Builder() .add(q2, Occur.MUST) .add(filter, Occur.FILTER) .build(); } // we test both INDEXORDER and RELEVANCE because we want to test needsScores=true/false for (Sort sort : new Sort[] { Sort.INDEXORDER, Sort.RELEVANCE }) { // not efficient, but simple! TopDocs td1 = s1.search(q1, reader.maxDoc(), sort); TopDocs td2 = s2.search(q2, reader.maxDoc(), sort); assertTrue("too many hits: " + td1.totalHits.value + " > " + td2.totalHits.value, td1.totalHits.value <= td2.totalHits.value); // fill the superset into a bitset BitSet bitset = new BitSet(); for (int i = 0; i < td2.scoreDocs.length; i++) { bitset.set(td2.scoreDocs[i].doc); } // check in the subset, that every bit was set by the super for (int i = 0; i < td1.scoreDocs.length; i++) { assertTrue(bitset.get(td1.scoreDocs[i].doc)); } } }