use of org.apache.lucene.search.spans.SpanQuery in project elasticsearch by elastic.
the class SpanTermQueryBuilder method doToQuery.
@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
MappedFieldType mapper = context.fieldMapper(fieldName);
Term term;
if (mapper == null) {
term = new Term(fieldName, BytesRefs.toBytesRef(value));
} else {
Query termQuery = mapper.termQuery(value, context);
term = MappedFieldType.extractTerm(termQuery);
}
return new SpanTermQuery(term);
}
use of org.apache.lucene.search.spans.SpanQuery in project languagetool by languagetool-org.
the class PatternRuleQueryBuilder method makeQuery.
private BooleanClause makeQuery(PatternToken patternToken) throws UnsupportedPatternRuleException {
checkUnsupportedElement(patternToken);
String termStr = patternToken.getString();
String pos = patternToken.getPOStag();
BooleanClause termQuery = getTermQueryOrNull(patternToken, termStr);
BooleanClause posQuery = getPosQueryOrNull(patternToken, pos);
if (termQuery != null && posQuery != null) {
// if both term and POS are set, we create a query where both are at the same position
if (mustOccur(termQuery) && mustOccur(posQuery)) {
SpanQuery spanQueryForTerm = asSpanQuery(termQuery);
SpanQuery spanQueryForPos = asSpanQuery(posQuery);
SpanQuery[] spanClauses = { spanQueryForTerm, spanQueryForPos };
return new BooleanClause(new SpanNearQuery(spanClauses, 0, false), BooleanClause.Occur.MUST);
} else {
// should not happen, we always use Occur.MUST:
throw new UnsupportedPatternRuleException("Term/POS combination not supported yet: " + patternToken);
}
} else if (termQuery != null) {
return termQuery;
} else if (posQuery != null) {
return posQuery;
}
throw new UnsupportedPatternRuleException("Neither POS tag nor term set for element: " + patternToken);
}
use of org.apache.lucene.search.spans.SpanQuery in project libresonic by Libresonic.
the class SearchService method getRandomAlbumsId3.
/**
* Returns a number of random albums, using ID3 tag.
*
* @param count Number of albums to return.
* @param musicFolders Only return albums from these folders.
* @return List of random albums.
*/
public List<Album> getRandomAlbumsId3(int count, List<MusicFolder> musicFolders) {
List<Album> result = new ArrayList<Album>();
IndexReader reader = null;
try {
reader = createIndexReader(ALBUM_ID3);
Searcher searcher = new IndexSearcher(reader);
List<SpanTermQuery> musicFolderQueries = new ArrayList<SpanTermQuery>();
for (MusicFolder musicFolder : musicFolders) {
musicFolderQueries.add(new SpanTermQuery(new Term(FIELD_FOLDER_ID, NumericUtils.intToPrefixCoded(musicFolder.getId()))));
}
Query query = new SpanOrQuery(musicFolderQueries.toArray(new SpanQuery[musicFolderQueries.size()]));
TopDocs topDocs = searcher.search(query, null, Integer.MAX_VALUE);
List<ScoreDoc> scoreDocs = Lists.newArrayList(topDocs.scoreDocs);
Random random = new Random(System.currentTimeMillis());
while (!scoreDocs.isEmpty() && result.size() < count) {
int index = random.nextInt(scoreDocs.size());
Document doc = searcher.doc(scoreDocs.remove(index).doc);
int id = Integer.valueOf(doc.get(FIELD_ID));
try {
addIfNotNull(albumDao.getAlbum(id), result);
} catch (Exception x) {
LOG.warn("Failed to get album file " + id, x);
}
}
} catch (Throwable x) {
LOG.error("Failed to search for random albums.", x);
} finally {
FileUtil.closeQuietly(reader);
}
return result;
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class SynonymTokenizer method testSpanHighlighting.
public void testSpanHighlighting() throws Exception {
Query query1 = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "wordx")), new SpanTermQuery(new Term(FIELD_NAME, "wordy")) }, 1, false);
Query query2 = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "wordy")), new SpanTermQuery(new Term(FIELD_NAME, "wordc")) }, 1, false);
BooleanQuery.Builder bquery = new BooleanQuery.Builder();
bquery.add(query1, Occur.SHOULD);
bquery.add(query2, Occur.SHOULD);
doSearching(bquery.build());
TestHighlightRunner helper = new TestHighlightRunner() {
@Override
public void run() throws Exception {
mode = QUERY;
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
}
};
helper.run();
assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 7);
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class MissesTest method testSpanNearQuery.
public void testSpanNearQuery() throws IOException, InvalidTokenOffsetsException {
try (Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)) {
final Query query = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("test", "foo")), new SpanTermQuery(new Term("test", "bar")) }, 0, true);
final Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(query));
assertEquals("this is a <B>foo</B> <B>bar</B> example", highlighter.getBestFragment(analyzer, "test", "this is a foo bar example"));
assertNull(highlighter.getBestFragment(analyzer, "test", "this does not match"));
}
}
Aggregations