Search in sources :

Example 46 with SpanQuery

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())));
    }
}
Also used : SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 47 with SpanQuery

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())));
    }
}
Also used : SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 48 with SpanQuery

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;
}
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 49 with SpanQuery

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);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery)

Example 50 with SpanQuery

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);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Aggregations

SpanQuery (org.apache.lucene.search.spans.SpanQuery)81 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)53 Term (org.apache.lucene.index.Term)51 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)43 Query (org.apache.lucene.search.Query)27 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)25 IndexReader (org.apache.lucene.index.IndexReader)18 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)18 Document (org.apache.lucene.document.Document)17 IndexSearcher (org.apache.lucene.search.IndexSearcher)16 BooleanQuery (org.apache.lucene.search.BooleanQuery)15 TopDocs (org.apache.lucene.search.TopDocs)15 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)13 TermQuery (org.apache.lucene.search.TermQuery)13 ArrayList (java.util.ArrayList)12 Spans (org.apache.lucene.search.spans.Spans)12 PhraseQuery (org.apache.lucene.search.PhraseQuery)11 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)10 SpanNotQuery (org.apache.lucene.search.spans.SpanNotQuery)10 Directory (org.apache.lucene.store.Directory)10