Search in sources :

Example 11 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project jena by apache.

the class TextIndexLucene method parseQuery.

private Query parseQuery(String queryString, Analyzer analyzer) throws ParseException {
    QueryParser queryParser = getQueryParser(analyzer);
    queryParser.setAllowLeadingWildcard(true);
    Query query = queryParser.parse(queryString);
    return query;
}
Also used : ComplexPhraseQueryParser(org.apache.lucene.queryparser.complexPhrase.ComplexPhraseQueryParser) AnalyzingQueryParser(org.apache.lucene.queryparser.analyzing.AnalyzingQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query)

Example 12 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project jackrabbit-oak by apache.

the class LucenePropertyIndex method getLuceneRequest.

/**
     * Get the Lucene query for the given filter.
     *
     * @param plan index plan containing filter details
     * @param reader the Lucene reader
     * @return the Lucene query
     */
private static LuceneRequestFacade getLuceneRequest(IndexPlan plan, IndexAugmentorFactory augmentorFactory, IndexReader reader) {
    FulltextQueryTermsProvider augmentor = getIndexAgumentor(plan, augmentorFactory);
    List<Query> qs = new ArrayList<Query>();
    Filter filter = plan.getFilter();
    FullTextExpression ft = filter.getFullTextConstraint();
    PlanResult planResult = getPlanResult(plan);
    IndexDefinition defn = planResult.indexDefinition;
    Analyzer analyzer = defn.getAnalyzer();
    if (ft == null) {
    // there might be no full-text constraint
    // when using the LowCostLuceneIndexProvider
    // which is used for testing
    } else {
        qs.add(getFullTextQuery(plan, ft, analyzer, augmentor));
    }
    //Check if native function is supported
    PropertyRestriction pr = null;
    if (defn.hasFunctionDefined()) {
        pr = filter.getPropertyRestriction(defn.getFunctionName());
    }
    if (pr != null) {
        String query = String.valueOf(pr.first.getValue(pr.first.getType()));
        QueryParser queryParser = new QueryParser(VERSION, "", analyzer);
        if (query.startsWith("mlt?")) {
            String mltQueryString = query.replace("mlt?", "");
            if (reader != null) {
                Query moreLikeThis = MoreLikeThisHelper.getMoreLikeThis(reader, analyzer, mltQueryString);
                if (moreLikeThis != null) {
                    qs.add(moreLikeThis);
                }
            }
        } else if (query.startsWith("spellcheck?")) {
            String spellcheckQueryString = query.replace("spellcheck?", "");
            if (reader != null) {
                return new LuceneRequestFacade<SpellcheckHelper.SpellcheckQuery>(SpellcheckHelper.getSpellcheckQuery(spellcheckQueryString, reader));
            }
        } else if (query.startsWith("suggest?")) {
            String suggestQueryString = query.replace("suggest?", "");
            if (reader != null) {
                return new LuceneRequestFacade<SuggestHelper.SuggestQuery>(SuggestHelper.getSuggestQuery(suggestQueryString));
            }
        } else {
            try {
                qs.add(queryParser.parse(query));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    } else if (planResult.evaluateNonFullTextConstraints()) {
        addNonFullTextConstraints(qs, plan, reader);
    }
    if (qs.size() == 0 && plan.getSortOrder() != null) {
        //This case indicates that query just had order by and no
        //property restriction defined. In this case property
        //existence queries for each sort entry
        List<OrderEntry> orders = removeNativeSort(plan.getSortOrder());
        for (int i = 0; i < orders.size(); i++) {
            OrderEntry oe = orders.get(i);
            PropertyDefinition pd = planResult.getOrderedProperty(i);
            PropertyRestriction orderRest = new PropertyRestriction();
            orderRest.propertyName = oe.getPropertyName();
            Query q = createQuery(orderRest, pd);
            if (q != null) {
                qs.add(q);
            }
        }
    }
    if (qs.size() == 0) {
        if (reader == null) {
            //just return match all queries
            return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
        }
        //be returned (if the index definition has a single rule)
        if (planResult.evaluateNodeTypeRestriction()) {
            return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
        }
        throw new IllegalStateException("No query created for filter " + filter);
    }
    return performAdditionalWraps(qs);
}
Also used : PropertyRestriction(org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction) PlanResult(org.apache.jackrabbit.oak.plugins.index.lucene.IndexPlanner.PlanResult) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SuggestHelper(org.apache.jackrabbit.oak.plugins.index.lucene.util.SuggestHelper) ArrayList(java.util.ArrayList) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Filter(org.apache.jackrabbit.oak.spi.query.Filter) FullTextExpression(org.apache.jackrabbit.oak.query.fulltext.FullTextExpression) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 13 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project lucene-solr by apache.

the class EnwikiQueryMaker method createQueries.

/**
   * Parse the strings containing Lucene queries.
   * 
   * @param qs array of strings containing query expressions
   * @param a analyzer to use when parsing queries
   * @return array of Lucene queries
   */
private static Query[] createQueries(List<Object> qs, Analyzer a) {
    QueryParser qp = new QueryParser(DocMaker.BODY_FIELD, a);
    List<Object> queries = new ArrayList<>();
    for (int i = 0; i < qs.size(); i++) {
        try {
            Object query = qs.get(i);
            Query q = null;
            if (query instanceof String) {
                q = qp.parse((String) query);
            } else if (query instanceof Query) {
                q = (Query) query;
            } else {
                System.err.println("Unsupported Query Type: " + query);
            }
            if (q != null) {
                queries.add(q);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return queries.toArray(new Query[0]);
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) ArrayList(java.util.ArrayList)

Example 14 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project lucene-solr by apache.

the class FileBasedQueryMaker method prepareQueries.

@Override
protected Query[] prepareQueries() throws Exception {
    Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer"));
    String defaultField = config.get("file.query.maker.default.field", DocMaker.BODY_FIELD);
    QueryParser qp = new QueryParser(defaultField, anlzr);
    qp.setAllowLeadingWildcard(true);
    List<Query> qq = new ArrayList<>();
    String fileName = config.get("file.query.maker.file", null);
    if (fileName != null) {
        Path path = Paths.get(fileName);
        Reader reader = null;
        // note: we use a decoding reader, so if your queries are screwed up you know
        if (Files.exists(path)) {
            reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
        } else {
            //see if we can find it as a resource
            InputStream asStream = FileBasedQueryMaker.class.getClassLoader().getResourceAsStream(fileName);
            if (asStream != null) {
                reader = IOUtils.getDecodingReader(asStream, StandardCharsets.UTF_8);
            }
        }
        if (reader != null) {
            try {
                BufferedReader buffered = new BufferedReader(reader);
                String line = null;
                int lineNum = 0;
                while ((line = buffered.readLine()) != null) {
                    line = line.trim();
                    if (line.length() != 0 && !line.startsWith("#")) {
                        try {
                            qq.add(qp.parse(line));
                        } catch (ParseException e) {
                            System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
                        }
                    }
                    lineNum++;
                }
            } finally {
                reader.close();
            }
        } else {
            System.err.println("No Reader available for: " + fileName);
        }
    }
    return qq.toArray(new Query[qq.size()]);
}
Also used : Path(java.nio.file.Path) Query(org.apache.lucene.search.Query) ArrayList(java.util.ArrayList) Analyzer(org.apache.lucene.analysis.Analyzer) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 15 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project lucene-solr by apache.

the class LongToEnglishQueryMaker method setConfig.

@Override
public void setConfig(Config config) throws Exception {
    Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer", StandardAnalyzer.class.getName()));
    parser = new QueryParser(DocMaker.BODY_FIELD, anlzr);
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer)

Aggregations

QueryParser (org.apache.lucene.queryparser.classic.QueryParser)67 Query (org.apache.lucene.search.Query)46 IndexSearcher (org.apache.lucene.search.IndexSearcher)30 Document (org.apache.lucene.document.Document)25 IOException (java.io.IOException)19 Analyzer (org.apache.lucene.analysis.Analyzer)19 IndexReader (org.apache.lucene.index.IndexReader)18 TopDocs (org.apache.lucene.search.TopDocs)18 ScoreDoc (org.apache.lucene.search.ScoreDoc)17 ArrayList (java.util.ArrayList)14 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)13 ParseException (org.apache.lucene.queryparser.classic.ParseException)12 TermQuery (org.apache.lucene.search.TermQuery)11 Term (org.apache.lucene.index.Term)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 WildcardQuery (org.apache.lucene.search.WildcardQuery)6 EnglishAnalyzer (org.apache.lucene.analysis.en.EnglishAnalyzer)5 IndexWriter (org.apache.lucene.index.IndexWriter)5 ScoredDocuments (io.anserini.rerank.ScoredDocuments)4