本文整理汇总了Java中org.apache.lucene.queryparser.flexible.standard.StandardQueryParser类的典型用法代码示例。如果您正苦于以下问题:Java StandardQueryParser类的具体用法?Java StandardQueryParser怎么用?Java StandardQueryParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StandardQueryParser类属于org.apache.lucene.queryparser.flexible.standard包,在下文中一共展示了StandardQueryParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: qs
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
/**
* Tries to parse a query string in order to check if it is valid.
* @param query a Lucene query string
* @return the query if valid, or '*' if invalid
*/
static String qs(String query) {
if (StringUtils.isBlank(query) || "*".equals(query.trim())) {
return "*";
}
query = query.trim();
if (query.length() > 1 && query.startsWith("*")) {
query = query.substring(1);
}
try {
StandardQueryParser parser = new StandardQueryParser();
parser.setAllowLeadingWildcard(false);
parser.parse(query, "");
} catch (Exception ex) {
logger.warn("Failed to parse query string '{}'.", query);
query = "*";
}
return query.trim();
}
示例2: parseQuery
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
@Override
public Query parseQuery(QueryContext context, IndexName name,
String defaultField, String query) {
FullTextIndexInfo index = getIndex(context.getSession(), name, null);
if (defaultField == null) {
defaultField = index.getDefaultFieldName();
}
StandardQueryParser parser = index.getParser();
try {
synchronized (parser) {
return parser.parse(query, defaultField);
}
}
catch (QueryNodeException ex) {
throw new FullTextQueryParseException(ex);
}
}
示例3: run
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
private int run(String[] args) throws Exception {
StandardQueryParser parser = new StandardQueryParser(Indexer.LUCENE_ANALYZER);
//try (Directory indexDirectory = new MMapDirectory(new File(indexName))) {
//try (Directory indexDirectory = new NIOFSDirectory(new File(indexName))) {
try (Directory indexDirectory = new SimpleFSDirectory(new File(indexName))) {
try (IndexReader indexReader = DirectoryReader.open(indexDirectory)) {
IndexSearcher indexSearcher = new IndexSearcher(indexReader, Executors.newFixedThreadPool(this.maxThreadCount));
BufferedReader queryReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type 'help' for help, type 'exit' or 'quit' to leave.");
System.out.flush();
String queryExpr;
do {
System.out.print(">>> ");
System.out.flush();
queryExpr = queryReader.readLine();
} while (queryExpr != null && processQuery(indexSearcher, parser, queryExpr));
}
}
return 0;
}
示例4: testRangeParsing
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
@Test
public void testRangeParsing() throws Exception {
NumericConfig numericConfig = new NumericConfig(8, NumberFormat.getNumberInstance(Locale.ENGLISH), FieldType.NumericType.FLOAT);
HashMap<String, NumericConfig> numericConfigMap = new HashMap<String, NumericConfig>();
numericConfigMap.put("reflec_7", numericConfig);
numericConfigMap.put("reflec_8", numericConfig);
numericConfigMap.put("reflec_9", numericConfig);
StandardQueryParser parser = new StandardQueryParser();
parser.setNumericConfigMap(numericConfigMap);
Query query1 = parser.parse("reflec_8:[0.0 TO 1.0]", "x");
assertEquals(NumericRangeQuery.class, query1.getClass());
Query query2 = parser.parse("reflec_8:[0.0 TO 1.0] AND reflec_9:[0.2 TO 0.6]^3.1", "x");
assertEquals(BooleanQuery.class, query2.getClass());
BooleanClause clause1 = ((BooleanQuery) query2).getClauses()[0];
BooleanClause clause2 = ((BooleanQuery) query2).getClauses()[1];
NumericRangeQuery<Float> nrq1 = NumericRangeQuery.newFloatRange("reflec_8", 8, 0.0F, 1.0F, true, true);
NumericRangeQuery<Float> nrq2 = NumericRangeQuery.newFloatRange("reflec_9", 8, 0.2F, 0.6F, true, true);
nrq2.setBoost(3.1F);
assertEquals(nrq1, clause1.getQuery());
assertEquals(BooleanClause.Occur.MUST, clause1.getOccur());
assertEquals(nrq2, clause2.getQuery());
assertEquals(BooleanClause.Occur.MUST, clause2.getOccur());
}
示例5: ExpertRevisionSearchCommand
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
private ExpertRevisionSearchCommand(DirectoryManager.DirectoryPath path, String qry, Configuration configuration)
throws QueryNodeException {
super(path, ResultList.ResultType.REVISION);
setLanguage(extractLanguage(qry));
Map<String, NumericConfig> nums = new HashMap<>();
/*StandardQueryParser parser = new StandardQueryParser(getAnalyzer());*/
StandardQueryParser parser = new StandardQueryParser();
parser.setAllowLeadingWildcard(true);
parser.setAnalyzer(getAnalyzer());
if(!StringUtils.hasText(qry)) {
throw new ParseException(new MessageImpl("EMPTY_QUERY"));
}
query = parser.parse(qry, "general");
// No matter if we have configuraion or not we'll parse the query twice. This second parsing will add analyzers for known keys like key.id
// as well as fields found from configuration if configuration is provided
addAnalyzersAndConfigs(query, nums, configuration);
parser.setAnalyzer(getAnalyzer());
parser.setNumericConfigMap(nums);
query = parser.parse(qry, "general");
}
示例6: getKeywords
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
public static List<String> getKeywords(final String q, final String[] fields) {
final List<String> keywords = new ArrayList<>();
final List<TermQuery> termQueryList;
try {
final StandardQueryParser parser = new StandardQueryParser();
parser.setDefaultOperator(StandardQueryConfigHandler.Operator.AND);
termQueryList = getTermQueryList(parser.parse(q, "default"), fields);
} catch (final Exception e) {
return keywords;
}
for (final TermQuery tq : termQueryList) {
final String text = tq.getTerm().text();
if (0 == text.length()) {
continue;
}
if (keywords.contains(text)) {
continue;
}
keywords.add(text);
}
return keywords;
}
示例7: qsParsed
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
static Query qsParsed(String query) {
if (StringUtils.isBlank(query) || "*".equals(query.trim())) {
return null;
}
try {
StandardQueryParser parser = new StandardQueryParser();
parser.setAllowLeadingWildcard(false);
return parser.parse(query, "");
} catch (Exception ex) {
logger.warn("Failed to parse query string '{}'.", query);
}
return null;
}
示例8: LucenePatchQuery
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
public LucenePatchQuery(final File datasetDir, DatasetDescriptor dsDescriptor, FeatureType[] effectiveFeatureTypes) throws IOException {
this.effectiveFeatureTypes = effectiveFeatureTypes;
parser = new StandardQueryParser(DsIndexer.LUCENE_ANALYZER);
NumericConfiguration numConf = new NumericConfiguration(PRECISION_STEP);
parser.setNumericConfigMap(numConf.getNumericConfigMap(dsDescriptor));
//try (Directory indexDirectory = new MMapDirectory(new File(datasetDir, INDEX_NAME))) {
//try (Directory indexDirectory = new NIOFSDirectory(new File(datasetDir, INDEX_NAME))) {
try (Directory indexDirectory = new SimpleFSDirectory(new File(datasetDir, INDEX_NAME))) {
indexReader = DirectoryReader.open(indexDirectory);
indexSearcher = new IndexSearcher(indexReader, Executors.newFixedThreadPool(MAX_THREAD_COUNT));
}
}
示例9: run
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
private int run(String[] args) throws Exception {
if (!parseCommandLine(args)) {
return 1;
}
dsDescriptor = DatasetDescriptor.read(new File(datasetDir, "ds-descriptor.xml"));
StandardQueryParser parser = new StandardQueryParser(DsIndexer.LUCENE_ANALYZER);
NumericConfiguration numConf = new NumericConfiguration(precisionStep);
parser.setNumericConfigMap(numConf.getNumericConfigMap(dsDescriptor));
//try (Directory indexDirectory = new MMapDirectory(new File(datasetDir, indexName))) {
//try (Directory indexDirectory = new NIOFSDirectory(new File(datasetDir, indexName))) {
try (Directory indexDirectory = new SimpleFSDirectory(new File(datasetDir, indexName))) {
try (IndexReader indexReader = DirectoryReader.open(indexDirectory)) {
IndexSearcher indexSearcher = new IndexSearcher(indexReader, Executors.newFixedThreadPool(this.maxThreadCount));
BufferedReader queryReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type 'help' for help, type 'exit' or 'quit' to leave.");
System.out.flush();
String queryExpr;
do {
System.out.print(">>> ");
System.out.flush();
queryExpr = queryReader.readLine();
} while (queryExpr != null && processQuery(indexSearcher, parser, queryExpr));
}
}
return 0;
}
示例10: testSimple
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
public void testSimple() throws Exception {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
StandardQueryParser parser = new StandardQueryParser(analyzer);
Query q = null;
try {
q = parser.parse("(agile OR extreme) AND methodology", "subject");
}
catch (QueryNodeException exc) {
// TODO: handle exc
}
LOGGER.info("parsed " + q);
}
示例11: testPhraseQuery
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
public void testPhraseQuery() throws Exception {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
StandardQueryParser parser = new CustomFlexibleQueryParser(analyzer);
Query query = parser.parse("singleTerm", "subject");
assertTrue("TermQuery", query instanceof TermQuery);
query = parser.parse("\"a phrase test\"", "subject");
LOGGER.info("got query=" + query);
assertTrue("SpanNearQuery", query instanceof SpanNearQuery);
}
示例12: queryParserSearch
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
@Test
public void queryParserSearch() throws IOException, QueryNodeException {
Document doc = new Document();
doc.add(new Field("content", "The quick brown fox jumps over the lazy dog",
TextField.TYPE_STORED));
writer.addDocument(doc);
searcher = new IndexSearcher(DirectoryReader.open(writer, false));
Query query = new StandardQueryParser(skosAnalyzer).parse("\"fox jumps\"",
"content");
Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
Assert.assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
Assert.assertEquals("org.apache.lucene.search.MultiPhraseQuery", query
.getClass().getName());
query = new StandardQueryParser(new StandardAnalyzer(matchVersion)).parse(
"\"fox jumps\"", "content");
Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
Assert.assertEquals("content:\"fox jumps\"", query.toString());
Assert.assertEquals("org.apache.lucene.search.PhraseQuery", query
.getClass().getName());
}
示例13: queryParserSearch
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
@Test
public void queryParserSearch() throws IOException, QueryNodeException {
Document doc = new Document();
doc.add(new Field("content", "The quick brown fox jumps over the lazy dog",
TextField.TYPE_STORED));
writer.addDocument(doc);
searcher = new IndexSearcher(DirectoryReader.open(writer, false));
Query query = new SKOSStandardQueryParser(skosAnalyzer).parse("\"fox jumps\"",
"content");
Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
Assert.assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
Assert.assertEquals("org.apache.lucene.search.MultiPhraseQuery", query
.getClass().getName());
query = new StandardQueryParser(new StandardAnalyzer(matchVersion)).parse(
"\"fox jumps\"", "content");
Assert.assertEquals(1, TestUtil.hitCount(searcher, query));
Assert.assertEquals("content:\"fox jumps\"", query.toString());
Assert.assertEquals("org.apache.lucene.search.PhraseQuery", query
.getClass().getName());
}
示例14: getParser
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
public StandardQueryParser getParser() {
return parser;
}
示例15: setParser
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; //导入依赖的package包/类
public void setParser(StandardQueryParser parser) {
this.parser = parser;
}