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;
}
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());
}
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());
}
}
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();
}
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));
}
Aggregations