本文整理汇总了Java中org.apache.lucene.queryParser.QueryParser类的典型用法代码示例。如果您正苦于以下问题:Java QueryParser类的具体用法?Java QueryParser怎么用?Java QueryParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
QueryParser类属于org.apache.lucene.queryParser包,在下文中一共展示了QueryParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLuceneQuery
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Gets the Lucene Query representation for the given XML Query starting at the Query element.
*
* @param queryElement A dom4j representation of the Query element
* @param queryParser QueryParser used to parse Lucene syntax based queries and tokenize the text
* @return The Lucene Query object representation of this XML Query
* @exception Exception If error
*/
public static Query getLuceneQuery(Element queryElement, QueryParser queryParser) throws Exception {
// Check if requested to exclude from results, which is an error if not within a boolean clause
String excludeOrRequire = queryElement.attributeValue("excludeOrRequire");
if (excludeOrRequire != null)
throw new Exception("Error parsing document: attribute excludeOrRequire may only be used when the query is enclosed in an encompassing <booleanQuery>. Error found at: " + queryElement.getUniquePath());
if (queryElement.getName().equals("booleanQuery"))
return makeBooleanQuery(queryElement, queryParser);
else if (queryElement.getName().equals("textQuery"))
return makeLuceneQuery(queryElement, queryParser);
else if (queryElement.getName().equals("luceneQuery"))
return makeLuceneQuery(queryElement, queryParser);
else
throw new Exception("Error parsing document: invalid element name '<" + queryElement.getName() + ">' at " + queryElement.getUniquePath());
}
示例2: formatSearchModeQuery
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
private final static Query formatSearchModeQuery(String q, String queryMode, RepositoryManager rm, ServletContext context)
throws org.apache.lucene.queryParser.ParseException {
prtln ("formatSearchModeQuery()");
if (queryMode.equals("id")) {
q = "id:" + SimpleLuceneIndex.encodeToTerm(q, false);
}
if (queryMode.equals("url")) {
q = "urlenc:" + SimpleLuceneIndex.encodeToTerm(q, false);
}
if (queryMode.equals("status_note")) {
// q = "dcsstatusEntryNote:" + SimpleLuceneIndex.encodeToTerm(q, false);
q = "dcsstatusEntryNote:" + SchemEditUtils.quoteWrap(q);
}
QueryParser qp = rm.getIndex().getQueryParser();
BooleanQuery simpleQuery = new BooleanQuery();
simpleQuery.add(qp.parse(q), BooleanClause.Occur.MUST);
return simpleQuery;
}
示例3: processArgument
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
@Override
protected boolean processArgument(String arg) throws CLIOException {
try {
Analyzer analyzer = PubMedIndexUtils.getGlobalAnalyzer();
QueryParser parser = new QueryParser(PubMedIndexUtils.LUCENE_VERSION, PubMedIndexField.ABSTRACT.fieldName, analyzer);
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
parser.setLowercaseExpandedTerms(false);
PubMedIndexUtils.log("parsing query: %s", arg);
Query q = parser.parse(arg);
PubMedIndexUtils.log("query: %s", q);
addClause(q);
return false;
}
catch (ParseException e) {
throw new CLIOException(e);
}
}
示例4: search
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
public Map<String, Integer> search(String word, String field, int maxSearch) {
if (indexSearcher == null) {
initialize(index);
}
Map<String, Integer> verbFreqs = new HashMap<>();
QueryParser queryParser = new QueryParser(Version.LUCENE_36, field, analyzer);
try {
Query query = queryParser.parse(word);
TopDocs topDocs = indexSearcher.search(query, maxSearch);
ScoreDoc[] doc = topDocs.scoreDocs;
for (int i = 0; i < maxSearch && i < doc.length; ++i) {
int documentId = doc[i].doc;
Document document = indexSearcher.doc(documentId);
String verb = document.get(VERB);
String frequency = document.get(FREQ);
verbFreqs.put(verb, Integer.parseInt(frequency));
}
} catch (ParseException | IOException e) {
log.warn("Error searching Lucene index.", e);
}
return verbFreqs;
}
示例5: select
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Executes a select query.
* @param query The Lucene query
* @return An array of record IDs; an empty array if an error occured
* @throws IOException
*/
public int[] select(String query, String sortPrefix) throws ParseException{
try {
WhitespaceAnalyzer sa= new WhitespaceAnalyzer();
BooleanQuery.setMaxClauseCount(20000);//zbog heap-a
QueryParser p = new QueryParser("KW", sa);
p.setDefaultOperator(QueryParser.Operator.AND); //default operator je AND a ne OR kao sto je inace inicijalno
Query q = p.parse(query);
return select(q, sortPrefix);
} catch (Exception ex) {
if (ex instanceof ParseException )
throw (ParseException)ex;
log.warn(ex);
return new int[0];
}
}
示例6: search
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
public static void search(String indexDir, String q)
throws IOException, ParseException {
Directory dir = FSDirectory.open(new File(indexDir));
IndexSearcher is = new IndexSearcher(dir);
QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
new StandardAnalyzer(Version.LUCENE_30));
Query query = parser.parse(q);
long start = System.currentTimeMillis();
TopDocs hits = is.search(query, 10);
long end = System.currentTimeMillis();
System.out.println(" document(s) (in " + (end - start) + " milliseconds) that matched query '" +
q + "':" + ("Found " + hits.totalHits));
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("fullpath"));
}
is.close();
}
示例7: buildFilter
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
@org.hibernate.search.annotations.Factory
public Filter buildFilter(){
QueryParser qp = new QueryParser(Version.LUCENE_CURRENT,field, analyzer);
Query q;
try {
if(allowLuceneSyntax)
q = qp.parse(value);
else
q = qp.parse(QueryParser.escape(value));
} catch (ParseException e) {
org.webdsl.logging.Logger.error("Error while parsing query in field filter: ");
org.webdsl.logging.Logger.error("EXCEPTION",e);
q = new TermQuery(new Term(field, value));
}
Filter filter = new QueryWrapperFilter(q);
filter = new CachingWrapperFilter( filter );
return filter;
}
示例8: getSubQueryFilters
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Execute the sub query facets and return the search results
* @throws ParseException
* @throws IOException
*/
private List<Filter> getSubQueryFilters( List<FacetFilter> filters,
IndexSearcher searcher) throws ParseException, IOException
{
List<Filter> luceneFilters = new LinkedList<Filter>();
for(FacetFilter filter : filters)
{
if(log.isDebugEnabled())
{
log.debug("adding filter for field " + filter.getField() + " and query " + filter.getQuery());
}
QueryParser subQueryParser = new QueryParser(Version.LUCENE_35, filter.getField(), analyzer);
subQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
String fixedQuery = SearchHelper.prepareFacetSearchString(filter.getQuery(), false);
fixedQuery = "\"" + fixedQuery + "\"";
Query subQuery = subQueryParser.parse(fixedQuery);
if(log.isDebugEnabled())
{
log.debug("sub query ing getSubQueryFilters is " + fixedQuery);
}
luceneFilters.add(new QueryWrapperFilter(subQuery));
}
return luceneFilters;
}
示例9: executeQuery
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Executes the query returning the number of hits.
*
* @param field - field to search on
* @param queryString - query string
* @param dir - lucene index to search
*
* @return - number of hits
*
* @throws CorruptIndexException
* @throws IOException
* @throws ParseException
*/
private int executeQuery(String field, String queryString, Directory dir)
throws CorruptIndexException, IOException, ParseException {
IndexReader reader = IndexReader.open(dir, true);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, field, new StandardAnalyzer(Version.LUCENE_35));
Query q1 = parser.parse(queryString);
TopDocs hits = searcher.search(q1, 1000);
int hitCount = hits.totalHits;
searcher.close();
reader.close();
return hitCount;
}
示例10: executeQuery
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Executes the query returning the number of hits.
*
* @param field - field to search on
* @param queryString - query string
* @param dir - lucene index to search
*
* @return - number of hits
*
* @throws CorruptIndexException
* @throws IOException
* @throws ParseException
*/
private int executeQuery(String field, String queryString, Directory dir)
throws CorruptIndexException, IOException, ParseException {
IndexReader reader = IndexReader.open(dir, true);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, field, new StandardWithACIIFoldingFilter());
Query q1 = parser.parse(queryString);
TopDocs hits = searcher.search(q1, 1000);
int hitCount = hits.totalHits;
searcher.close();
return hitCount;
}
示例11: executeQuery
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
private int executeQuery(String field, String queryString, Directory dir) throws
CorruptIndexException, IOException, ParseException
{
IndexReader reader = IndexReader.open(dir, true);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, field, new StandardWithACIIFoldingFilter() );
Query q1 = parser.parse(queryString);
TopDocs hits= searcher.search(q1, 1000);
int hitCount = hits.totalHits;
searcher.close();
return hitCount;
}
示例12: executeQuery
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Executes the query returning the number of hits.
*
* @param field - field to search on
* @param queryString - query string
* @param dir - lucene index to search
*
* @return - number of hits
*
* @throws CorruptIndexException
* @throws IOException
* @throws ParseException
*/
private int executeQuery(String field, String queryString, Directory dir)
throws CorruptIndexException, IOException, ParseException {
IndexReader reader = IndexReader.open(dir, true);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, field, new StandardWithACIIFoldingFilter());
Query q1 = parser.parse(queryString);
TopDocs hits = searcher.search(q1, 1000);
int hitCount = hits.totalHits;
searcher.close();
return hitCount;
}
示例13: search
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
public void search(IndexerQueryResultList response) {
LOG.debug("Search called, using search index type: " + indexType);
QueryParser parser = new QueryParser("contents", analyzer);
try {
Hits hits;
Query query = parser.parse(response.getQueryString());
if (response.getPagedListInfo().getColumnToSortBy() == null) {
hits = searcher.search(query);
} else {
Sort sort = new Sort(new SortField(response.getPagedListInfo().getColumnToSortBy()));
hits = searcher.search(query, sort);
}
// Convert hits to IndexerResponse objects...
convertHits(response, hits);
} catch (Exception e) {
e.printStackTrace();
}
}
示例14: escapeKeywords
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
protected String escapeKeywords(String keywords) {
keywords = keywords.trim();
if (keywords.isEmpty()) {
return keywords;
}
if (keywords.charAt(0) == '*') {
if (keywords.length() == 1) {
return "";
} else {
keywords = keywords.substring(1).trim();
}
}
keywords = QueryParser.escape(keywords);
return keywords;
}
示例15: build
import org.apache.lucene.queryParser.QueryParser; //导入依赖的package包/类
/**
* Crea el buscador a partir del índice pasado como argumento de entrada
* @param index índice con el que crear el buscador
*/
@Override
public void build(Index index) {
try{
//apertura del indice
Directory directory = new SimpleFSDirectory(new File(index.getPath()));
this.ireader = IndexReader.open(directory);
this.isearcher = new IndexSearcher(this.ireader);
//query parser, parsea igual que el creador de indices.
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
this.qParser = new QueryParser(Version.LUCENE_31, "content", analyzer);
}catch(Exception e){
Logger.getLogger(LuceneSearcher.class.getName()).log(Level.SEVERE, null, e);
}
}