use of org.apache.lucene.search.MultiPhraseQuery in project elasticsearch by elastic.
the class CustomFieldQuery method convertMultiPhraseQuery.
private void convertMultiPhraseQuery(int currentPos, int[] termsIdx, MultiPhraseQuery orig, Term[][] terms, int[] pos, IndexReader reader, Collection<Query> flatQueries) throws IOException {
if (currentPos == 0) {
// if we have more than 16 terms
int numTerms = 0;
for (Term[] currentPosTerm : terms) {
numTerms += currentPosTerm.length;
}
if (numTerms > 16) {
for (Term[] currentPosTerm : terms) {
for (Term term : currentPosTerm) {
super.flatten(new TermQuery(term), reader, flatQueries, 1F);
}
}
return;
}
}
/*
* we walk all possible ways and for each path down the MPQ we create a PhraseQuery this is what FieldQuery supports.
* It seems expensive but most queries will pretty small.
*/
if (currentPos == terms.length) {
PhraseQuery.Builder queryBuilder = new PhraseQuery.Builder();
queryBuilder.setSlop(orig.getSlop());
for (int i = 0; i < termsIdx.length; i++) {
queryBuilder.add(terms[i][termsIdx[i]], pos[i]);
}
Query query = queryBuilder.build();
this.flatten(query, reader, flatQueries, 1F);
} else {
Term[] t = terms[currentPos];
for (int i = 0; i < t.length; i++) {
termsIdx[currentPos] = i;
convertMultiPhraseQuery(currentPos + 1, termsIdx, orig, terms, pos, reader, flatQueries);
}
}
}
use of org.apache.lucene.search.MultiPhraseQuery in project elasticsearch by elastic.
the class QueryAnalyzerTests method testExtractQueryMetadata_multiPhraseQuery.
public void testExtractQueryMetadata_multiPhraseQuery() {
MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery.Builder().add(new Term("_field", "_long_term")).add(new Term[] { new Term("_field", "_long_term"), new Term("_field", "_term") }).add(new Term[] { new Term("_field", "_long_term"), new Term("_field", "_very_long_term") }).add(new Term[] { new Term("_field", "_very_long_term") }).build();
Result result = analyze(multiPhraseQuery);
assertThat(result.verified, is(false));
List<Term> terms = new ArrayList<>(result.terms);
assertThat(terms.size(), equalTo(1));
assertThat(terms.get(0).field(), equalTo("_field"));
assertThat(terms.get(0).bytes().utf8ToString(), equalTo("_very_long_term"));
}
use of org.apache.lucene.search.MultiPhraseQuery in project lucene-solr by apache.
the class MultiPhraseQueryNodeBuilder method build.
@Override
public MultiPhraseQuery build(QueryNode queryNode) throws QueryNodeException {
MultiPhraseQueryNode phraseNode = (MultiPhraseQueryNode) queryNode;
MultiPhraseQuery.Builder phraseQueryBuilder = new MultiPhraseQuery.Builder();
List<QueryNode> children = phraseNode.getChildren();
if (children != null) {
TreeMap<Integer, List<Term>> positionTermMap = new TreeMap<>();
for (QueryNode child : children) {
FieldQueryNode termNode = (FieldQueryNode) child;
TermQuery termQuery = (TermQuery) termNode.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
List<Term> termList = positionTermMap.get(termNode.getPositionIncrement());
if (termList == null) {
termList = new LinkedList<>();
positionTermMap.put(termNode.getPositionIncrement(), termList);
}
termList.add(termQuery.getTerm());
}
for (int positionIncrement : positionTermMap.keySet()) {
List<Term> termList = positionTermMap.get(positionIncrement);
phraseQueryBuilder.add(termList.toArray(new Term[termList.size()]), positionIncrement);
}
}
return phraseQueryBuilder.build();
}
use of org.apache.lucene.search.MultiPhraseQuery in project lucene-solr by apache.
the class SlopQueryNodeBuilder method build.
@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;
}
use of org.apache.lucene.search.MultiPhraseQuery in project lucene-solr by apache.
the class PayloadSpanUtil method queryToSpanQuery.
private void queryToSpanQuery(Query query, Collection<byte[]> payloads) throws IOException {
if (query instanceof BooleanQuery) {
for (BooleanClause clause : (BooleanQuery) query) {
if (!clause.isProhibited()) {
queryToSpanQuery(clause.getQuery(), payloads);
}
}
} else if (query instanceof PhraseQuery) {
Term[] phraseQueryTerms = ((PhraseQuery) query).getTerms();
SpanQuery[] clauses = new SpanQuery[phraseQueryTerms.length];
for (int i = 0; i < phraseQueryTerms.length; i++) {
clauses[i] = new SpanTermQuery(phraseQueryTerms[i]);
}
int slop = ((PhraseQuery) query).getSlop();
boolean inorder = false;
if (slop == 0) {
inorder = true;
}
SpanNearQuery sp = new SpanNearQuery(clauses, slop, inorder);
getPayloads(payloads, sp);
} else if (query instanceof TermQuery) {
SpanTermQuery stq = new SpanTermQuery(((TermQuery) query).getTerm());
getPayloads(payloads, stq);
} else if (query instanceof SpanQuery) {
getPayloads(payloads, (SpanQuery) query);
} else if (query instanceof DisjunctionMaxQuery) {
for (Iterator<Query> iterator = ((DisjunctionMaxQuery) query).iterator(); iterator.hasNext(); ) {
queryToSpanQuery(iterator.next(), payloads);
}
} else if (query instanceof MultiPhraseQuery) {
final MultiPhraseQuery mpq = (MultiPhraseQuery) query;
final Term[][] termArrays = mpq.getTermArrays();
final int[] positions = mpq.getPositions();
if (positions.length > 0) {
int maxPosition = positions[positions.length - 1];
for (int i = 0; i < positions.length - 1; ++i) {
if (positions[i] > maxPosition) {
maxPosition = positions[i];
}
}
@SuppressWarnings({ "rawtypes", "unchecked" }) final List<Query>[] disjunctLists = new List[maxPosition + 1];
int distinctPositions = 0;
for (int i = 0; i < termArrays.length; ++i) {
final Term[] termArray = termArrays[i];
List<Query> disjuncts = disjunctLists[positions[i]];
if (disjuncts == null) {
disjuncts = (disjunctLists[positions[i]] = new ArrayList<>(termArray.length));
++distinctPositions;
}
for (final Term term : termArray) {
disjuncts.add(new SpanTermQuery(term));
}
}
int positionGaps = 0;
int position = 0;
final SpanQuery[] clauses = new SpanQuery[distinctPositions];
for (int i = 0; i < disjunctLists.length; ++i) {
List<Query> disjuncts = disjunctLists[i];
if (disjuncts != null) {
clauses[position++] = new SpanOrQuery(disjuncts.toArray(new SpanQuery[disjuncts.size()]));
} else {
++positionGaps;
}
}
final int slop = mpq.getSlop();
final boolean inorder = (slop == 0);
SpanNearQuery sp = new SpanNearQuery(clauses, slop + positionGaps, inorder);
getPayloads(payloads, sp);
}
}
}
Aggregations