Search in sources :

Example 26 with PrefixQuery

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

the class LuceneInMemorySearchTest method givenPrefixQueryWhenFetchedDocumentThenCorrect.

@Test
public void givenPrefixQueryWhenFetchedDocumentThenCorrect() {
    InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
    inMemoryLuceneIndex.indexDocument("article", "Lucene introduction");
    inMemoryLuceneIndex.indexDocument("article", "Introduction to Lucene");
    Term term = new Term("body", "intro");
    Query query = new PrefixQuery(term);
    List<Document> documents = inMemoryLuceneIndex.searchIndex(query);
    Assert.assertEquals(2, 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) PrefixQuery(org.apache.lucene.search.PrefixQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Test(org.junit.Test)

Example 27 with PrefixQuery

use of org.apache.lucene.search.PrefixQuery in project webcert by sklintyg.

the class DiagnosRepositoryImpl method searchDiagnosisByCode.

/*
     * (non-Javadoc)
     *
     * @see se.inera.intyg.webcert.web.service.diagnos.model.DiagnosRepository#searchDiagnosisByCode(java.lang.String)
     */
@Override
public List<Diagnos> searchDiagnosisByCode(String codeFragment, int nbrOfResults) {
    String codeFragmentSanitized = sanitizeCodeValue(codeFragment);
    if (codeFragmentSanitized == null) {
        return new ArrayList<>();
    }
    PrefixQuery query = new PrefixQuery(new Term(CODE, codeFragmentSanitized));
    return searchDiagnosisByQuery(query, nbrOfResults);
}
Also used : PrefixQuery(org.apache.lucene.search.PrefixQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term)

Example 28 with PrefixQuery

use of org.apache.lucene.search.PrefixQuery in project vertigo by KleeGroup.

the class RamLuceneQueryFactory method createParsedKeywordsQuery.

private static Query createParsedKeywordsQuery(final Analyzer queryAnalyser, final String fieldName, final String keywords) throws IOException {
    final Builder queryBuilder = new BooleanQuery.Builder();
    final Reader reader = new StringReader(keywords);
    try (final TokenStream tokenStream = queryAnalyser.tokenStream(fieldName, reader)) {
        tokenStream.reset();
        try {
            final CharTermAttribute termAttribute = tokenStream.getAttribute(CharTermAttribute.class);
            while (tokenStream.incrementToken()) {
                final String term = new String(termAttribute.buffer(), 0, termAttribute.length());
                final PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, term));
                queryBuilder.add(prefixQuery, BooleanClause.Occur.MUST);
                final SpanFirstQuery spanSecondQuery = new SpanFirstQuery(new SpanMultiTermQueryWrapper<>(prefixQuery), 1);
                queryBuilder.add(spanSecondQuery, BooleanClause.Occur.SHOULD);
            }
        } finally {
            reader.reset();
            tokenStream.end();
        }
    }
    return queryBuilder.build();
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Term(org.apache.lucene.index.Term)

Example 29 with PrefixQuery

use of org.apache.lucene.search.PrefixQuery in project vertigo by KleeGroup.

the class RamLuceneQueryFactory method createParsedKeywordsQuery.

private static Query createParsedKeywordsQuery(final Analyzer queryAnalyser, final String fieldName, final String keywords) throws IOException {
    final Builder queryBuilder = new BooleanQuery.Builder();
    final Reader reader = new StringReader(keywords);
    try (final TokenStream tokenStream = queryAnalyser.tokenStream(fieldName, reader)) {
        tokenStream.reset();
        try {
            final CharTermAttribute termAttribute = tokenStream.getAttribute(CharTermAttribute.class);
            while (tokenStream.incrementToken()) {
                final String term = new String(termAttribute.buffer(), 0, termAttribute.length());
                final PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, term));
                queryBuilder.add(prefixQuery, BooleanClause.Occur.MUST);
                final SpanFirstQuery spanSecondQuery = new SpanFirstQuery(new SpanMultiTermQueryWrapper<>(prefixQuery), 1);
                queryBuilder.add(spanSecondQuery, BooleanClause.Occur.SHOULD);
            }
        } finally {
            reader.reset();
            tokenStream.end();
        }
    }
    return queryBuilder.build();
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Term(org.apache.lucene.index.Term)

Example 30 with PrefixQuery

use of org.apache.lucene.search.PrefixQuery in project che by eclipse.

the class LuceneSearcher method delete.

@Override
public final void delete(String path, boolean isFile) throws ServerException {
    try {
        if (isFile) {
            Term term = new Term(PATH_FIELD, path);
            getIndexWriter().deleteDocuments(term);
        } else {
            Term term = new Term(PATH_FIELD, path + '/');
            getIndexWriter().deleteDocuments(new PrefixQuery(term));
        }
    } catch (OutOfMemoryError oome) {
        close();
        throw oome;
    } catch (IOException e) {
        throw new ServerException(e.getMessage(), e);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term) IOException(java.io.IOException)

Aggregations

PrefixQuery (org.apache.lucene.search.PrefixQuery)68 Term (org.apache.lucene.index.Term)62 BooleanQuery (org.apache.lucene.search.BooleanQuery)34 Query (org.apache.lucene.search.Query)30 TermQuery (org.apache.lucene.search.TermQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 WildcardQuery (org.apache.lucene.search.WildcardQuery)23 BoostQuery (org.apache.lucene.search.BoostQuery)20 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)19 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)15 Document (org.apache.lucene.document.Document)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 PhraseQuery (org.apache.lucene.search.PhraseQuery)14 RegexpQuery (org.apache.lucene.search.RegexpQuery)13 TopDocs (org.apache.lucene.search.TopDocs)13 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)12 IndexReader (org.apache.lucene.index.IndexReader)11 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)11 Field (org.apache.lucene.document.Field)10