Search in sources :

Example 6 with StandardQueryParser

use of org.apache.lucene.queryparser.flexible.standard.StandardQueryParser in project Anserini by castorini.

the class SearchCollection method searchBackgroundLinking.

public <K> ScoredDocuments searchBackgroundLinking(IndexSearcher searcher, K qid, String docid, RerankerCascade cascade) throws IOException {
    // Extract a list of analyzed terms from the document to compose a query.
    List<String> terms = BackgroundLinkingTopicReader.extractTerms(reader, docid, args.backgroundlinking_k, analyzer);
    // Since the terms are already analyzed, we just join them together and use the StandardQueryParser.
    Query docQuery;
    try {
        docQuery = new StandardQueryParser().parse(StringUtils.join(terms, " "), IndexArgs.CONTENTS);
    } catch (QueryNodeException e) {
        throw new RuntimeException("Unable to create a Lucene query comprised of terms extracted from query document!");
    }
    // Per track guidelines, no opinion or editorials. Filter out articles of these types.
    Query filter = new TermInSetQuery(WashingtonPostGenerator.WashingtonPostField.KICKER.name, new BytesRef("Opinions"), new BytesRef("Letters to the Editor"), new BytesRef("The Post's View"));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(filter, BooleanClause.Occur.MUST_NOT);
    builder.add(docQuery, BooleanClause.Occur.MUST);
    Query query = builder.build();
    // Search using constructed query.
    TopDocs rs;
    if (args.arbitraryScoreTieBreak) {
        rs = searcher.search(query, (isRerank && args.rf_qrels == null) ? args.rerankcutoff : args.hits);
    } else {
        rs = searcher.search(query, (isRerank && args.rf_qrels == null) ? args.rerankcutoff : args.hits, BREAK_SCORE_TIES_BY_DOCID, true);
    }
    RerankerContext context = new RerankerContext<>(searcher, qid, query, docid, StringUtils.join(", ", terms), terms, null, args);
    // Run the existing cascade.
    ScoredDocuments docs = cascade.run(ScoredDocuments.fromTopDocs(rs, searcher), context);
    // Perform post-processing (e.g., date filter, dedupping, etc.) as a final step.
    return new NewsBackgroundLinkingReranker().rerank(docs, context);
}
Also used : QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) NewsBackgroundLinkingReranker(io.anserini.rerank.lib.NewsBackgroundLinkingReranker) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ScoredDocuments(io.anserini.rerank.ScoredDocuments) TopDocs(org.apache.lucene.search.TopDocs) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser) BytesRef(org.apache.lucene.util.BytesRef) RerankerContext(io.anserini.rerank.RerankerContext)

Example 7 with StandardQueryParser

use of org.apache.lucene.queryparser.flexible.standard.StandardQueryParser in project stargate-core by tuplejump.

the class LuceneCondition method query.

/**
 * {@inheritDoc}
 */
@Override
public Query query(Options schema) {
    if (query == null) {
        throw new IllegalArgumentException("Query statement required");
    }
    try {
        StandardQueryParser parser = new StandardQueryParser(schema.analyzer);
        parser.setNumericConfigMap(schema.numericFieldOptions);
        parser.setAllowLeadingWildcard(true);
        Query luceneQuery = parser.parse(query, getDefaultField(schema));
        if (logger.isDebugEnabled()) {
            logger.debug("Lucene query is {}", luceneQuery);
        }
        return luceneQuery;
    } catch (Exception e) {
        throw new RuntimeException("Error while parsing lucene syntax query", e);
    }
}
Also used : Query(org.apache.lucene.search.Query) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser)

Example 8 with StandardQueryParser

use of org.apache.lucene.queryparser.flexible.standard.StandardQueryParser in project lucene-skos by behas.

the class SKOSStandardQueryParserTest method queryParserSearch.

@Test
public void queryParserSearch() throws IOException, QueryNodeException {
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", TextField.TYPE_STORED));
    writer.addDocument(doc);
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    Query query = new SKOSStandardQueryParser(skosAnalyzer).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
    assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());
    query = new StandardQueryParser(new StandardAnalyzer()).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
    assertEquals("content:\"fox jumps\"", query.toString());
    assertEquals("org.apache.lucene.search.PhraseQuery", query.getClass().getName());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) Query(org.apache.lucene.search.Query) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Document(org.apache.lucene.document.Document) SKOSStandardQueryParser(at.ac.univie.mminf.luceneSKOS.queryparser.flexible.standard.SKOSStandardQueryParser) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser) SKOSStandardQueryParser(at.ac.univie.mminf.luceneSKOS.queryparser.flexible.standard.SKOSStandardQueryParser) Test(org.junit.Test)

Example 9 with StandardQueryParser

use of org.apache.lucene.queryparser.flexible.standard.StandardQueryParser in project lucene-skos by behas.

the class SKOSLabelFilterTest method queryParserSearch.

@Test
public void queryParserSearch() throws IOException, QueryNodeException {
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", TextField.TYPE_STORED));
    writer.addDocument(doc);
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    Query query = new StandardQueryParser(skosAnalyzer).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
    assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());
    query = new StandardQueryParser(new StandardAnalyzer()).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
    assertEquals("content:\"fox jumps\"", query.toString());
    assertEquals("org.apache.lucene.search.PhraseQuery", query.getClass().getName());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Document(org.apache.lucene.document.Document) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser) Test(org.junit.Test)

Example 10 with StandardQueryParser

use of org.apache.lucene.queryparser.flexible.standard.StandardQueryParser in project lucene-skos by behas.

the class SKOSLabelFilterTest method testTermQuery.

@Test
public void testTermQuery() throws IOException, QueryNodeException {
    Document doc = new Document();
    doc.add(new Field("content", "I work for the united nations", TextField.TYPE_STORED));
    writer.addDocument(doc);
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    StandardQueryParser parser = new StandardQueryParser(new SimpleAnalyzer());
    Query query = parser.parse("united nations", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) SimpleAnalyzer(org.apache.lucene.analysis.core.SimpleAnalyzer) Document(org.apache.lucene.document.Document) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser) Test(org.junit.Test)

Aggregations

StandardQueryParser (org.apache.lucene.queryparser.flexible.standard.StandardQueryParser)10 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)6 Query (org.apache.lucene.search.Query)5 Document (org.apache.lucene.document.Document)3 Field (org.apache.lucene.document.Field)3 TextField (org.apache.lucene.document.TextField)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 TermQuery (org.apache.lucene.search.TermQuery)3 Test (org.junit.Test)3 ListFilter (io.vertigo.dynamo.collections.ListFilter)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Builder (org.apache.lucene.search.BooleanQuery.Builder)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 SKOSStandardQueryParser (at.ac.univie.mminf.luceneSKOS.queryparser.flexible.standard.SKOSStandardQueryParser)1 RerankerContext (io.anserini.rerank.RerankerContext)1 ScoredDocuments (io.anserini.rerank.ScoredDocuments)1 NewsBackgroundLinkingReranker (io.anserini.rerank.lib.NewsBackgroundLinkingReranker)1 LuceneQueryException (org.apache.geode.cache.lucene.LuceneQueryException)1 SimpleAnalyzer (org.apache.lucene.analysis.core.SimpleAnalyzer)1