Search in sources :

Example 96 with PhraseQuery

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

the class TestQPHelper method testPositionIncrement.

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

Example 97 with PhraseQuery

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

the class TestSimpleQueryParser method testGarbagePhrase.

public void testGarbagePhrase() throws Exception {
    PhraseQuery expected = new PhraseQuery("field", "star", "wars");
    assertEquals(expected, parse("\"star wars\""));
    assertEquals(expected, parse("\"star wars\\ \""));
    assertEquals(expected, parse("\"\" | \"star wars\""));
    assertEquals(expected, parse("          \"star wars\"        \"\"\\"));
}
Also used : PhraseQuery(org.apache.lucene.search.PhraseQuery)

Example 98 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project xodus by JetBrains.

the class ExodusLuceneWithPatriciaTests method addSearchPhraseTestQuery.

@Test
public void addSearchPhraseTestQuery() throws IOException, ParseException {
    addSearchMatch();
    Query query = getQuery(DESCRIPTION, "\"could now start selling\"");
    Assert.assertTrue(query instanceof PhraseQuery);
    TopDocs docs = indexSearcher.search(query, Integer.MAX_VALUE);
    Assert.assertEquals(1, docs.totalHits);
    query = getQuery(DESCRIPTION, "\"the fourth stage\"");
    Assert.assertTrue(query instanceof PhraseQuery);
    docs = indexSearcher.search(query, Integer.MAX_VALUE);
    Assert.assertEquals(1, docs.totalHits);
    removeStopWord("on");
    query = getQuery(DESCRIPTION, "\"on the plane\"");
    Assert.assertTrue(query instanceof PhraseQuery);
    docs = indexSearcher.search(query, Integer.MAX_VALUE);
    Assert.assertEquals(0, docs.totalHits);
    removeStopWord("as");
    query = getQuery(DESCRIPTION, "\"as a player\"");
    Assert.assertTrue(query instanceof PhraseQuery);
    docs = indexSearcher.search(query, Integer.MAX_VALUE);
    Assert.assertEquals(0, docs.totalHits);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Test(org.junit.Test)

Example 99 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project eol-globi-data by jhpoelen.

the class TaxonLookupServiceImpl method findTaxon.

private Taxon[] findTaxon(String fieldName1, String fieldValue) throws IOException {
    Taxon[] terms = new TaxonImpl[0];
    if (StringUtils.isNotBlank(fieldValue) && indexSearcher != null) {
        PhraseQuery query = new PhraseQuery();
        query.add(new Term(fieldName1, fieldValue));
        TopDocs docs = indexSearcher.search(query, getMaxHits());
        if (docs.totalHits > 0) {
            int maxResults = Math.min(docs.totalHits, getMaxHits());
            terms = new TaxonImpl[maxResults];
            for (int i = 0; i < maxResults; i++) {
                ScoreDoc scoreDoc = docs.scoreDocs[i];
                Document foundDoc = indexSearcher.doc(scoreDoc.doc);
                Taxon term = new TaxonImpl();
                Fieldable idField = foundDoc.getFieldable(FIELD_ID);
                if (idField != null) {
                    term.setExternalId(idField.stringValue());
                }
                Fieldable rankPathField = foundDoc.getFieldable(FIELD_RANK_PATH);
                if (rankPathField != null) {
                    term.setPath(rankPathField.stringValue());
                }
                Fieldable rankPathIdsField = foundDoc.getFieldable(FIELD_RANK_PATH_IDS);
                if (rankPathIdsField != null) {
                    term.setPathIds(rankPathIdsField.stringValue());
                }
                Fieldable rankPathNamesField = foundDoc.getFieldable(FIELD_RANK_PATH_NAMES);
                if (rankPathNamesField != null) {
                    term.setPathNames(rankPathNamesField.stringValue());
                }
                Fieldable commonNamesFields = foundDoc.getFieldable(FIELD_COMMON_NAMES);
                if (commonNamesFields != null) {
                    term.setCommonNames(commonNamesFields.stringValue());
                }
                Fieldable fieldName = foundDoc.getFieldable(FIELD_RECOMMENDED_NAME);
                if (fieldName != null) {
                    term.setName(fieldName.stringValue());
                }
                terms[i] = term;
            }
        }
    }
    return terms;
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) PhraseQuery(org.apache.lucene.search.PhraseQuery) Fieldable(org.apache.lucene.document.Fieldable) Taxon(org.eol.globi.domain.Taxon) TaxonImpl(org.eol.globi.domain.TaxonImpl) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 100 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();
    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....
                    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()));
                }
                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)

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