Java Code Examples for org.apache.lucene.search.PhraseQuery#Builder
The following examples show how to use
org.apache.lucene.search.PhraseQuery#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: MapperQueryParser.java From Elasticsearch with Apache License 2.0 | 6 votes |
private Query applySlop(Query q, int slop) { if (q instanceof PhraseQuery) { PhraseQuery pq = (PhraseQuery) q; PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.setSlop(slop); final Term[] terms = pq.getTerms(); final int[] positions = pq.getPositions(); for (int i = 0; i < terms.length; ++i) { builder.add(terms[i], positions[i]); } pq = builder.build(); pq.setBoost(q.getBoost()); return pq; } else if (q instanceof MultiPhraseQuery) { ((MultiPhraseQuery) q).setSlop(slop); return q; } else { return q; } }
Example 2
Source File: StringFieldType.java From crate with Apache License 2.0 | 6 votes |
@Override public Query phraseQuery(String field, TokenStream stream, int slop, boolean enablePosIncrements) throws IOException { PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.setSlop(slop); TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class); PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class); int position = -1; stream.reset(); while (stream.incrementToken()) { if (enablePosIncrements) { position += posIncrAtt.getPositionIncrement(); } else { position += 1; } builder.add(new Term(field, termAtt.getBytesRef()), position); } return builder.build(); }
Example 3
Source File: LumongoMultiFieldQueryParser.java From lumongo with Apache License 2.0 | 6 votes |
private Query applySlop(Query q, int slop) { if (q instanceof PhraseQuery) { PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.setSlop(slop); PhraseQuery pq = (PhraseQuery) q; org.apache.lucene.index.Term[] terms = pq.getTerms(); int[] positions = pq.getPositions(); for (int i = 0; i < terms.length; ++i) { builder.add(terms[i], positions[i]); } q = builder.build(); } else if (q instanceof MultiPhraseQuery) { MultiPhraseQuery mpq = (MultiPhraseQuery) q; if (slop != mpq.getSlop()) { q = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build(); } } return q; }
Example 4
Source File: QueryBuilder.java From lucene-solr with Apache License 2.0 | 6 votes |
/** * Creates simple phrase query from the cached tokenstream contents */ protected Query analyzePhrase(String field, TokenStream stream, int slop) throws IOException { PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.setSlop(slop); TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class); BoostAttribute boostAtt = stream.addAttribute(BoostAttribute.class); PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class); int position = -1; float phraseBoost = DEFAULT_BOOST; stream.reset(); while (stream.incrementToken()) { if (enablePositionIncrements) { position += posIncrAtt.getPositionIncrement(); } else { position += 1; } builder.add(new Term(field, termAtt.getBytesRef()), position); phraseBoost *= boostAtt.getBoost(); } PhraseQuery query = builder.build(); if (phraseBoost == DEFAULT_BOOST) { return query; } return new BoostQuery(query, phraseBoost); }
Example 5
Source File: MultiFieldQueryParser.java From lucene-solr with Apache License 2.0 | 6 votes |
private Query applySlop(Query q, int slop) { if (q instanceof PhraseQuery) { PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.setSlop(slop); PhraseQuery pq = (PhraseQuery) q; org.apache.lucene.index.Term[] terms = pq.getTerms(); int[] positions = pq.getPositions(); for (int i = 0; i < terms.length; ++i) { builder.add(terms[i], positions[i]); } q = builder.build(); } else if (q instanceof MultiPhraseQuery) { MultiPhraseQuery mpq = (MultiPhraseQuery)q; if (slop != mpq.getSlop()) { q = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build(); } } return q; }
Example 6
Source File: ShingleAnalyzerWrapperTest.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testShingleAnalyzerWrapperPhraseQuery() throws Exception { PhraseQuery.Builder builder = new PhraseQuery.Builder(); try (TokenStream ts = analyzer.tokenStream("content", "this sentence")) { int j = -1; PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class); CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class); ts.reset(); while (ts.incrementToken()) { j += posIncrAtt.getPositionIncrement(); String termText = termAtt.toString(); builder.add(new Term("content", termText), j); } ts.end(); } PhraseQuery q = builder.build(); ScoreDoc[] hits = searcher.search(q, 1000).scoreDocs; int[] ranks = new int[] { 0 }; compareRanks(hits, ranks); }
Example 7
Source File: SlopQueryNodeBuilder.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public Query build(QueryNode queryNode) throws QueryNodeException { SlopQueryNode phraseSlopNode = (SlopQueryNode) queryNode; Query query = (Query) phraseSlopNode.getChild().getTag( QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); if (query instanceof PhraseQuery) { PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.setSlop(phraseSlopNode.getValue()); PhraseQuery pq = (PhraseQuery) query; org.apache.lucene.index.Term[] terms = pq.getTerms(); int[] positions = pq.getPositions(); for (int i = 0; i < terms.length; ++i) { builder.add(terms[i], positions[i]); } query = builder.build(); } else { MultiPhraseQuery mpq = (MultiPhraseQuery)query; int slop = phraseSlopNode.getValue(); if (slop != mpq.getSlop()) { query = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build(); } } return query; }
Example 8
Source File: QueryParserTestBase.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testPhraseQueryPositionIncrements() throws Exception { CharacterRunAutomaton stopStopList = new CharacterRunAutomaton(new RegExp("[sS][tT][oO][pP]").toAutomaton()); CommonQueryParserConfiguration qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false, stopStopList)); qp.setEnablePositionIncrements(true); PhraseQuery.Builder phraseQuery = new PhraseQuery.Builder(); phraseQuery.add(new Term("field", "1")); phraseQuery.add(new Term("field", "2"), 2); assertEquals(phraseQuery.build(), getQuery("\"1 stop 2\"",qp)); }
Example 9
Source File: TestIndexWriter.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testStopwordsPosIncHole() throws Exception { Directory dir = newDirectory(); Analyzer a = new Analyzer() { @Override protected TokenStreamComponents createComponents(String fieldName) { Tokenizer tokenizer = new MockTokenizer(); TokenStream stream = new MockTokenFilter(tokenizer, MockTokenFilter.ENGLISH_STOPSET); return new TokenStreamComponents(tokenizer, stream); } }; RandomIndexWriter iw = new RandomIndexWriter(random(), dir, a); Document doc = new Document(); doc.add(new TextField("body", "just a", Field.Store.NO)); doc.add(new TextField("body", "test of gaps", Field.Store.NO)); iw.addDocument(doc); IndexReader ir = iw.getReader(); iw.close(); IndexSearcher is = newSearcher(ir); PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.add(new Term("body", "just"), 0); builder.add(new Term("body", "test"), 2); PhraseQuery pq = builder.build(); // body:"just ? test" assertEquals(1, is.search(pq, 5).totalHits.value); ir.close(); dir.close(); }
Example 10
Source File: TestIndexWriter.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testStopwordsPosIncHole2() throws Exception { // use two stopfilters for testing here Directory dir = newDirectory(); final Automaton secondSet = Automata.makeString("foobar"); Analyzer a = new Analyzer() { @Override protected TokenStreamComponents createComponents(String fieldName) { Tokenizer tokenizer = new MockTokenizer(); TokenStream stream = new MockTokenFilter(tokenizer, MockTokenFilter.ENGLISH_STOPSET); stream = new MockTokenFilter(stream, new CharacterRunAutomaton(secondSet)); return new TokenStreamComponents(tokenizer, stream); } }; RandomIndexWriter iw = new RandomIndexWriter(random(), dir, a); Document doc = new Document(); doc.add(new TextField("body", "just a foobar", Field.Store.NO)); doc.add(new TextField("body", "test of gaps", Field.Store.NO)); iw.addDocument(doc); IndexReader ir = iw.getReader(); iw.close(); IndexSearcher is = newSearcher(ir); PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.add(new Term("body", "just"), 0); builder.add(new Term("body", "test"), 3); PhraseQuery pq = builder.build(); // body:"just ? ? test" assertEquals(1, is.search(pq, 5).totalHits.value); ir.close(); dir.close(); }
Example 11
Source File: TestQueryBuilder.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testPhraseQueryPositionIncrements() throws Exception { PhraseQuery.Builder pqBuilder = new PhraseQuery.Builder(); pqBuilder.add(new Term("field", "1"), 0); pqBuilder.add(new Term("field", "2"), 2); PhraseQuery expected = pqBuilder.build(); CharacterRunAutomaton stopList = new CharacterRunAutomaton(new RegExp("[sS][tT][oO][pP]").toAutomaton()); Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false, stopList); QueryBuilder builder = new QueryBuilder(analyzer); assertEquals(expected, builder.createPhraseQuery("field", "1 stop 2")); }
Example 12
Source File: SolrQueryParserBase.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Base implementation delegates to {@link #getFieldQuery(String,String,boolean,boolean)}. * This method may be overridden, for example, to return * a SpanNearQuery instead of a PhraseQuery. * */ protected Query getFieldQuery(String field, String queryText, int slop) throws SyntaxError { Query query = getFieldQuery(field, queryText, true, false); // only set slop of the phrase query was a result of this parser // and not a sub-parser. if (subQParser == null) { if (query instanceof PhraseQuery) { PhraseQuery pq = (PhraseQuery) query; Term[] terms = pq.getTerms(); int[] positions = pq.getPositions(); PhraseQuery.Builder builder = new PhraseQuery.Builder(); for (int i = 0; i < terms.length; ++i) { builder.add(terms[i], positions[i]); } builder.setSlop(slop); query = builder.build(); } else if (query instanceof MultiPhraseQuery) { MultiPhraseQuery mpq = (MultiPhraseQuery)query; if (slop != mpq.getSlop()) { query = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build(); } } } return query; }
Example 13
Source File: TestUnifiedHighlighterStrictPhrases.java From lucene-solr with Apache License 2.0 | 5 votes |
private PhraseQuery setSlop(PhraseQuery query, int slop) { PhraseQuery.Builder builder = new PhraseQuery.Builder(); Term[] terms = query.getTerms(); int[] positions = query.getPositions(); for (int i = 0; i < terms.length; i++) { builder.add(terms[i], positions[i]); } builder.setSlop(slop); return builder.build(); }
Example 14
Source File: LuceneQueryVisitor.java From cxf with Apache License 2.0 | 5 votes |
private Query newPhraseQuery(final String field, final String query) { if (queryBuilder != null) { return queryBuilder.createPhraseQuery(field, query); } PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.add(new Term(field, query)); return builder.build(); }
Example 15
Source File: NGramLuceneQuery.java From onedev with MIT License | 5 votes |
private static PhraseQuery build(String fieldName, String fieldValue, int gramSize) { Preconditions.checkArgument(fieldValue.length()>=gramSize); PhraseQuery.Builder builder = new PhraseQuery.Builder(); try (NGramTokenizer tokenizer = new NGramTokenizer(gramSize, gramSize)) { tokenizer.setReader(new StringReader(fieldValue.toLowerCase())); tokenizer.reset(); while (tokenizer.incrementToken()) { builder.add(new Term(fieldName, tokenizer.getAttribute(CharTermAttribute.class).toString())); } } catch (IOException e) { throw new RuntimeException(e); } return builder.build(); }
Example 16
Source File: PhraseQueryNodeBuilder.java From lucene-solr with Apache License 2.0 | 4 votes |
@Override public Query build(QueryNode queryNode) throws QueryNodeException { TokenizedPhraseQueryNode phraseNode = (TokenizedPhraseQueryNode) queryNode; PhraseQuery.Builder builder = new PhraseQuery.Builder(); List<QueryNode> children = phraseNode.getChildren(); if (children != null) { for (QueryNode child : children) { TermQuery termQuery = (TermQuery) child .getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); FieldQueryNode termNode = (FieldQueryNode) child; builder.add(termQuery.getTerm(), termNode.getPositionIncrement()); } } return builder.build(); }
Example 17
Source File: SimpleSloppyPhraseQueryMaker.java From lucene-solr with Apache License 2.0 | 4 votes |
@Override protected Query[] prepareQueries() throws Exception { // extract some 100 words from doc text to an array String words[]; ArrayList<String> w = new ArrayList<>(); StringTokenizer st = new StringTokenizer(SingleDocSource.DOC_TEXT); while (st.hasMoreTokens() && w.size()<100) { w.add(st.nextToken()); } words = w.toArray(new String[0]); // create queries (that would find stuff) with varying slops ArrayList<Query> queries = new ArrayList<>(); for (int slop=0; slop<8; slop++) { for (int qlen=2; qlen<6; qlen++) { for (int wd=0; wd<words.length-qlen-slop; wd++) { // ordered int remainedSlop = slop; int wind = wd; PhraseQuery.Builder builder = new PhraseQuery.Builder(); for (int i=0; i<qlen; i++) { builder.add(new Term(DocMaker.BODY_FIELD, words[wind++]), i); if (remainedSlop>0) { remainedSlop--; wind++; } } builder.setSlop(slop); PhraseQuery q = builder.build(); queries.add(q); // reversed remainedSlop = slop; wind = wd+qlen+remainedSlop-1; builder = new PhraseQuery.Builder(); for (int i=0; i<qlen; i++) { builder.add(new Term(DocMaker.BODY_FIELD, words[wind--]), i); if (remainedSlop>0) { remainedSlop--; wind--; } } builder.setSlop(slop + 2 * qlen); q = builder.build(); queries.add(q); } } } return queries.toArray(new Query[0]); }
Example 18
Source File: ExtendedDismaxQParser.java From lucene-solr with Apache License 2.0 | 4 votes |
private Query getQuery() { try { switch (type) { case FIELD: // fallthrough case PHRASE: Query query; if (val == null) { query = super.getFieldQuery(field, vals, false); } else { query = super.getFieldQuery(field, val, type == QType.PHRASE, false); } // Boolean query on a whitespace-separated string // If these were synonyms we would have a SynonymQuery if (query instanceof BooleanQuery) { if (type == QType.FIELD) { // Don't set mm for boolean query containing phrase queries BooleanQuery bq = (BooleanQuery) query; query = SolrPluginUtils.setMinShouldMatch(bq, minShouldMatch, false); } } else if (query instanceof PhraseQuery) { PhraseQuery pq = (PhraseQuery)query; if (minClauseSize > 1 && pq.getTerms().length < minClauseSize) return null; PhraseQuery.Builder builder = new PhraseQuery.Builder(); Term[] terms = pq.getTerms(); int[] positions = pq.getPositions(); for (int i = 0; i < terms.length; ++i) { builder.add(terms[i], positions[i]); } builder.setSlop(slop); query = builder.build(); } else if (query instanceof MultiPhraseQuery) { MultiPhraseQuery mpq = (MultiPhraseQuery)query; if (minClauseSize > 1 && mpq.getTermArrays().length < minClauseSize) return null; if (slop != mpq.getSlop()) { query = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build(); } } else if (query instanceof SpanQuery) { return query; } else if (minClauseSize > 1) { // if it's not a type of phrase query, it doesn't meet the minClauseSize requirements return null; } return query; case PREFIX: return super.getPrefixQuery(field, val); case WILDCARD: return super.getWildcardQuery(field, val); case FUZZY: return super.getFuzzyQuery(field, val, flt); case RANGE: return super.getRangeQuery(field, val, val2, bool, bool2); } return null; } catch (Exception e) { // an exception here is due to the field query not being compatible with the input text // for example, passing a string to a numeric field. return null; } }
Example 19
Source File: TestExtractors.java From lucene-solr with Apache License 2.0 | 3 votes |
public void testPhraseQueryExtractor() { PhraseQuery.Builder pq = new PhraseQuery.Builder(); pq.add(new Term("f", "hello")); pq.add(new Term("f", "encyclopedia")); Set<Term> expected = Collections.singleton(new Term("f", "encyclopedia")); assertEquals(expected, collectTerms(pq.build())); }