Search in sources :

Example 6 with SpanTermQuery

use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_spanFirstQuery.

public void testExtractQueryMetadata_spanFirstQuery() {
    SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
    SpanFirstQuery spanFirstQuery = new SpanFirstQuery(spanTermQuery1, 20);
    Result result = analyze(spanFirstQuery);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, spanTermQuery1.getTerm());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 7 with SpanTermQuery

use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_spanNotQuery.

public void testExtractQueryMetadata_spanNotQuery() {
    SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
    SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term"));
    SpanNotQuery spanNotQuery = new SpanNotQuery(spanTermQuery1, spanTermQuery2);
    Result result = analyze(spanNotQuery);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, spanTermQuery1.getTerm());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 8 with SpanTermQuery

use of org.apache.lucene.search.spans.SpanTermQuery in project elasticsearch by elastic.

the class SpanTermQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanTermQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, instanceOf(SpanTermQuery.class));
    SpanTermQuery spanTermQuery = (SpanTermQuery) query;
    assertThat(spanTermQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
    MappedFieldType mapper = context.getQueryShardContext().fieldMapper(queryBuilder.fieldName());
    if (mapper != null) {
        Term term = ((TermQuery) mapper.termQuery(queryBuilder.value(), null)).getTerm();
        assertThat(spanTermQuery.getTerm(), equalTo(term));
    } else {
        assertThat(spanTermQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value())));
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 9 with SpanTermQuery

use of org.apache.lucene.search.spans.SpanTermQuery 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;
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Document(org.apache.lucene.document.Document) IOException(java.io.IOException) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) IndexReader(org.apache.lucene.index.IndexReader)

Example 10 with SpanTermQuery

use of org.apache.lucene.search.spans.SpanTermQuery in project libresonic by Libresonic.

the class SearchService method search.

public SearchResult search(SearchCriteria criteria, List<MusicFolder> musicFolders, IndexType indexType) {
    SearchResult result = new SearchResult();
    int offset = criteria.getOffset();
    int count = criteria.getCount();
    result.setOffset(offset);
    IndexReader reader = null;
    try {
        reader = createIndexReader(indexType);
        Searcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new LibresonicAnalyzer();
        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(LUCENE_VERSION, indexType.getFields(), analyzer, indexType.getBoosts());
        BooleanQuery query = new BooleanQuery();
        query.add(queryParser.parse(analyzeQuery(criteria.getQuery())), BooleanClause.Occur.MUST);
        List<SpanTermQuery> musicFolderQueries = new ArrayList<SpanTermQuery>();
        for (MusicFolder musicFolder : musicFolders) {
            if (indexType == ALBUM_ID3 || indexType == ARTIST_ID3) {
                musicFolderQueries.add(new SpanTermQuery(new Term(FIELD_FOLDER_ID, NumericUtils.intToPrefixCoded(musicFolder.getId()))));
            } else {
                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, offset + count);
        result.setTotalHits(topDocs.totalHits);
        int start = Math.min(offset, topDocs.totalHits);
        int end = Math.min(start + count, topDocs.totalHits);
        for (int i = start; i < end; i++) {
            Document doc = searcher.doc(topDocs.scoreDocs[i].doc);
            switch(indexType) {
                case SONG:
                case ARTIST:
                case ALBUM:
                    MediaFile mediaFile = mediaFileService.getMediaFile(Integer.valueOf(doc.get(FIELD_ID)));
                    addIfNotNull(mediaFile, result.getMediaFiles());
                    break;
                case ARTIST_ID3:
                    Artist artist = artistDao.getArtist(Integer.valueOf(doc.get(FIELD_ID)));
                    addIfNotNull(artist, result.getArtists());
                    break;
                case ALBUM_ID3:
                    Album album = albumDao.getAlbum(Integer.valueOf(doc.get(FIELD_ID)));
                    addIfNotNull(album, result.getAlbums());
                    break;
                default:
                    break;
            }
        }
    } catch (Throwable x) {
        LOG.error("Failed to execute Lucene search.", x);
    } finally {
        FileUtil.closeQuietly(reader);
    }
    return result;
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) Term(org.apache.lucene.index.Term) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Document(org.apache.lucene.document.Document) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) IndexReader(org.apache.lucene.index.IndexReader)

Aggregations

SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)196 Term (org.apache.lucene.index.Term)191 SpanQuery (org.apache.lucene.search.spans.SpanQuery)121 Test (org.junit.Test)103 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)59 KrillIndex (de.ids_mannheim.korap.KrillIndex)57 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)35 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)34 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)31 Result (de.ids_mannheim.korap.response.Result)30 ArrayList (java.util.ArrayList)27 Document (org.apache.lucene.document.Document)24 IndexReader (org.apache.lucene.index.IndexReader)24 BooleanQuery (org.apache.lucene.search.BooleanQuery)22 Query (org.apache.lucene.search.Query)22 TermQuery (org.apache.lucene.search.TermQuery)22 TopDocs (org.apache.lucene.search.TopDocs)21 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)20 IndexSearcher (org.apache.lucene.search.IndexSearcher)20 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)18