use of org.apache.lucene.search.spans.SpanQuery in project elasticsearch by elastic.
the class SpanNearQueryBuilderTests method doAssertLuceneQuery.
@Override
protected void doAssertLuceneQuery(SpanNearQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
assertThat(query, instanceOf(SpanNearQuery.class));
SpanNearQuery spanNearQuery = (SpanNearQuery) query;
assertThat(spanNearQuery.getSlop(), equalTo(queryBuilder.slop()));
assertThat(spanNearQuery.isInOrder(), equalTo(queryBuilder.inOrder()));
assertThat(spanNearQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
for (SpanQuery spanQuery : spanNearQuery.getClauses()) {
assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context.getQueryShardContext())));
}
}
use of org.apache.lucene.search.spans.SpanQuery in project elasticsearch by elastic.
the class SpanOrQueryBuilderTests method doAssertLuceneQuery.
@Override
protected void doAssertLuceneQuery(SpanOrQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
assertThat(query, instanceOf(SpanOrQuery.class));
SpanOrQuery spanOrQuery = (SpanOrQuery) query;
assertThat(spanOrQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
for (SpanQuery spanQuery : spanOrQuery.getClauses()) {
assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context.getQueryShardContext())));
}
}
use of org.apache.lucene.search.spans.SpanQuery in project libresonic by Libresonic.
the class SearchService method getRandomAlbums.
/**
* Returns a number of random albums.
*
* @param count Number of albums to return.
* @param musicFolders Only return albums from these folders.
* @return List of random albums.
*/
public List<MediaFile> getRandomAlbums(int count, List<MusicFolder> musicFolders) {
List<MediaFile> result = new ArrayList<MediaFile>();
IndexReader reader = null;
try {
reader = createIndexReader(ALBUM);
Searcher searcher = new IndexSearcher(reader);
List<SpanTermQuery> musicFolderQueries = new ArrayList<SpanTermQuery>();
for (MusicFolder musicFolder : musicFolders) {
musicFolderQueries.add(new SpanTermQuery(new Term(FIELD_FOLDER, musicFolder.getPath().getPath())));
}
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(mediaFileService.getMediaFile(id), result);
} catch (Exception x) {
LOG.warn("Failed to get media 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 TestSolrCoreParser method testCustomQueryWrapping.
// test custom queries being wrapped in a Query or SpanQuery
public void testCustomQueryWrapping() throws IOException, ParserException {
final boolean span = random().nextBoolean();
// the custom queries
final String fieldName = "contents";
final String[] randomTerms = new String[] { "bumble", "honey", "solitary" };
final String randomQuery = composeChooseOneWordQueryXml(fieldName, randomTerms);
final String apacheLuceneSolr = "<ApacheLuceneSolr fieldName='" + fieldName + "'/>";
// the wrapping query
final String parentQuery = (span ? "SpanOr" : "BooleanQuery");
final String subQueryPrefix = (span ? "" : "<Clause occurs='must'>");
final String subQuerySuffix = (span ? "" : "</Clause>");
final String xml = "<" + parentQuery + ">" + subQueryPrefix + randomQuery + subQuerySuffix + subQueryPrefix + apacheLuceneSolr + subQuerySuffix + "</" + parentQuery + ">";
// the test
final Query query = parseXmlString(xml);
if (span) {
assertTrue(unwrapSpanBoostQuery(query) instanceof SpanOrQuery);
final SpanOrQuery soq = (SpanOrQuery) unwrapSpanBoostQuery(query);
assertEquals(2, soq.getClauses().length);
checkChooseOneWordQuery(span, soq.getClauses()[0], fieldName, randomTerms);
checkApacheLuceneSolr(soq.getClauses()[1], fieldName);
} else {
assertTrue(query instanceof BooleanQuery);
final BooleanQuery bq = (BooleanQuery) query;
assertEquals(2, bq.clauses().size());
checkChooseOneWordQuery(span, bq.clauses().get(0).getQuery(), fieldName, randomTerms);
checkApacheLuceneSolr(bq.clauses().get(1).getQuery(), fieldName);
}
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testFilteredOutSpan.
public void testFilteredOutSpan() throws IOException {
indexWriter.addDocument(newDoc("freezing cold stuff like stuff freedom of speech"));
initReaderSearcherHighlighter();
WildcardQuery wildcardQuery = new WildcardQuery(new Term("body", "free*"));
SpanMultiTermQueryWrapper<WildcardQuery> wildcardSpanQuery = new SpanMultiTermQueryWrapper<>(wildcardQuery);
SpanTermQuery termQuery = new SpanTermQuery(new Term("body", "speech"));
SpanQuery spanQuery = new SpanNearQuery(new SpanQuery[] { wildcardSpanQuery, termQuery }, 3, false);
BooleanQuery query = new BooleanQuery.Builder().add(spanQuery, BooleanClause.Occur.MUST).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "freezing cold stuff like stuff <b>freedom</b> of <b>speech</b>" }, snippets);
}
Aggregations