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]);
}
}
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\" \"\"\\"));
}
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);
}
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;
}
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;
}
Aggregations