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());
}
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);
}
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();
}
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();
}
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);
}
}
Aggregations