Search in sources :

Example 1 with QueryParser

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

the class LuceneSearcher method doSearch.

private int doSearch(String searchPhrase, int maxNumberOfHits, Version luceneVersion) throws NullPointerException, ParseException, IOException {
    LOG.trace("*** Search Phrase: {} ***", searchPhrase);
    QueryParser parser = new QueryParser("contents", analyzer);
    Query query = parser.parse(searchPhrase);
    TopScoreDocCollector collector = TopScoreDocCollector.create(maxNumberOfHits);
    indexSearcher.search(query, collector);
    hits = collector.topDocs().scoreDocs;
    LOG.trace("*** Search generated {} hits ***", hits.length);
    return hits.length;
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector)

Example 2 with QueryParser

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

the class TopHitsAggregatorTests method testTopLevel.

public void testTopLevel() throws Exception {
    Aggregation result;
    if (randomBoolean()) {
        result = testCase(new MatchAllDocsQuery(), topHits("_name").sort("string", SortOrder.DESC));
    } else {
        Query query = new QueryParser("string", new KeywordAnalyzer()).parse("d^1000 c^100 b^10 a^1");
        result = testCase(query, topHits("_name"));
    }
    SearchHits searchHits = ((TopHits) result).getHits();
    assertEquals(3L, searchHits.getTotalHits());
    assertEquals("3", searchHits.getAt(0).getId());
    assertEquals("type", searchHits.getAt(0).getType());
    assertEquals("2", searchHits.getAt(1).getId());
    assertEquals("type", searchHits.getAt(1).getType());
    assertEquals("1", searchHits.getAt(2).getId());
    assertEquals("type", searchHits.getAt(2).getType());
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SearchHits(org.elasticsearch.search.SearchHits) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 3 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 4 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 5 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)111 Query (org.apache.lucene.search.Query)79 IndexSearcher (org.apache.lucene.search.IndexSearcher)52 Document (org.apache.lucene.document.Document)44 TopDocs (org.apache.lucene.search.TopDocs)39 ParseException (org.apache.lucene.queryparser.classic.ParseException)37 IOException (java.io.IOException)34 Analyzer (org.apache.lucene.analysis.Analyzer)31 ScoreDoc (org.apache.lucene.search.ScoreDoc)31 IndexReader (org.apache.lucene.index.IndexReader)25 ArrayList (java.util.ArrayList)24 BooleanQuery (org.apache.lucene.search.BooleanQuery)23 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)22 TermQuery (org.apache.lucene.search.TermQuery)19 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)13 HashSet (java.util.HashSet)10 Directory (org.apache.lucene.store.Directory)10 IndexWriter (org.apache.lucene.index.IndexWriter)8 IndexableField (org.apache.lucene.index.IndexableField)8 Term (org.apache.lucene.index.Term)7