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