Search in sources :

Example 46 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.

the class TestIndexWriter method testStopwordsPosIncHole.

// LUCENE-3849
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);
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) MockTokenFilter(org.apache.lucene.analysis.MockTokenFilter) PhraseQuery(org.apache.lucene.search.PhraseQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) Document(org.apache.lucene.document.Document) MockTokenizer(org.apache.lucene.analysis.MockTokenizer) TextField(org.apache.lucene.document.TextField) Tokenizer(org.apache.lucene.analysis.Tokenizer) MockTokenizer(org.apache.lucene.analysis.MockTokenizer) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Example 47 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project entando-core by entando.

the class SearcherDAO method createQuery.

private Query createQuery(SearchEngineFilter filter) {
    BooleanQuery fieldQuery = new BooleanQuery();
    String key = filter.getKey();
    String attachmentKey = key + IIndexerDAO.ATTACHMENT_FIELD_SUFFIX;
    Object value = filter.getValue();
    if (null != value) {
        if (value instanceof String) {
            SearchEngineFilter.TextSearchOption option = filter.getTextSearchOption();
            if (null == option) {
                option = SearchEngineFilter.TextSearchOption.AT_LEAST_ONE_WORD;
            }
            String stringValue = value.toString();
            String[] values = stringValue.split("\\s+");
            if (!option.equals(SearchEngineFilter.TextSearchOption.EXACT)) {
                BooleanClause.Occur bc = BooleanClause.Occur.SHOULD;
                if (option.equals(SearchEngineFilter.TextSearchOption.ALL_WORDS)) {
                    bc = BooleanClause.Occur.MUST;
                } else if (option.equals(SearchEngineFilter.TextSearchOption.ANY_WORD)) {
                    bc = BooleanClause.Occur.MUST_NOT;
                }
                for (int i = 0; i < values.length; i++) {
                    TermQuery term = new TermQuery(new Term(key, values[i].toLowerCase()));
                    // NOTE: search lower case....
                    if (filter.isIncludeAttachments()) {
                        BooleanQuery compositeQuery = new BooleanQuery();
                        compositeQuery.add(term, BooleanClause.Occur.SHOULD);
                        TermQuery termAttachment = new TermQuery(new Term(attachmentKey, values[i].toLowerCase()));
                        compositeQuery.add(termAttachment, BooleanClause.Occur.SHOULD);
                        fieldQuery.add(compositeQuery, bc);
                    } else {
                        fieldQuery.add(term, bc);
                    }
                }
            } else {
                PhraseQuery phraseQuery = new PhraseQuery();
                for (int i = 0; i < values.length; i++) {
                    // NOTE: search lower case....
                    phraseQuery.add(new Term(key, values[i].toLowerCase()));
                }
                if (filter.isIncludeAttachments()) {
                    fieldQuery.add(phraseQuery, BooleanClause.Occur.SHOULD);
                    PhraseQuery phraseQuery2 = new PhraseQuery();
                    for (int i = 0; i < values.length; i++) {
                        // NOTE: search lower case....
                        phraseQuery2.add(new Term(attachmentKey, values[i].toLowerCase()));
                    }
                    fieldQuery.add(phraseQuery2, BooleanClause.Occur.SHOULD);
                } else {
                    return phraseQuery;
                }
            }
        } else if (value instanceof Date) {
            String toString = DateTools.timeToString(((Date) value).getTime(), DateTools.Resolution.MINUTE);
            TermQuery term = new TermQuery(new Term(filter.getKey(), toString));
            fieldQuery.add(term, BooleanClause.Occur.MUST);
        } else if (value instanceof Number) {
            TermQuery term = new TermQuery(new Term(filter.getKey(), value.toString()));
            fieldQuery.add(term, BooleanClause.Occur.MUST);
        }
    } else {
        if (filter.getStart() instanceof Number || filter.getEnd() instanceof Number) {
        // .............................. TODO
        } else {
            String start = null;
            String end = null;
            if (filter.getStart() instanceof Date || filter.getEnd() instanceof Date) {
                if (null != filter.getStart()) {
                    start = DateTools.timeToString(((Date) filter.getStart()).getTime(), DateTools.Resolution.MINUTE);
                }
                if (null != filter.getEnd()) {
                    end = DateTools.timeToString(((Date) filter.getEnd()).getTime(), DateTools.Resolution.MINUTE);
                }
            } else {
                start = (null != filter.getStart()) ? filter.getStart().toString().toLowerCase() : null;
                end = (null != filter.getEnd()) ? filter.getEnd().toString().toLowerCase() : null;
            }
            BytesRef byteStart = (null != start) ? new BytesRef(start.getBytes()) : null;
            BytesRef byteEnd = (null != end) ? new BytesRef(end.getBytes()) : null;
            TermRangeQuery range = new TermRangeQuery(filter.getKey(), byteStart, byteEnd, true, true);
            fieldQuery.add(range, BooleanClause.Occur.MUST);
        }
    }
    return fieldQuery;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) Date(java.util.Date) BooleanClause(org.apache.lucene.search.BooleanClause) SearchEngineFilter(org.entando.entando.aps.system.services.searchengine.SearchEngineFilter) BytesRef(org.apache.lucene.util.BytesRef)

Example 48 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project cxf by apache.

the class LuceneQueryVisitor method newPhraseQuery.

private Query newPhraseQuery(final String field, final String query) {
    if (queryBuilder != null) {
        return queryBuilder.createPhraseQuery(field, query);
    }
    final PhraseQuery phraseQuery = new PhraseQuery();
    phraseQuery.add(new Term(field, query));
    return phraseQuery;
}
Also used : PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term)

Example 49 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project tutorials by eugenp.

the class LuceneInMemorySearchTest method givenPhraseQueryWhenFetchedDocumentThenCorrect.

@Test
public void givenPhraseQueryWhenFetchedDocumentThenCorrect() {
    InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
    inMemoryLuceneIndex.indexDocument("quotes", "A rose by any other name would smell as sweet.");
    Query query = new PhraseQuery(1, "body", new BytesRef("smell"), new BytesRef("sweet"));
    List<Document> documents = inMemoryLuceneIndex.searchIndex(query);
    Assert.assertEquals(1, documents.size());
}
Also used : Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 50 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)105 Term (org.apache.lucene.index.Term)56 TermQuery (org.apache.lucene.search.TermQuery)43 BooleanQuery (org.apache.lucene.search.BooleanQuery)39 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)37 Document (org.apache.lucene.document.Document)36 Query (org.apache.lucene.search.Query)30 Directory (org.apache.lucene.store.Directory)26 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)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 TopDocs (org.apache.lucene.search.TopDocs)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 TokenStream (org.apache.lucene.analysis.TokenStream)15 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexWriter (org.apache.lucene.index.IndexWriter)13 ArrayList (java.util.ArrayList)11 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)10