Search in sources :

Example 6 with PhraseQuery

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")));
}
Also used : QueryCachingPolicy(org.apache.lucene.search.QueryCachingPolicy) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term)

Example 7 with PhraseQuery

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]);
}
Also used : QueryCachingPolicy(org.apache.lucene.search.QueryCachingPolicy) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term)

Example 8 with PhraseQuery

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);
        }
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) Term(org.apache.lucene.index.Term)

Example 9 with PhraseQuery

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);
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 10 with PhraseQuery

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 });
            }
        }
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) LinkedHashSet(java.util.LinkedHashSet) BooleanQuery(org.apache.lucene.search.BooleanQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term)

Aggregations

PhraseQuery (org.apache.lucene.search.PhraseQuery)95 Term (org.apache.lucene.index.Term)49 TermQuery (org.apache.lucene.search.TermQuery)38 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)36 BooleanQuery (org.apache.lucene.search.BooleanQuery)35 Document (org.apache.lucene.document.Document)34 Directory (org.apache.lucene.store.Directory)26 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)25 Query (org.apache.lucene.search.Query)25 IndexSearcher (org.apache.lucene.search.IndexSearcher)22 IndexReader (org.apache.lucene.index.IndexReader)20 Field (org.apache.lucene.document.Field)17 TextField (org.apache.lucene.document.TextField)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 TokenStream (org.apache.lucene.analysis.TokenStream)15 TopDocs (org.apache.lucene.search.TopDocs)14 IndexWriter (org.apache.lucene.index.IndexWriter)13 BoostQuery (org.apache.lucene.search.BoostQuery)13 ArrayList (java.util.ArrayList)10 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)10