本文整理汇总了Java中org.apache.lucene.queryParser.MultiFieldQueryParser类的典型用法代码示例。如果您正苦于以下问题:Java MultiFieldQueryParser类的具体用法?Java MultiFieldQueryParser怎么用?Java MultiFieldQueryParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MultiFieldQueryParser类属于org.apache.lucene.queryParser包,在下文中一共展示了MultiFieldQueryParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchIndex
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
/**
* 查询索引
*
* @param keywords
* @return
* @throws Exception
*/
public List<Document> searchIndex(Integer typeId, String keywords) throws Exception {
// 1.init searcher
Analyzer analyzer = new PaodingAnalyzer();
IndexReader reader = IndexReader.open(typeId == appConfig.getGameTypeId() ? appConfig.getGameIndexDir()
: appConfig.getSoftIndexDir());
BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(keywords, appConfig.getQueryFields(), flags, analyzer);
query = query.rewrite(reader);
// 2.search
List<Document> docs = new ArrayList<Document>();
Hits hits = (typeId == appConfig.getGameTypeId() ? gameSearcher.search(query, Sort.RELEVANCE) : softSearcher
.search(query, Sort.RELEVANCE));// searcher.search(query,
// Sort.RELEVANCE);
for (int i = 0; i < hits.length(); i++) {
docs.add(hits.doc(i));
}
// 3.return
reader.close();
return docs;
}
示例2: search
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public List<Document> search (String queryString, SearchScope luceneFields) throws Exception {
// using stop analyzer in search
Analyzer analyzer = new StopAnalyzer(Version.LUCENE_30, new File(ApplicationConstants.STOP_WORD_FILE));
String[] fields = getSupportedFileds(luceneFields);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30, fields, analyzer);
// scpaing special characters
queryString = QueryParser.escape(queryString);
Query query = parser.parse(queryString);
TopDocs topDocs = searcher.search(query, MAX_RESULT);
List<Document> documents = new ArrayList<Document>();
for(ScoreDoc scoreDocs: topDocs.scoreDocs) {
Document document = searcher.doc(scoreDocs.doc);
documents.add(document);
}
return documents;
}
示例3: skynetsearch
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public Hits skynetsearch(String query, String Field, String indexPath) {
String indexfield = Field + ":";
String querytext = indexfield + query.trim();
Hits result = null;
try {
String[] search_fields = {Field};
//String indexPath = StorageHandler.GetDocIndexPath();
IndexSearcher searcher = new IndexSearcher(indexPath);
KeywordAnalyzer analyzer = new KeywordAnalyzer();
Query lucenequery = MultiFieldQueryParser.parse(query,
search_fields, analyzer);
// QueryParser queryparse = new QueryParser(query,analyzer);
// Query lucenequery = queryparse.parse(querytext);
result = searcher.search(lucenequery);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
System.out.println(ex + "");
}
return result;
}
示例4: skynetsearchMulti
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public Hits skynetsearchMulti(String query, String[] Field, String indexPath) {
Hits result = null;
try {
IndexSearcher searcher = new IndexSearcher(indexPath);
KeywordAnalyzer analyzer = new KeywordAnalyzer();
MultiFieldQueryParser multiparser = new MultiFieldQueryParser(Field, analyzer);
multiparser.setDefaultOperator(QueryParser.Operator.OR);
Query lucenequery = multiparser.parse(query);
result = searcher.search(lucenequery);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
System.out.println(ex + "");
}
return result;
}
示例5: createFullTextQuery
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
private org.apache.lucene.search.Query createFullTextQuery(final String[] searchFields, final QueryFilter queryFilter,
final String searchString)
{
final MultiFieldQueryParser parser = new MultiFieldQueryParser(LUCENE_VERSION, searchFields, new ClassicAnalyzer(Version.LUCENE_31));
parser.setAllowLeadingWildcard(true);
org.apache.lucene.search.Query query = null;
try {
query = parser.parse(searchString);
} catch (final org.apache.lucene.queryParser.ParseException ex) {
final String errorMsg = "Lucene error message: "
+ ex.getMessage()
+ " (for "
+ this.getClass().getSimpleName()
+ ": "
+ searchString
+ ").";
if (queryFilter != null) {
queryFilter.setErrorMessage(errorMsg);
}
log.info(errorMsg);
return null;
}
return query;
}
示例6: getFullTextQueryForKeywordOnAllAnnotatedFields
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的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;
}
示例7: search
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public Hits search(String type, String qstr)
{
String queryStr = "(" + qstr + ") OR ("+TStransformer.STtransform(qstr)+")";
Hits hits = null;
try{
FSDirectory fsd = FSDirectory.getDirectory(QuickSearchConfig.getIndexDir() + "/" + type);
//RAMDirectory ramd = new RAMDirectory(QuickSearchConfig.getIndexDir() + "/" + type);
IndexSearcher indexSearcher = new IndexSearcher(fsd);
Query query = MultiFieldQueryParser.parse(queryStr, fields, flags, analyzer);
hits = indexSearcher.search(query);
}catch(Exception ex){
ex.printStackTrace();
}
return hits;
}
示例8: testDefaultOperator
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public void testDefaultOperator() throws Exception {
Query query = new MultiFieldQueryParser(Version.LUCENE_41,
new String[]{"title", "subject"},
new SimpleAnalyzer()).parse("development");
Directory dir = TestUtil.getBookIndexDirectory();
IndexSearcher searcher = new IndexSearcher(
dir,
true);
TopDocs hits = searcher.search(query, 10);
assertTrue(TestUtil.hitsIncludeTitle(
searcher,
hits,
"Ant in Action"));
assertTrue(TestUtil.hitsIncludeTitle( //A
searcher, //A
hits, //A
"Extreme Programming Explained")); //A
searcher.close();
dir.close();
}
示例9: testSpecifiedOperator
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public void testSpecifiedOperator() throws Exception {
Query query = MultiFieldQueryParser.parse(Version.LUCENE_41,
"lucene",
new String[]{"title", "subject"},
new BooleanClause.Occur[]{BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST},
new SimpleAnalyzer());
Directory dir = TestUtil.getBookIndexDirectory();
IndexSearcher searcher = new IndexSearcher(
dir,
true);
TopDocs hits = searcher.search(query, 10);
assertTrue(TestUtil.hitsIncludeTitle(
searcher,
hits,
"Lucene in Action, Second Edition"));
assertEquals("one and only one", 1, hits.scoreDocs.length);
searcher.close();
dir.close();
}
示例10: IndexSearcher
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
public IndexSearcher() {
try {
searcher = new org.apache.lucene.search.IndexSearcher(new ClasspathDirectory());
} catch (IOException e) {
e.printStackTrace();
}
analyzer = new StandardAnalyzer(Version.LUCENE_31);
parser = new MultiFieldQueryParser(Version.LUCENE_31, new String[]{"name","description"}, analyzer);
}
示例11: search
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
/**
* 检索
*
* @param keyword
*/
public static void search(String keyword) throws Exception {
IndexSearcher searcher = null;
try {
// 创建search
searcher = getIndexSearcher();
String[] fields = { "name", "author", "content" };
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_35, fields,
new StandardAnalyzer(Version.LUCENE_35));
queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = queryParser.parse(keyword);
// 查询
TopDocs topDocs = searcher.search(query, MAX);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
logger.info("查询到条数=" + scoreDocs.length);
for (ScoreDoc scoreDoc : scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
logger.info("doc信息:" + "docId=" + scoreDoc.doc + "id=" + doc.get("id") + "author="
+ doc.get("author") + "name=" + doc.get("name") + "content="
+ doc.get("content"));
}
} catch (Exception e) {
logger.error("查询失败:" + e.getMessage());
throw e;
}
}
示例12: multiFieldQuery
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
/**
* 多条搜索
* @param fields
* @param value
* @throws Exception
*/
public static void multiFieldQuery( String[] fields, String value,List<Document>docs) throws Exception {
try {
// 创建search
IndexSearcher searcher = getIndexSearcher();
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_35, fields,
new StandardAnalyzer(Version.LUCENE_35));
queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = queryParser.parse(value);
// 查询
TopDocs topDocs = searcher.search(query, 1000);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
logger.info("查询到条数=" + scoreDocs.length);
for (ScoreDoc scoreDoc : scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
// logger.info("doc信息:" + " docId=" + scoreDoc.doc + " id=" + doc.get("id") + " author="
// + doc.get("author") + " name=" + doc.get("name") + " content="
// + doc.get("content"));
docs.add(doc);
}
} catch (Exception e) {
logger.error("termQuery查询失败");
throw e;
}
}
示例13: loadModel
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
@Override
public void loadModel(String modelPath) throws IOException {
docsSearcher = new IndexSearcher((new File(modelPath, "docs"))
.getAbsolutePath());
String[] fields = { "doc_id", "content", "user_id", "tag" };
queryParser = new MultiFieldQueryParser(fields,
new WhitespaceAnalyzer());
}
示例14: search
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@DataProvider
public List<SearchResult> search(String searchText) throws Exception {
FullTextSession fullTextSession = Search.getFullTextSession(exampleDao
.getSession());
if (!indexRebuilded) {
indexRebuilded = true;
fullTextSession.createIndexer().startAndWait();
}
MultiFieldQueryParser parser = new MultiFieldQueryParser(
Version.LUCENE_31, new String[] { "label", "tags", "url",
"summary" }, new StandardAnalyzer(Version.LUCENE_31));
org.apache.lucene.search.Query luceneQuery = parser.parse(searchText);
FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery,
Example.class);
query.setFirstResult(0);
query.setMaxResults(100);
List<SearchResult> searchResults = new ArrayList<SearchResult>();
for (Example example : (List<Example>) query.list()) {
SearchResult searchResult = new SearchResult();
PropertyUtils.copyProperties(searchResult, example);
searchResults.add(searchResult);
}
return searchResults;
}
示例15: findByKeywords
import org.apache.lucene.queryParser.MultiFieldQueryParser; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public List<User> findByKeywords(String keywords, SearchType searchType) {
MultiFieldQueryParser parser = new MultiFieldQueryParser(
Version.LUCENE_31,
new String[] { "names", USERNAME_FIELD, EMAIL_FIELD },
new StandardAnalyzer(Version.LUCENE_31));
keywords = escapeKeywords(keywords);
if (searchType == SearchType.START) {
keywords += "*";
}
if (searchType == SearchType.FUZZY) {
keywords += "~";
}
if (searchType == SearchType.FULL) {
String[] parts = keywords.split(" ");
StringBuilder sb = new StringBuilder();
for (String part : parts) {
sb.append(part + "* ");
}
keywords = sb.toString();
}
Query query;
try {
query = parser.parse(keywords);
} catch (ParseException e) {
log.warn("Problem parsing query", e);
return Collections.emptyList();
}
FullTextQuery ftq = getFullTextEntityManager().createFullTextQuery(
query, User.class);
ftq.setMaxResults(commonPageSize);
return ftq.getResultList();
}