本文整理汇总了Java中org.apache.lucene.queryparser.classic.QueryParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java QueryParser.parse方法的具体用法?Java QueryParser.parse怎么用?Java QueryParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.queryparser.classic.QueryParser
的用法示例。
在下文中一共展示了QueryParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deleteIndexesByField
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
@Override
public void deleteIndexesByField(String field) {
if (!isNullOrEmpty(field)) {
try {
Query query;
QueryParser parser = new QueryParser(Version.LUCENE_4_9, field, analyzer);
parser.setAllowLeadingWildcard(true);
try {
query = parser.parse("*");
} catch (ParseException e) {
throw new IndexingException(errorMessage(
"could not remove full-text index for value " + field, 0));
}
synchronized (this) {
indexWriter.deleteDocuments(query);
commit();
}
} catch (IOException ioe) {
throw new IndexingException(errorMessage(
"could not remove full-text index for value " + field, 0));
} catch (VirtualMachineError vme) {
handleVirtualMachineError(vme);
}
}
}
示例2: main
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_DIRECTORY)));
IndexSearcher indexSearcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser queryParser = new QueryParser(FIELD_CONTENTS, analyzer);
String searchString = "shakespeare";
Query query = queryParser.parse(searchString);
TopDocs results = indexSearcher.search(query, 5);
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
System.out.println(numTotalHits + " total matching documents");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = indexSearcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("path") + " score=" + hits[i].score);
}
}
示例3: search
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
public SearchResult search(String index, String queryString, int page) {
SearchResult searchResult = null;
try {
IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(Properties.getProperties().getProperty(Values.INDEX_LOCATION, Values.DEFAULT_INDEX_LOCATION))));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
// Search over the titles only for the moment
QueryParser parser = new QueryParser(index, analyzer);
Query query = parser.parse(queryString);
searchResult = this.doPagingSearch(reader, searcher, query, queryString, page);
reader.close();
}
catch(Exception ex) {}
return searchResult;
}
示例4: synTokenQuery
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
private void synTokenQuery(String search, final int numbOfResults, final double minLuceneScore,
Map<String, Float> result, IndexSearcher searcher) throws ParseException, IOException {
QueryParser parser = new QueryParser(Version.LUCENE_46, "surfaceFormTokens",
new StandardAnalyzer(Version.LUCENE_46));
search = QueryParser.escape(search);
Query q = parser.parse(search);
/*
* Works only in String field!!
*/
// Query q = new FuzzyQuery(new Term("surfaceFormTokens",
// QueryParser.escape(search)), 2);
TopDocs top = searcher.search(q, numbOfResults);
for (ScoreDoc doc : top.scoreDocs) {
if (doc.score >= minLuceneScore) {
final String key = searcher.doc(doc.doc).get("conceptID");
if (result.getOrDefault(key, 0f) < doc.score) {
result.put(key, doc.score);
}
}
}
}
示例5: Search
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* 查询方法
* @throws IOException
* @throws CorruptIndexException
* @throws ParseException
*/
public List Search(String searchString,LuceneResultCollector luceneResultCollector) throws CorruptIndexException, IOException, ParseException{
//方法一:
System.out.println(this.indexSettings.getAnalyzer().getClass()+"----分词选择");
QueryParser q = new QueryParser(Version.LUCENE_44, "summary", this.indexSettings.getAnalyzer());
String search = new String(searchString.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(search+"----------搜索的词语dd");
Query query = q.parse(search);
//方法二:
/*
Term t = new Term("title", searchString);
TermQuery query = new TermQuery(t);
*/
System.out.println(query.toString()+"--------query.tostring");
ScoreDoc[] docs = this.indexSearcher.search(query,100).scoreDocs;
System.out.println("一共有:"+docs.length+"条记录");
List result = luceneResultCollector.collect(docs, this.indexSearcher);
return result;
}
示例6: getQuery
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
@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);
}
q.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
return q;
} catch (ParseException e1) {
throw new ParserException(e1.getMessage());
}
}
示例7: query
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* Returns the Lucene {@link Query} represented by the specified
* {@link Search} and key filter.
*
* @param expression
* the expression
* @param command
* the command
* @return a Lucene {@link Query}
*/
private Query query(String expression, ReadCommand command) {
try {
QueryParser queryParser = new QueryParser("query", this.indexOptions.search.defaultAnalyzer);
queryParser.setDateResolution(Resolution.SECOND);
Query searchQuery = queryParser.parse(expression);
return searchQuery;
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new FhirIndexException(e);
}
// TODO: mejorar las busquedas por tipo
// Optional<Query> maybeKeyRangeQuery = query(command);
// if (maybeKeyRangeQuery.isPresent()) {
// BooleanQuery.Builder builder = new BooleanQuery.Builder();
// builder.add(maybeKeyRangeQuery.get(), FILTER);
// builder.add(searchQuery, MUST);
// return builder.build();
// } else {
// }
}
示例8: queryIndex
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
public static LuceneSearchResult queryIndex(String keyword, int offset, int pagesize){
// 查询
List<Query> querys = new ArrayList<>();
if (keyword!=null && keyword.trim().length()>0) {
try {
//Analyzer analyzer = new SmartChineseAnalyzer();
Analyzer analyzer = new IKAnalyzer();
QueryParser parser = new QueryParser(ExcelUtil.KEYWORDS, analyzer);
Query shopNameQuery = parser.parse(keyword);
querys.add(shopNameQuery);
} catch (ParseException e) {
e.printStackTrace();
}
}
LuceneSearchResult result = search(querys, offset, pagesize);
return result;
}
示例9: getExplicitFieldQuery
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* Gets the lucene query for explicit lookup field
* @param analyzer
* @param fieldName
* @param fieldValue
* @param fieldID
* @param locale
* @return
*/
@Override
protected Query getExplicitFieldQuery(Analyzer analyzer,
String fieldName, String fieldValue, Integer fieldID, Locale locale) {
Query query= null;
QueryParser queryParser = new QueryParser(fieldName, analyzer);
//adds the type criteria
String externalFieldQueryString = fieldValue +
" AND " + LuceneUtil.EXTERNAL_INDEX_FIELDS.FIELDID +
LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + fieldID;
try {
query = queryParser.parse(externalFieldQueryString);
} catch (ParseException e) {
LOGGER.error("Parsing the external lookups query string fieldName " + fieldName + " fieldValue '" + fieldValue + "' failed with " + e.getMessage());
LOGGER.debug(ExceptionUtils.getStackTrace(e));
}
return query;
}
示例10: getNoExplicitFieldQuery
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* Preprocess the toBeProcessedString when no field is specified
* @param analyzer
* @param toBeProcessedString
* @param locale
* @return
*/
@Override
public Query getNoExplicitFieldQuery(Analyzer analyzer, String toBeProcessedString, Locale locale) {
Query query = null;
String queryString = searchNoExplicitField(analyzer, toBeProcessedString, locale);
if (queryString!=null && queryString.trim().length()>0) {
QueryParser queryParser = new QueryParser(LuceneUtil.getFieldName(SystemFields.ISSUENO), analyzer);
LOGGER.debug("List matches found for no explicit field " + queryString);
try {
query = queryParser.parse(queryString);
} catch (ParseException e) {
LOGGER.warn("Parsing without explicit field for " + queryParser + " field failed with " + e.getMessage());
LOGGER.debug(ExceptionUtils.getStackTrace(e));
}
}
return query;
}
示例11: getNoExlplicitFieldQuery
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* Gets the lucene query when no field is specified,
* that means search the entire localized index by label, default localized or locale
* @param analyzer
* @param toBeProcessedString
* @param fieldID
* @param locale
* @return
*/
@Override
protected Query getNoExlplicitFieldQuery(Analyzer analyzer,
String toBeProcessedString, Locale locale) {
QueryParser queryParser = new QueryParser(getLabelFieldName(), analyzer);
Query query = null;
try {
//search for both localized and not localized value
query = queryParser.parse(getLabelFieldName()+ LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + toBeProcessedString +
" OR " + locale.toString() + LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + toBeProcessedString +
" OR " + LuceneUtil.LIST_INDEX_FIELDS_LOCALIZED.DEFAULT_LOCALIZED + LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + toBeProcessedString);
} catch (ParseException e) {
LOGGER.error("Parsing the no field query string for fieldValue '" + toBeProcessedString + "' failed with " + e.getMessage());
LOGGER.debug(ExceptionUtils.getStackTrace(e));
}
return query;
}
示例12: getNoExplicitFieldQuery
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* Preprocess the toBeProcessedString when no field is specified
* @param analyzer
* @param toBeProcessedString
* @param locale
* @return
*/
@Override
public Query getNoExplicitFieldQuery(Analyzer analyzer, String toBeProcessedString, Locale locale) {
Query query = null;
String workItemsFound = searchNoExplicitField(analyzer, toBeProcessedString, locale);
if (workItemsFound!=null && !workItemsFound.equals(toBeProcessedString)) {
QueryParser queryParser = new QueryParser(LuceneUtil.getFieldName(SystemFields.ISSUENO), analyzer);
String queryString = LuceneUtil.getFieldName(SystemFields.ISSUENO) +
LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + workItemsFound;
LOGGER.debug(getLuceneFieldName() + " workItems found : " + workItemsFound);
try {
query = queryParser.parse(queryString);
} catch (ParseException e) {
LOGGER.warn("Parsing without explicit field the " + getLuceneFieldName() + " for " + queryString + " field failed with " + e.getMessage());
LOGGER.debug(ExceptionUtils.getStackTrace(e));
}
}
return query;
}
示例13: _fullTextQueryClauseFor
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
@SuppressWarnings("resource")
private Query _fullTextQueryClauseFor(final IndexDocumentFieldID fieldId,
final String text) {
IndexDocumentFieldConfig<?> fieldCfg = _fieldsConfigSet.getConfigFor(fieldId);
Analyzer analyzer = fieldCfg.isLanguageDependent() ? new LuceneLanguageDependentAnalyzer(_fieldsConfigSet) // language dependant analyzer
: new StandardAnalyzer(LuceneConstants.VERSION); // StandardAnalyzer!!!
Query outQry = null;
QueryParser qp = new QueryParser(LuceneConstants.VERSION,
fieldId.asString(),
analyzer);
try {
String luceneTextQry = Strings.customized("{}:{}",
fieldId.asString(),text);
outQry = qp.parse(luceneTextQry);
} catch(ParseException parseEx) {
log.error("Error parsing the term text search filter: {}",parseEx.getMessage(),
parseEx);
}
return outQry;
}
示例14: query
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
Query query( String keyOrNull, Object value, QueryContext contextOrNull )
{
if ( value instanceof Query )
{
return (Query) value;
}
QueryParser parser = new QueryParser( keyOrNull, analyzer );
parser.setAllowLeadingWildcard( true );
parser.setLowercaseExpandedTerms( toLowerCase );
if ( contextOrNull != null && contextOrNull.getDefaultOperator() != null )
{
parser.setDefaultOperator( contextOrNull.getDefaultOperator() );
}
try
{
return parser.parse( value.toString() );
}
catch ( ParseException e )
{
throw new RuntimeException( e );
}
}
示例15: getNoFieldQuerySearchableField
import org.apache.lucene.queryparser.classic.QueryParser; //导入方法依赖的package包/类
/**
* Gets the lucene query when no field is specified
* @param analyzer
* @param toBeProcessedString
* @param fieldID
* @param locale
* @return
*/
protected Query getNoFieldQuerySearchableField(Analyzer analyzer,
String toBeProcessedString, Integer fieldID, Locale locale) {
BooleanQuery booleanQuery = new BooleanQuery();
IExternalLookupLucene externalLookupField = (IExternalLookupLucene)FieldTypeManager.getFieldTypeRT(fieldID);
String[] searchableFields = externalLookupField.getSearchableFieldNames();
if (searchableFields!=null && searchableFields.length>0) {
for (String searchableField : searchableFields) {
QueryParser queryParser = new QueryParser(searchableField, analyzer);
Query externalLookupFieldQuery = null;
String externalFieldQueryString = toBeProcessedString +
" AND " + LuceneUtil.EXTERNAL_INDEX_FIELDS.FIELDID +
LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + fieldID;
try {
externalLookupFieldQuery = queryParser.parse(toBeProcessedString);
booleanQuery.add(externalLookupFieldQuery, BooleanClause.Occur.SHOULD);
} catch (ParseException e) {
LOGGER.error("Parsing the external lookups query string for fieldValue '" + externalFieldQueryString + "' failed with " + e.getMessage());
LOGGER.debug(ExceptionUtils.getStackTrace(e));
}
}
}
return booleanQuery;
}