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