當前位置: 首頁>>代碼示例>>Java>>正文


Java QueryParser.parse方法代碼示例

本文整理匯總了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);
        }
    }
}
 
開發者ID:dizitart,項目名稱:nitrite-database,代碼行數:27,代碼來源:LuceneService.java

示例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);
	}
}
 
開發者ID:PacktPublishing,項目名稱:Java-Data-Science-Cookbook,代碼行數:22,代碼來源:SearchFiles.java

示例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;
}
 
開發者ID:boyter,項目名稱:freemoz,代碼行數:20,代碼來源:Searcher.java

示例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);
			}
		}
	}
}
 
開發者ID:ag-sc,項目名稱:JLink,代碼行數:27,代碼來源:LuceneRetrieval.java

示例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;
}
 
開發者ID:zhangjikai,項目名稱:sdudoc,代碼行數:26,代碼來源:LuceneIndexSearch.java

示例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());
  }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:23,代碼來源:UserInputQueryBuilder.java

示例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 {
	// }
}
 
開發者ID:jmiddleton,項目名稱:cassandra-fhir-index,代碼行數:33,代碼來源:FhirIndexService.java

示例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;
}
 
開發者ID:xuxueli,項目名稱:xxl-search,代碼行數:18,代碼來源:LuceneUtil.java

示例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;
}
 
開發者ID:trackplus,項目名稱:Genji,代碼行數:27,代碼來源:ExternalListSearcher.java

示例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;
}
 
開發者ID:trackplus,項目名稱:Genji,代碼行數:24,代碼來源:AbstractListFieldSearcher.java

示例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;
}
 
開發者ID:trackplus,項目名稱:Genji,代碼行數:26,代碼來源:LocalizedListSearcher.java

示例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;
}
 
開發者ID:trackplus,項目名稱:Genji,代碼行數:26,代碼來源:AbstractAssociatedFieldSearcher.java

示例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;
}
 
開發者ID:opendata-euskadi,項目名稱:r01fb,代碼行數:21,代碼來源:LuceneSearchQuery.java

示例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 );
    }
}
 
開發者ID:neo4j-contrib,項目名稱:neo4j-lucene5-index,代碼行數:24,代碼來源:IndexType.java

示例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;
}
 
開發者ID:trackplus,項目名稱:Genji,代碼行數:32,代碼來源:ExternalListSearcher.java


注:本文中的org.apache.lucene.queryparser.classic.QueryParser.parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。