本文整理汇总了Java中org.apache.lucene.queryParser.QueryParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java QueryParser.parse方法的具体用法?Java QueryParser.parse怎么用?Java QueryParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.queryParser.QueryParser
的用法示例。
在下文中一共展示了QueryParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
}
示例2: 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;
}
示例3: 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];
}
}
示例4: 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();
}
示例5: 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;
}
示例6: 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;
}
示例7: 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;
}
示例8: getSentencesContaining
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
/**
* getSentencesContaining returns a Vector of Integers containing the
* numbers of the sentences that contain the given words in the source
* language corpus.
*
* @param words_ The words to be found
* @return A Vector of the sentence numbers
*/
public Vector<String> getSentencesContaining(String words_) {
Vector<String> sentenceNums = new Vector<>();
words_ = requireAll(words_);
try {
Analyzer analyzer = new SimpleAnalyzer();
Query query = QueryParser.parse(words_, "contents", analyzer);
Hits hits = sourceSearcher.search(query);
// Add the numbers of all the hits to the Vector
for (int i = 0; i < hits.length(); i++) {
Document sentence = hits.doc(i);
sentenceNums.add(sentence.get("snum"));
//DEBUG System.out.println(sentence.get("snum") + ": " + sentence.get("contents"));
}
} catch (Exception e) {
System.out.println(" caught a " + e.getClass()
+ "\n with message: " + e.getMessage());
}
return sentenceNums;
}
示例9: search
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
public int search (String queryString, SearchField field) throws Exception {
// Query query = QueryParser.parse(queryString,"mailcontent", new StandardAnalyzer());
// hits = is.search(query);
// return hits.length();
//Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
Analyzer analyzer = new StopAnalyzer(Version.LUCENE_20, new File(ApplicationConstants.STOP_WORD_FILE));
QueryParser parser = new QueryParser(Version.LUCENE_20, field.Value(), analyzer);
Query query = parser.parse(queryString) ;
//Query query = QueryParser.parse(queryString,"body", new StandardAnalyzer());
//hits = is.search(query);
//return hits.length();
results = searcher.search(query,100);
return results.totalHits ;
}
示例10: query
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
Query query( String keyOrNull, Object value, QueryContext contextOrNull )
{
if ( value instanceof Query )
{
return (Query) value;
}
QueryParser parser = new QueryParser( Version.LUCENE_30, 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 );
}
}
示例11: createSearchInfoQuery
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
Query createSearchInfoQuery( SearchQuery searchQuery, Analyzer analyzer )
throws ParseException
{
TermQuery infoQuery =
new TermQuery(new Term(DOC_TYPE, DocType.INFO.toString()));
BooleanQuery query = new BooleanQuery();
query.add(infoQuery, Occur.MUST);
Query textQuery = createFreeTextQuery(searchQuery, analyzer);
if (textQuery != null) {
query.add(textQuery, Occur.MUST);
}
Query maturityQuery = createMaturityQuery(searchQuery);
if (maturityQuery != null) {
query.add(maturityQuery, Occur.MUST);
}
Query licenseQuery = createLicenseQuery(searchQuery);
if (licenseQuery != null) {
query.add(licenseQuery, Occur.MUST);
}
QueryParser parser = new QueryParser(Version.LUCENE_35, NAME, analyzer);
parser.setAllowLeadingWildcard(true);
Query docQuery = parser.parse(query.toString());
return docQuery;
}
示例12: getFullTextQueryForKeywordOnAllAnnotatedFields
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
public FullTextQuery getFullTextQueryForKeywordOnAllAnnotatedFields(String keyword) throws ParseException {
String[] indexedFields = findAllIndexedFields(entity);
final QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, indexedFields, analyzer);
parser.setAllowLeadingWildcard(true);
// Add wildcards when not already in search
if (!(StringUtils.containsAny(keyword, new char[] { '*', '?', ':' }) || keyword.startsWith("\"") && keyword.endsWith("\""))) {
keyword = "*" + keyword + "*";
}
Query luceneQuery = parser.parse(keyword);
FullTextQuery fullTextQuery = getFullTextQuery(luceneQuery);
return fullTextQuery;
}
示例13: run
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
public void run() throws java.io.IOException {
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser(field, analyzer);
while (true) {
String line = in.readLine();
if (line == null || line.length() == -1)
break;
line = line.trim();
if (line.length() == 0)
break;
Query query = null;
try {
query = parser.parse(line);
} catch (Exception e) {
e.printStackTrace();
}
searcher.search(query, null, 10);
doPagingSearch(query);
}
reader.close();
out.flush();
out.close();
synchronized (parent) {
parent.completed++;
if (parent.completed % 4 == 0) {
System.out.println(parent.completed + " query batches completed");
}
parent.notify();
}
}
示例14: search
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public Page<Article> search(String keyword, Pageable pageable) {
if (StringUtils.isEmpty(keyword)) {
return new Page<Article>();
}
if (pageable == null) {
pageable = new Pageable();
}
try {
String text = QueryParser.escape(keyword);
QueryParser titleParser = new QueryParser(Version.LUCENE_35, "title", new IKAnalyzer());
titleParser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query titleQuery = titleParser.parse(text);
FuzzyQuery titleFuzzyQuery = new FuzzyQuery(new Term("title", text), FUZZY_QUERY_MINIMUM_SIMILARITY);
Query contentQuery = new TermQuery(new Term("content", text));
Query isPublicationQuery = new TermQuery(new Term("isPublication", "true"));
BooleanQuery textQuery = new BooleanQuery();
BooleanQuery query = new BooleanQuery();
textQuery.add(titleQuery, Occur.SHOULD);
textQuery.add(titleFuzzyQuery, Occur.SHOULD);
textQuery.add(contentQuery, Occur.SHOULD);
query.add(isPublicationQuery, Occur.MUST);
query.add(textQuery, Occur.MUST);
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Article.class);
fullTextQuery.setSort(new Sort(new SortField[] { new SortField("isTop", SortField.STRING, true), new SortField(null, SortField.SCORE), new SortField("createDate", SortField.LONG, true) }));
fullTextQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
fullTextQuery.setMaxResults(pageable.getPageSize());
return new Page<Article>(fullTextQuery.getResultList(), fullTextQuery.getResultSize(), pageable);
} catch (ParseException e) {
e.printStackTrace();
}
return new Page<Article>();
}
示例15: getQueryFilter
import org.apache.lucene.queryParser.QueryParser; //导入方法依赖的package包/类
public static Filter getQueryFilter(String query){
try{
WhitespaceAnalyzer sa= new WhitespaceAnalyzer();
QueryParser p = new QueryParser("contents",sa);
Query q = p.parse(query);
Filter filter = new QueryWrapperFilter(q);
return filter;
}catch (Exception e){
return null;
}
}