use of org.apache.lucene.search.PhraseQuery in project elasticsearch by elastic.
the class ElasticsearchQueryCachingPolicyTests method testDoesNotCacheTermQueries.
public void testDoesNotCacheTermQueries() throws IOException {
QueryCachingPolicy policy = QueryCachingPolicy.ALWAYS_CACHE;
assertTrue(policy.shouldCache(new TermQuery(new Term("foo", "bar"))));
assertTrue(policy.shouldCache(new PhraseQuery("foo", "bar", "baz")));
policy = new ElasticsearchQueryCachingPolicy(policy);
assertFalse(policy.shouldCache(new TermQuery(new Term("foo", "bar"))));
assertTrue(policy.shouldCache(new PhraseQuery("foo", "bar", "baz")));
}
use of org.apache.lucene.search.PhraseQuery in project elasticsearch by elastic.
the class ElasticsearchQueryCachingPolicyTests method testDoesNotPutTermQueriesIntoTheHistory.
public void testDoesNotPutTermQueriesIntoTheHistory() {
boolean[] used = new boolean[1];
QueryCachingPolicy policy = new QueryCachingPolicy() {
@Override
public boolean shouldCache(Query query) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void onUse(Query query) {
used[0] = true;
}
};
policy = new ElasticsearchQueryCachingPolicy(policy);
policy.onUse(new TermQuery(new Term("foo", "bar")));
assertFalse(used[0]);
policy.onUse(new PhraseQuery("foo", "bar", "baz"));
assertTrue(used[0]);
}
use of org.apache.lucene.search.PhraseQuery 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.PhraseQuery in project zm-mailbox by Zimbra.
the class AbstractIndexStoreTest method phraseQueryWithStopWord.
@Test
public void phraseQueryWithStopWord() throws Exception {
ZimbraLog.test.debug("--->TEST phraseQueryWithStopWord");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
createContact(mbox, "Non", "Match", "nOn.MaTchiNg@zimbra.com");
Contact contact2 = createContact(mbox, "First", "Last", "f.last@zimbra.com", "1066 and all that with William the conqueror and others");
createContact(mbox, "Given", "Surname", "GiV.SurN@zimbra.com");
// Make sure all indexing has been done
mbox.index.indexDeferredItems();
IndexStore index = mbox.index.getIndexStore();
ZimbraIndexSearcher searcher = index.openSearcher();
PhraseQuery pquery = new PhraseQuery();
// Lower case required for each term for Lucene
pquery.add(new Term(LuceneFields.L_CONTENT, "william"));
// pquery.add(new Term(LuceneFields.L_CONTENT, "the")); - excluded because it is a stop word
pquery.add(new Term(LuceneFields.L_CONTENT, "conqueror"));
ZimbraTopDocs result = searcher.search(pquery, 100);
Assert.assertNotNull("searcher.search result object", result);
ZimbraLog.test.debug("Result for search [hits=%d]:%s", result.getTotalHits(), result.toString());
Assert.assertEquals("Number of hits", 1, result.getTotalHits());
String expected1Id = String.valueOf(contact2.getId());
String match1Id = getBlobIdForResultDoc(searcher, result, 0);
Assert.assertEquals("Mailbox Blob ID of match", expected1Id, match1Id);
}
use of org.apache.lucene.search.PhraseQuery in project jackrabbit by apache.
the class AbstractExcerpt method getQueryTerms.
private static void getQueryTerms(Query q, Set<Term[]> relevantTerms) {
if (q instanceof BooleanQuery) {
final BooleanQuery bq = (BooleanQuery) q;
for (BooleanClause clause : bq.getClauses()) {
getQueryTerms(clause.getQuery(), relevantTerms);
}
return;
}
//need to preserve insertion order
Set<Term> extractedTerms = new LinkedHashSet<Term>();
q.extractTerms(extractedTerms);
Set<Term> filteredTerms = filterRelevantTerms(extractedTerms);
if (!filteredTerms.isEmpty()) {
if (q instanceof PhraseQuery) {
// inline the terms, basically a 'must all' condition
relevantTerms.add(filteredTerms.toArray(new Term[] {}));
} else {
// each possible term gets a new slot
for (Term t : filteredTerms) {
relevantTerms.add(new Term[] { t });
}
}
}
}
Aggregations