Java Code Examples for org.apache.lucene.search.PhraseQuery#getPositions()
The following examples show how to use
org.apache.lucene.search.PhraseQuery#getPositions() .
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: 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 3
Source File: TestQPHelper.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testPositionIncrement() throws Exception { StandardQueryParser qp = new StandardQueryParser(); qp.setAnalyzer( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET)); qp.setEnablePositionIncrements(true); String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\""; // 0 2 5 7 8 int expectedPositions[] = { 1, 3, 4, 6, 9 }; PhraseQuery pq = (PhraseQuery) qp.parse(qtxt, "a"); // System.out.println("Query text: "+qtxt); // System.out.println("Result: "+pq); Term t[] = pq.getTerms(); int pos[] = pq.getPositions(); for (int i = 0; i < t.length; i++) { // System.out.println(i+". "+t[i]+" pos: "+pos[i]); assertEquals("term " + i + " = " + t[i] + " has wrong term-position!", expectedPositions[i], pos[i]); } }
Example 4
Source File: QueryParserTestBase.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testPositionIncrement() throws Exception { CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET)); qp.setEnablePositionIncrements(true); String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\""; // 0 2 5 7 8 int expectedPositions[] = {1,3,4,6,9}; PhraseQuery pq = (PhraseQuery) getQuery(qtxt,qp); //System.out.println("Query text: "+qtxt); //System.out.println("Result: "+pq); Term t[] = pq.getTerms(); int pos[] = pq.getPositions(); for (int i = 0; i < t.length; i++) { //System.out.println(i+". "+t[i]+" pos: "+pos[i]); assertEquals("term "+i+" = "+t[i]+" has wrong term-position!",expectedPositions[i],pos[i]); } }
Example 5
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) { PhraseQuery phraseQuery = (PhraseQuery) query; SpanNearQuery.Builder builder = new SpanNearQuery.Builder(phraseQuery.getTerms()[0].field(), true); int i = 0; int position = -1; for (Term term : phraseQuery.getTerms()) { if (i > 0) { int gap = (phraseQuery.getPositions()[i] - position) - 1; if (gap > 0) { builder.addGap(gap); } } position = phraseQuery.getPositions()[i]; builder.addClause(new CustomSpanPayloadCheckQuery(new SpanTermQuery(term), Collections.singletonList(null))); i++; } return builder.setSlop(phraseQuery.getSlop()).build(); }
Example 6
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 7
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 8
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 9
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 10
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; } }