use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.
the class QueryAnalyzerTests method testExtractQueryMetadata_spanNearQuery.
public void testExtractQueryMetadata_spanNearQuery() {
SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term"));
SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("_field", true).addClause(spanTermQuery1).addClause(spanTermQuery2).build();
Result result = analyze(spanNearQuery);
assertThat(result.verified, is(false));
assertTermsEqual(result.terms, spanTermQuery2.getTerm());
}
use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.
the class SpanTermQueryBuilderTests method testWithMetaDataField.
public void testWithMetaDataField() throws IOException {
QueryShardContext context = createShardContext();
for (String field : new String[] { "_type", "_all" }) {
SpanTermQueryBuilder spanTermQueryBuilder = new SpanTermQueryBuilder(field, "toto");
Query query = spanTermQueryBuilder.toQuery(context);
Query expected = new SpanTermQuery(new Term(field, "toto"));
assertEquals(expected, query);
}
}
use of org.apache.lucene.search.spans.SpanTermQuery in project libresonic by Libresonic.
the class SearchService method getRandomSongs.
/**
* Returns a number of random songs.
*
* @param criteria Search criteria.
* @return List of random songs.
*/
public List<MediaFile> getRandomSongs(RandomSearchCriteria criteria) {
List<MediaFile> result = new ArrayList<MediaFile>();
IndexReader reader = null;
try {
reader = createIndexReader(SONG);
Searcher searcher = new IndexSearcher(reader);
BooleanQuery query = new BooleanQuery();
query.add(new TermQuery(new Term(FIELD_MEDIA_TYPE, MediaFile.MediaType.MUSIC.name().toLowerCase())), BooleanClause.Occur.MUST);
if (criteria.getGenre() != null) {
String genre = normalizeGenre(criteria.getGenre());
query.add(new TermQuery(new Term(FIELD_GENRE, genre)), BooleanClause.Occur.MUST);
}
if (criteria.getFromYear() != null || criteria.getToYear() != null) {
NumericRangeQuery<Integer> rangeQuery = NumericRangeQuery.newIntRange(FIELD_YEAR, criteria.getFromYear(), criteria.getToYear(), true, true);
query.add(rangeQuery, BooleanClause.Occur.MUST);
}
List<SpanTermQuery> musicFolderQueries = new ArrayList<SpanTermQuery>();
for (MusicFolder musicFolder : criteria.getMusicFolders()) {
musicFolderQueries.add(new SpanTermQuery(new Term(FIELD_FOLDER, musicFolder.getPath().getPath())));
}
query.add(new SpanOrQuery(musicFolderQueries.toArray(new SpanQuery[musicFolderQueries.size()])), BooleanClause.Occur.MUST);
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() < criteria.getCount()) {
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);
}
}
} catch (Throwable x) {
LOG.error("Failed to search or random songs.", x);
} finally {
FileUtil.closeQuietly(reader);
}
return result;
}
use of org.apache.lucene.search.spans.SpanTermQuery 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.SpanTermQuery in project lucene-solr by apache.
the class HighlighterTest method payloadFilteringSpanQuery.
@Test
public void payloadFilteringSpanQuery() throws IOException {
clearIndex();
String FIELD_NAME = "payloadDelimited";
assertU(adoc("id", "0", FIELD_NAME, "word|7 word|2"));
assertU(commit());
//We search at a lower level than typical Solr tests because there's no QParser for payloads
//Create query matching this payload
Query query = new SpanPayloadCheckQuery(new SpanTermQuery(new Term(FIELD_NAME, "word")), //bytes for integer 7
Collections.singletonList(new BytesRef(new byte[] { 0, 0, 0, 7 })));
//invoke highlight component... the hard way
final SearchComponent hlComp = h.getCore().getSearchComponent("highlight");
SolrQueryRequest req = req("hl", "true", "hl.fl", FIELD_NAME, HighlightParams.USE_PHRASE_HIGHLIGHTER, "true");
try {
SolrQueryResponse resp = new SolrQueryResponse();
ResponseBuilder rb = new ResponseBuilder(req, resp, Collections.singletonList(hlComp));
rb.setHighlightQuery(query);
rb.setResults(req.getSearcher().getDocListAndSet(query, (DocSet) null, null, 0, 1));
//highlight:
hlComp.prepare(rb);
hlComp.process(rb);
//inspect response
final String[] snippets = (String[]) resp.getValues().findRecursive("highlighting", "0", FIELD_NAME);
assertEquals("<em>word|7</em> word|2", snippets[0]);
} finally {
req.close();
}
}
Aggregations