Search in sources :

Example 6 with SpanQuery

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

the class SpanTermQueryBuilder method doToQuery.

@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType mapper = context.fieldMapper(fieldName);
    Term term;
    if (mapper == null) {
        term = new Term(fieldName, BytesRefs.toBytesRef(value));
    } else {
        Query termQuery = mapper.termQuery(value, context);
        term = MappedFieldType.extractTerm(termQuery);
    }
    return new SpanTermQuery(term);
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 7 with SpanQuery

use of org.apache.lucene.search.spans.SpanQuery in project languagetool by languagetool-org.

the class PatternRuleQueryBuilder method makeQuery.

private BooleanClause makeQuery(PatternToken patternToken) throws UnsupportedPatternRuleException {
    checkUnsupportedElement(patternToken);
    String termStr = patternToken.getString();
    String pos = patternToken.getPOStag();
    BooleanClause termQuery = getTermQueryOrNull(patternToken, termStr);
    BooleanClause posQuery = getPosQueryOrNull(patternToken, pos);
    if (termQuery != null && posQuery != null) {
        // if both term and POS are set, we create a query where both are at the same position
        if (mustOccur(termQuery) && mustOccur(posQuery)) {
            SpanQuery spanQueryForTerm = asSpanQuery(termQuery);
            SpanQuery spanQueryForPos = asSpanQuery(posQuery);
            SpanQuery[] spanClauses = { spanQueryForTerm, spanQueryForPos };
            return new BooleanClause(new SpanNearQuery(spanClauses, 0, false), BooleanClause.Occur.MUST);
        } else {
            // should not happen, we always use Occur.MUST:
            throw new UnsupportedPatternRuleException("Term/POS combination not supported yet: " + patternToken);
        }
    } else if (termQuery != null) {
        return termQuery;
    } else if (posQuery != null) {
        return posQuery;
    }
    throw new UnsupportedPatternRuleException("Neither POS tag nor term set for element: " + patternToken);
}
Also used : SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 8 with SpanQuery

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

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

the class SynonymTokenizer method testSpanHighlighting.

public void testSpanHighlighting() throws Exception {
    Query query1 = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "wordx")), new SpanTermQuery(new Term(FIELD_NAME, "wordy")) }, 1, false);
    Query query2 = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "wordy")), new SpanTermQuery(new Term(FIELD_NAME, "wordc")) }, 1, false);
    BooleanQuery.Builder bquery = new BooleanQuery.Builder();
    bquery.add(query1, Occur.SHOULD);
    bquery.add(query2, Occur.SHOULD);
    doSearching(bquery.build());
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            mode = QUERY;
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
        }
    };
    helper.run();
    assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 7);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) Query(org.apache.lucene.search.Query) SpanPayloadCheckQuery(org.apache.lucene.queries.payloads.SpanPayloadCheckQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 10 with SpanQuery

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

the class MissesTest method testSpanNearQuery.

public void testSpanNearQuery() throws IOException, InvalidTokenOffsetsException {
    try (Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)) {
        final Query query = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("test", "foo")), new SpanTermQuery(new Term("test", "bar")) }, 0, true);
        final Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(query));
        assertEquals("this is a <B>foo</B> <B>bar</B> example", highlighter.getBestFragment(analyzer, "test", "this is a foo bar example"));
        assertNull(highlighter.getBestFragment(analyzer, "test", "this does not match"));
    }
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Aggregations

SpanQuery (org.apache.lucene.search.spans.SpanQuery)316 Test (org.junit.Test)217 Term (org.apache.lucene.index.Term)127 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)126 KrillIndex (de.ids_mannheim.korap.KrillIndex)110 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)65 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)52 Result (de.ids_mannheim.korap.response.Result)48 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)39 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)35 ArrayList (java.util.ArrayList)34 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)32 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)31 Query (org.apache.lucene.search.Query)30 IndexReader (org.apache.lucene.index.IndexReader)19 BooleanQuery (org.apache.lucene.search.BooleanQuery)19 IndexSearcher (org.apache.lucene.search.IndexSearcher)18 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)18 SpanMultiTermQueryWrapper (org.apache.lucene.search.spans.SpanMultiTermQueryWrapper)18 Document (org.apache.lucene.document.Document)17