Search in sources :

Example 1 with SpanOrQuery

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

the class QueryAnalyzerTests method testExtractQueryMetadata_spanOrQuery.

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

Example 2 with SpanOrQuery

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

the class CustomUnifiedHighlighter method rewriteCustomQuery.

/**
     * Translate custom queries in queries that are supported by the unified highlighter.
     */
private Collection<Query> rewriteCustomQuery(Query query) {
    if (query instanceof MultiPhrasePrefixQuery) {
        MultiPhrasePrefixQuery mpq = (MultiPhrasePrefixQuery) query;
        Term[][] terms = mpq.getTerms();
        int[] positions = mpq.getPositions();
        SpanQuery[] positionSpanQueries = new SpanQuery[positions.length];
        int sizeMinus1 = terms.length - 1;
        for (int i = 0; i < positions.length; i++) {
            SpanQuery[] innerQueries = new SpanQuery[terms[i].length];
            for (int j = 0; j < terms[i].length; j++) {
                if (i == sizeMinus1) {
                    innerQueries[j] = new SpanMultiTermQueryWrapper(new PrefixQuery(terms[i][j]));
                } else {
                    innerQueries[j] = new SpanTermQuery(terms[i][j]);
                }
            }
            if (innerQueries.length > 1) {
                positionSpanQueries[i] = new SpanOrQuery(innerQueries);
            } else {
                positionSpanQueries[i] = innerQueries[0];
            }
        }
        // sum position increments beyond 1
        int positionGaps = 0;
        if (positions.length >= 2) {
            // positions are in increasing order.   max(0,...) is just a safeguard.
            positionGaps = Math.max(0, positions[positions.length - 1] - positions[0] - positions.length + 1);
        }
        //if original slop is 0 then require inOrder
        boolean inorder = (mpq.getSlop() == 0);
        return Collections.singletonList(new SpanNearQuery(positionSpanQueries, mpq.getSlop() + positionGaps, inorder));
    } else if (query instanceof CommonTermsQuery) {
        CommonTermsQuery ctq = (CommonTermsQuery) query;
        List<Query> tqs = new ArrayList<>();
        for (Term term : ctq.getTerms()) {
            tqs.add(new TermQuery(term));
        }
        return tqs;
    } else if (query instanceof AllTermQuery) {
        AllTermQuery atq = (AllTermQuery) query;
        return Collections.singletonList(new TermQuery(atq.getTerm()));
    } else if (query instanceof FunctionScoreQuery) {
        return Collections.singletonList(((FunctionScoreQuery) query).getSubQuery());
    } else if (query instanceof FiltersFunctionScoreQuery) {
        return Collections.singletonList(((FiltersFunctionScoreQuery) query).getSubQuery());
    } else {
        return null;
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) Term(org.apache.lucene.index.Term) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ArrayList(java.util.ArrayList) List(java.util.List) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 3 with SpanOrQuery

use of org.apache.lucene.search.spans.SpanOrQuery 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 4 with SpanOrQuery

use of org.apache.lucene.search.spans.SpanOrQuery 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)

Example 5 with SpanOrQuery

use of org.apache.lucene.search.spans.SpanOrQuery in project lucene-solr by apache.

the class QueryBuilder method analyzeGraphPhrase.

/**
   * Creates a span near (phrase) query from a graph token stream. The articulation points of the graph are visited in
   * order and the queries created at each point are merged in the returned near query.
   */
protected SpanQuery analyzeGraphPhrase(TokenStream source, String field, int phraseSlop) throws IOException {
    source.reset();
    GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);
    List<SpanQuery> clauses = new ArrayList<>();
    int[] articulationPoints = graph.articulationPoints();
    int lastState = 0;
    for (int i = 0; i <= articulationPoints.length; i++) {
        int start = lastState;
        int end = -1;
        if (i < articulationPoints.length) {
            end = articulationPoints[i];
        }
        lastState = end;
        final SpanQuery queryPos;
        if (graph.hasSidePath(start)) {
            List<SpanQuery> queries = new ArrayList<>();
            Iterator<TokenStream> it = graph.getFiniteStrings(start, end);
            while (it.hasNext()) {
                TokenStream ts = it.next();
                SpanQuery q = createSpanQuery(ts, field);
                if (q != null) {
                    queries.add(q);
                }
            }
            if (queries.size() > 0) {
                queryPos = new SpanOrQuery(queries.toArray(new SpanQuery[0]));
            } else {
                queryPos = null;
            }
        } else {
            Term[] terms = graph.getTerms(field, start);
            assert terms.length > 0;
            if (terms.length == 1) {
                queryPos = new SpanTermQuery(terms[0]);
            } else {
                SpanTermQuery[] orClauses = new SpanTermQuery[terms.length];
                for (int idx = 0; idx < terms.length; idx++) {
                    orClauses[idx] = new SpanTermQuery(terms[idx]);
                }
                queryPos = new SpanOrQuery(orClauses);
            }
        }
        if (queryPos != null) {
            clauses.add(queryPos);
        }
    }
    if (clauses.isEmpty()) {
        return null;
    } else if (clauses.size() == 1) {
        return clauses.get(0);
    } else {
        return new SpanNearQuery(clauses.toArray(new SpanQuery[0]), phraseSlop, true);
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) GraphTokenStreamFiniteStrings(org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Aggregations

SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)30 Term (org.apache.lucene.index.Term)22 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)22 SpanQuery (org.apache.lucene.search.spans.SpanQuery)20 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)15 BooleanQuery (org.apache.lucene.search.BooleanQuery)10 Query (org.apache.lucene.search.Query)10 TermQuery (org.apache.lucene.search.TermQuery)10 ArrayList (java.util.ArrayList)8 Document (org.apache.lucene.document.Document)8 IndexReader (org.apache.lucene.index.IndexReader)7 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)7 BooleanClause (org.apache.lucene.search.BooleanClause)5 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 PrefixQuery (org.apache.lucene.search.PrefixQuery)5 IOException (java.io.IOException)4 BoostQuery (org.apache.lucene.search.BoostQuery)4 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)4 PhraseQuery (org.apache.lucene.search.PhraseQuery)4