Java Code Examples for org.apache.lucene.search.MultiPhraseQuery#Builder
The following examples show how to use
org.apache.lucene.search.MultiPhraseQuery#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: HighlighterTest.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testQueryScorerMultiPhraseQueryHighlightingWithGap() throws Exception { MultiPhraseQuery.Builder mpqb = new MultiPhraseQuery.Builder(); /* * The toString of MultiPhraseQuery doesn't work so well with these * out-of-order additions, but the Query itself seems to match accurately. */ mpqb.add(new Term[] { new Term(FIELD_NAME, "wordz") }, 2); mpqb.add(new Term[] { new Term(FIELD_NAME, "wordx") }, 0); doSearching(mpqb.build()); final int maxNumFragmentsRequired = 1; final int expectedHighlights = 2; assertExpectedHighlightCount(maxNumFragmentsRequired, expectedHighlights); }
Example 2
Source File: TestMultiPhraseQueryParsing.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testMultiPhraseQueryParsing() throws Exception { TokenAndPos[] INCR_0_QUERY_TOKENS_AND = new TokenAndPos[]{ new TokenAndPos("a", 0), new TokenAndPos("1", 0), new TokenAndPos("b", 1), new TokenAndPos("1", 1), new TokenAndPos("c", 2) }; QueryParser qp = new QueryParser("field", new CannedAnalyzer(INCR_0_QUERY_TOKENS_AND)); Query q = qp.parse("\"this text is acually ignored\""); assertTrue("wrong query type!", q instanceof MultiPhraseQuery); MultiPhraseQuery.Builder multiPhraseQueryBuilder = new MultiPhraseQuery.Builder(); multiPhraseQueryBuilder.add(new Term[]{ new Term("field", "a"), new Term("field", "1") }, -1); multiPhraseQueryBuilder.add(new Term[]{ new Term("field", "b"), new Term("field", "1") }, 0); multiPhraseQueryBuilder.add(new Term[]{ new Term("field", "c") }, 1); assertEquals(multiPhraseQueryBuilder.build(), q); }
Example 3
Source File: HighlighterTest.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testQueryScorerMultiPhraseQueryHighlighting() throws Exception { MultiPhraseQuery.Builder mpqb = new MultiPhraseQuery.Builder(); mpqb.add(new Term[] { new Term(FIELD_NAME, "wordx"), new Term(FIELD_NAME, "wordb") }); mpqb.add(new Term(FIELD_NAME, "wordy")); doSearching(mpqb.build()); final int maxNumFragmentsRequired = 2; assertExpectedHighlightCount(maxNumFragmentsRequired, 6); }
Example 4
Source File: QueryBuilder.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Creates complex phrase query from the cached tokenstream contents */ protected Query analyzeMultiPhrase(String field, TokenStream stream, int slop) throws IOException { MultiPhraseQuery.Builder mpqb = newMultiPhraseQueryBuilder(); mpqb.setSlop(slop); TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class); PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class); int position = -1; List<Term> multiTerms = new ArrayList<>(); stream.reset(); while (stream.incrementToken()) { int positionIncrement = posIncrAtt.getPositionIncrement(); if (positionIncrement > 0 && multiTerms.size() > 0) { if (enablePositionIncrements) { mpqb.add(multiTerms.toArray(new Term[0]), position); } else { mpqb.add(multiTerms.toArray(new Term[0])); } multiTerms.clear(); } position += positionIncrement; multiTerms.add(new Term(field, termAtt.getBytesRef())); } if (enablePositionIncrements) { mpqb.add(multiTerms.toArray(new Term[0]), position); } else { mpqb.add(multiTerms.toArray(new Term[0])); } return mpqb.build(); }
Example 5
Source File: TestQueryBuilder.java From lucene-solr with Apache License 2.0 | 5 votes |
/** forms multiphrase query */ public void testSynonymsPhrase() throws Exception { MultiPhraseQuery.Builder expectedBuilder = new MultiPhraseQuery.Builder(); expectedBuilder.add(new Term("field", "old")); expectedBuilder.add(new Term[] { new Term("field", "dogs"), new Term("field", "dog") }); QueryBuilder builder = new QueryBuilder(new MockSynonymAnalyzer()); assertEquals(expectedBuilder.build(), builder.createPhraseQuery("field", "old dogs")); }
Example 6
Source File: TestQueryBuilder.java From lucene-solr with Apache License 2.0 | 5 votes |
/** forms multiphrase query */ public void testCJKSynonymsPhrase() throws Exception { MultiPhraseQuery.Builder expectedBuilder = new MultiPhraseQuery.Builder(); expectedBuilder.add(new Term("field", "中")); expectedBuilder.add(new Term[] { new Term("field", "国"), new Term("field", "國")}); QueryBuilder builder = new QueryBuilder(new MockCJKSynonymAnalyzer()); assertEquals(expectedBuilder.build(), builder.createPhraseQuery("field", "中国")); expectedBuilder.setSlop(3); assertEquals(expectedBuilder.build(), builder.createPhraseQuery("field", "中国", 3)); }
Example 7
Source File: StringFieldType.java From crate with Apache License 2.0 | 5 votes |
@Override public Query multiPhraseQuery(String field, TokenStream stream, int slop, boolean enablePositionIncrements) throws IOException { MultiPhraseQuery.Builder mpqb = new MultiPhraseQuery.Builder(); mpqb.setSlop(slop); TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class); PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class); int position = -1; List<Term> multiTerms = new ArrayList<>(); stream.reset(); while (stream.incrementToken()) { int positionIncrement = posIncrAtt.getPositionIncrement(); if (positionIncrement > 0 && multiTerms.size() > 0) { if (enablePositionIncrements) { mpqb.add(multiTerms.toArray(new Term[0]), position); } else { mpqb.add(multiTerms.toArray(new Term[0])); } multiTerms.clear(); } position += positionIncrement; multiTerms.add(new Term(field, termAtt.getBytesRef())); } if (enablePositionIncrements) { mpqb.add(multiTerms.toArray(new Term[0]), position); } else { mpqb.add(multiTerms.toArray(new Term[0])); } return mpqb.build(); }
Example 8
Source File: MultiPhrasePrefixQuery.java From crate with Apache License 2.0 | 5 votes |
@Override public Query rewrite(IndexReader reader) throws IOException { Query rewritten = super.rewrite(reader); if (rewritten != this) { return rewritten; } if (termArrays.isEmpty()) { return new MatchNoDocsQuery(); } MultiPhraseQuery.Builder query = new MultiPhraseQuery.Builder(); query.setSlop(slop); int sizeMinus1 = termArrays.size() - 1; for (int i = 0; i < sizeMinus1; i++) { query.add(termArrays.get(i), positions.get(i)); } Term[] suffixTerms = termArrays.get(sizeMinus1); int position = positions.get(sizeMinus1); ObjectHashSet<Term> terms = new ObjectHashSet<>(); for (Term term : suffixTerms) { getPrefixTerms(terms, term, reader); if (terms.size() > maxExpansions) { break; } } if (terms.isEmpty()) { if (sizeMinus1 == 0) { // no prefix and the phrase query is empty return Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName()); } // if the terms does not exist we could return a MatchNoDocsQuery but this would break the unified highlighter // which rewrites query with an empty reader. return new BooleanQuery.Builder() .add(query.build(), BooleanClause.Occur.MUST) .add(Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName()), BooleanClause.Occur.MUST).build(); } query.add(terms.toArray(Term.class), position); return query.build(); }
Example 9
Source File: QueryBuilder.java From lucene-solr with Apache License 2.0 | 2 votes |
/** * Builds a new MultiPhraseQuery instance. * <p> * This is intended for subclasses that wish to customize the generated queries. * @return new MultiPhraseQuery instance */ protected MultiPhraseQuery.Builder newMultiPhraseQueryBuilder() { return new MultiPhraseQuery.Builder(); }