本文整理汇总了Java中org.apache.lucene.queryparser.classic.MultiFieldQueryParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java MultiFieldQueryParser.parse方法的具体用法?Java MultiFieldQueryParser.parse怎么用?Java MultiFieldQueryParser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.queryparser.classic.MultiFieldQueryParser
的用法示例。
在下文中一共展示了MultiFieldQueryParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: search
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public synchronized ObjectMatrix2D search(String searchString) {
try {
MultiFieldQueryParser p = new MultiFieldQueryParser(Version.LUCENE_47, new String[] { VALUESTRING },
getAnalyzer());
Query query = p.parse(searchString);
TopDocs docs = getIndexSearcher().search(query, 100);
ObjectMatrix2D result = ObjectMatrix2D.Factory.zeros(docs.totalHits, 3);
for (int row = 0; row < docs.totalHits; row++) {
ScoreDoc match = docs.scoreDocs[row];
Document doc = getIndexSearcher().doc(match.doc);
result.setAsFloat(match.score, row, 0);
result.setAsObject(SerializationUtil.deserialize(doc.getBinaryValue(KEYDATA).bytes), row, 1);
result.setAsObject(SerializationUtil.deserialize(doc.getBinaryValue(VALUEDATA).bytes), row, 2);
}
return result;
} catch (Exception e) {
throw new RuntimeException("could not search documents: " + searchString, e);
}
}
示例2: simConsqQuery
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
private Query simConsqQuery(String cons, String consVal, double sim) {
String[] field = new String[] { cons };
MultiFieldQueryParser parser = new MultiFieldQueryParser(
org.apache.lucene.util.Version.LUCENE_48, field,
new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_48));
if (consVal == null || "".equals(consVal.trim())) {
consVal = "\" \"";
}
Query q = null;
try {
q = parser.parse(QueryParser.escape(consVal + "~ "
+ (int) ((1.0d - sim) * (consVal.length()))));
} catch (ParseException e) {
logger.log(ProdLevel.PROD, "EXCEPTION : " + e.getMessage());
}
// logger.log(ProdLevel.PROD, "Executing lucene query : " + q);
return q;
}
示例3: buildApproxMatchingQuery
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public Query buildApproxMatchingQuery(String colsStr, List<String> cols) {
StringBuilder sb = new StringBuilder();
sb.append(colsStr);
sb.append("~");
MultiFieldQueryParser parser = new MultiFieldQueryParser(
org.apache.lucene.util.Version.LUCENE_48,
cols.toArray(new String[cols.size()]), new StandardAnalyzer(
org.apache.lucene.util.Version.LUCENE_48));
Query q = null;
try {
q = parser.parse(QueryParser.escape(sb.toString()));
// logger.log(DebugLevel.DEBUG, "Approx data matching query : " +
// q);
} catch (ParseException e) {
logger.log(ProdLevel.PROD, "EXCEPTION : " + e.getMessage());
}
return q;
}
示例4: qs
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
/**
* Tries to parse a query string in order to check if it is valid.
* @param query a Lucene query string
* @return the query if valid, or '*' if invalid
*/
static Query qs(String query, Collection<String> fields) {
if (fields == null || fields.isEmpty()) {
fields = Collections.singletonList(Config._NAME);
}
if (StringUtils.isBlank(query)) {
query = "*";
}
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields.toArray(new String[0]), new StandardAnalyzer());
parser.setAllowLeadingWildcard(false);
//parser.setLowercaseExpandedTerms(false); // DEPRECATED in Lucene 7.x
query = query.trim();
if (query.length() > 1 && query.startsWith("*")) {
query = query.substring(1);
}
if (!StringUtils.isBlank(query) && !"*".equals(query)) {
try {
Query q = parser.parse(query);
return q;
} catch (Exception ex) {
logger.warn("Failed to parse query string '{}'.", query);
}
}
return new MatchAllDocsQuery();
}
示例5: searchPage
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String searchPage(
Model model,
@RequestParam(value = "keyword", required = false) String keyword) {
String[] fieldList = {"uid", "rpcid", "traceid", "rt_type", "url", "param", "ip", "httpcod", "project"};
Map<String, Float> boosts = new HashMap<>();
boosts.put("uid", 1.0f);
boosts.put("ip", 1.0f);
boosts.put("rpcid", 1.0f);
boosts.put("traceid", 1.0f);
boosts.put("rt_type", 1.0f);
boosts.put("url", 1.0f);
boosts.put("urlraw", 1.0f);
boosts.put("param", 1.0f);
boosts.put("httpcode", 1.0f);
boosts.put("project", 1.0f);
MultiFieldQueryParser mulFieldQueryParser = new MultiFieldQueryParser(fieldList, new StandardAnalyzer(), boosts);
Query query;
try {
query = mulFieldQueryParser.parse(keyword);
} catch (Exception e) {
model.addAttribute("msg", "query parser error");
return "search";
}
Sort sort = new Sort(new SortField("time", SortField.Type.DOUBLE, true));
ResponseJson result = indexHelper.searchByQuery(DateTimeHelper.getCurrentTime(), query, 0, 1000, sort);
model.addAttribute("resultlist", result.getResult());
model.addAttribute("keyword", keyword);
return "search";
}
示例6: createLuceneQuery
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
private org.apache.lucene.search.Query createLuceneQuery(String query) {
try {
return MultiFieldQueryParser.parse(
new String[] {query},
new String[] {field},
analyzer);
} catch (ParseException e) {
throw new QueryException("Cannot parse query", e);
}
}
示例7: runQuery
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
/**
* Performs scoring on the query provided.
* @param qno
* @param queryTerms
* @return
*/
public ScoreDoc[] runQuery(String qno, String queryTerms){
ScoreDoc[] hits = null;
String[] fields = new String[fl.fields.size()];
Map<String, Float> boosts = new HashMap<>();
int i = 0;
for (Field f : fl.fields) {
fields[i] = f.fieldName;
boosts.put(f.fieldName, f.fieldBoost);
i++;
}
try {
MultiFieldQueryParser mfq = new MultiFieldQueryParser(fields, analyzer, boosts);
Query q = mfq.parse(queryTerms);
System.out.println(qno+ ": " + q.toString());
try {
TopDocs results = searcher.search(q, p.maxResults);
hits = results.scoreDocs;
} catch (IOException ioe) {
System.out.println(" caught a " + ioe.getClass() +
"\n with message: " + ioe.getMessage());
}
} catch (ParseException pe){
System.out.println("Can't parse query");
}
return hits;
}
示例8: test
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
@Test
public void test() throws IOException, ParseException {
Directory directory = FSDirectory.open(Paths.get("d:\\data1\\lucene"));
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
Document document = new Document();
document.add(new StringField("title" , "Bill Cosby loses latest legal bid to block sexual abuse lawsuit", Field.Store.YES));
document.add(new TextField("content", "The accuser's attorney, Gloria Allred, said the decision cleared the way for the litigation brought by Judy Huth, now in her 50s, to proceed, and that she intended to take Cosby's sworn deposition within the next 30 days." , Field.Store.YES));
indexWriter.updateDocument(new Term("title" , "Bill Cosby loses latest legal bid to block sexual abuse lawsuit") , document);
document = new Document();
document.add(new StringField("title", "BillMarines braved gunfire to save comrades in Chattanooga ", Field.Store.YES));
document.add(new TextField("content", "The FBI is investigating whether Abdulazeez had any ties to terrorists. The FBI seized personal notes from his house that hinted at extremist views. ", Field.Store.YES));
indexWriter.updateDocument(new Term("title" , "BillMarines braved gunfire to save comrades in Chattanooga ") , document);
indexWriter.close();
DirectoryReader directoryReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
String fields[] = {"title" , "content"};
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = multiFieldQueryParser.parse("FBI");
TopScoreDocCollector collector = TopScoreDocCollector.create(10);
indexSearcher.search(query , collector);
TopDocs topDocs = collector.topDocs();
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc d : scoreDocs){
Document doc = indexSearcher.doc(d.doc);
System.out.println(doc.get("title"));
System.out.println(doc.get("content"));
System.out.println("=====================");
}
}
示例9: parse
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public Query parse(String queryString) throws ParseException {
String[] fields = new String[]{
DocumentIndex.FIELD__META_ID,
DocumentIndex.FIELD__META_HEADLINE,
DocumentIndex.FIELD__META_TEXT,
DocumentIndex.FIELD__TEXT,
DocumentIndex.FIELD__KEYWORD,
DocumentIndex.FIELD__ALIAS
};
BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length];
Arrays.fill(flags, BooleanClause.Occur.SHOULD);
return MultiFieldQueryParser.parse(Version.LUCENE_43, queryString, fields, flags, new AnalyzerImpl());
}
示例10: constructQuery
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
private Query constructQuery(final Set<AttributeType> queryAttributes, final String queryString) throws ParseException {
final Set<String> queryAttributeNames = queryAttributes.stream().map(attributeType -> attributeType.getName()).collect(Collectors.toSet());
final MultiFieldQueryParser parser = new MultiFieldQueryParser(
queryAttributeNames.toArray(new String[queryAttributeNames.size()]),
new FullTextAnalyzer(FullTextAnalyzer.Operation.QUERY));
parser.setAnalyzer(FullTextIndex.QUERY_ANALYZER);
parser.setDefaultOperator(QueryParser.Operator.AND);
return parser.parse(String.format("%s*", normalise(queryString)));
}
示例11: checkIfRecipeExists
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
private boolean checkIfRecipeExists(String recipeName) throws IOException {
try (IndexReader reader = DirectoryReader.open(index)) {
IndexSearcher searcher = new IndexSearcher(reader);
MultiFieldQueryParser.parse(new String[]{recipeName}, new String[]{"title"}, analyzer);
Query query = new QueryParser("title", analyzer).parse(recipeName);
TopDocs topdocs = searcher.search(query, 1);
if (topdocs.totalHits > 0) {
return true;
}
} catch (CorruptIndexException | ParseException e) {
throw new IOException(e);
}
return false;
}
示例12: testMultiFieldQuery1
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public void testMultiFieldQuery1() throws Exception {
String queryString = "com.twitter.status.text.url:h* AND com.twitter.status.id:(1604330852 OR 1604330857)";
String[] queryFieldNames = new String[] { "com.twitter.status.text.url", "com.twitter.status.id" };
BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST };
Query query = MultiFieldQueryParser.parse(Version.LUCENE_41, queryString, queryFieldNames, flags,
new KeywordAnalyzer());
queryIndex(query, "com.twitter.status.text.url");
}
示例13: testMultiFieldQuery2
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public void testMultiFieldQuery2() throws Exception {
String queryString = "com.twitter.status.text.url:h* AND com.twitter.status.date.creation:[20090424150351000 TO 20090424150354000]";
String[] queryFieldNames = new String[] { "com.twitter.status.text.url", "com.twitter.status.id" };
BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST };
Query query = MultiFieldQueryParser.parse(Version.LUCENE_41, queryString, queryFieldNames, flags,
new KeywordAnalyzer());
queryIndex(query, "com.twitter.status.text.url");
}
示例14: testMultiFieldQuery3
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public void testMultiFieldQuery3() throws Exception {
Date now = Calendar.getInstance().getTime();
String nowString = DateTools.timeToString(now.getTime(), DateTools.Resolution.MILLISECOND);
String queryString = "com.twitter.status.text.url:h* AND com.twitter.status.date.creation:[20090424150351000 TO "
+ nowString + "]";
String[] queryFieldNames = new String[] { "com.twitter.status.text.url", "com.twitter.status.id" };
BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST };
Query query = MultiFieldQueryParser.parse(Version.LUCENE_41, queryString, queryFieldNames, flags,
new KeywordAnalyzer());
queryIndex(query, "com.twitter.status.text.url");
}
示例15: testMultiFieldQuery4
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; //导入方法依赖的package包/类
public void testMultiFieldQuery4() throws Exception {
String[] fieldQueries = new String[] { "h*", "1604330852 OR 1604330857 OR 1604330866" };
String[] queryFieldNames = new String[] { "com.twitter.status.text.url", "com.twitter.status.id" };
BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST };
Query query = MultiFieldQueryParser.parse(Version.LUCENE_41, fieldQueries, queryFieldNames, flags,
new KeywordAnalyzer());
queryIndex(query, "com.twitter.status.text.url");
}