Search in sources :

Example 16 with QueryParser

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

the class SimpleQueryMaker method prepareQueries.

/**
   * Prepare the queries for this test.
   * Extending classes can override this method for preparing different queries. 
   * @return prepared queries.
   * @throws Exception if cannot prepare the queries.
   */
@Override
protected Query[] prepareQueries() throws Exception {
    // analyzer (default is standard analyzer)
    Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer"));
    QueryParser qp = new QueryParser(DocMaker.BODY_FIELD, anlzr);
    ArrayList<Query> qq = new ArrayList<>();
    Query q1 = new TermQuery(new Term(DocMaker.ID_FIELD, "doc2"));
    qq.add(q1);
    Query q2 = new TermQuery(new Term(DocMaker.BODY_FIELD, "simple"));
    qq.add(q2);
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(q1, Occur.MUST);
    bq.add(q2, Occur.MUST);
    qq.add(bq.build());
    qq.add(qp.parse("synthetic body"));
    qq.add(qp.parse("\"synthetic body\""));
    qq.add(qp.parse("synthetic text"));
    qq.add(qp.parse("\"synthetic text\""));
    qq.add(qp.parse("\"synthetic text\"~3"));
    qq.add(qp.parse("zoom*"));
    qq.add(qp.parse("synth*"));
    return qq.toArray(new Query[0]);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 17 with QueryParser

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

the class SimpleQQParser method parse.

/* (non-Javadoc)
   * @see org.apache.lucene.benchmark.quality.QualityQueryParser#parse(org.apache.lucene.benchmark.quality.QualityQuery)
   */
@Override
public Query parse(QualityQuery qq) throws ParseException {
    QueryParser qp = queryParser.get();
    if (qp == null) {
        qp = new QueryParser(indexField, new StandardAnalyzer());
        queryParser.set(qp);
    }
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    for (int i = 0; i < qqNames.length; i++) bq.add(qp.parse(QueryParserBase.escape(qq.getValue(qqNames[i]))), BooleanClause.Occur.SHOULD);
    return bq.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) QualityQueryParser(org.apache.lucene.benchmark.quality.QualityQueryParser) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer)

Example 18 with QueryParser

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

the class UserInputQueryBuilder method getQuery.

/* (non-Javadoc)
    * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
    */
@Override
public Query getQuery(Element e) throws ParserException {
    String text = DOMUtils.getText(e);
    try {
        Query q = null;
        if (unSafeParser != null) {
            //synchronize on unsafe parser
            synchronized (unSafeParser) {
                q = unSafeParser.parse(text);
            }
        } else {
            String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField);
            //Create new parser
            QueryParser parser = createQueryParser(fieldName, analyzer);
            q = parser.parse(text);
        }
        float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
        return new BoostQuery(q, boost);
    } catch (ParseException e1) {
        throw new ParserException(e1.getMessage());
    }
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 19 with QueryParser

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

the class SearchFiles method main.

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);
    }
    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }
    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();
    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    QueryParser parser = new QueryParser(field, analyzer);
    while (true) {
        if (queries == null && queryString == null) {
            // prompt the user
            System.out.println("Enter query: ");
        }
        String line = queryString != null ? queryString : in.readLine();
        if (line == null || line.length() == -1) {
            break;
        }
        line = line.trim();
        if (line.length() == 0) {
            break;
        }
        Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));
        if (repeat > 0) {
            // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }
        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);
        if (queryString != null) {
            break;
        }
    }
    reader.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) InputStreamReader(java.io.InputStreamReader) Query(org.apache.lucene.search.Query) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) BufferedReader(java.io.BufferedReader) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Date(java.util.Date)

Example 20 with QueryParser

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

the class QueryParserTestBase method testAutoGeneratePhraseQueriesOn.

public void testAutoGeneratePhraseQueriesOn() throws Exception {
    // individual CJK chars as terms
    SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
    PhraseQuery expected = new PhraseQuery("field", "中", "国");
    CommonQueryParserConfiguration qp = getParserConfig(analyzer);
    if (qp instanceof QueryParser) {
        // Always true, since TestStandardQP overrides this method
        // LUCENE-7533
        ((QueryParser) qp).setSplitOnWhitespace(true);
    }
    setAutoGeneratePhraseQueries(qp, true);
    assertEquals(expected, getQuery("中国", qp));
}
Also used : TestQueryParser(org.apache.lucene.queryparser.classic.TestQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) CommonQueryParserConfiguration(org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)

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