本文整理汇总了C#中Lucene.Net.QueryParsers.QueryParser.SetDefaultOperator方法的典型用法代码示例。如果您正苦于以下问题:C# QueryParser.SetDefaultOperator方法的具体用法?C# QueryParser.SetDefaultOperator怎么用?C# QueryParser.SetDefaultOperator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.QueryParsers.QueryParser
的用法示例。
在下文中一共展示了QueryParser.SetDefaultOperator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnExecuteSearch_Click
private void btnExecuteSearch_Click(object sender, EventArgs e)
{
Directory indexDirectory = FSDirectory.Open(new System.IO.DirectoryInfo(tempPath));
IndexSearcher searcher = new IndexSearcher(indexDirectory, true); // read-only=true
// TODO: QueryParser support for Hebrew terms (most concerning issue is with acronyms - mid-word quotes)
QueryParser qp = new QueryParser("content", analyzer);
qp.SetDefaultOperator(QueryParser.Operator.AND);
Query query = qp.Parse(txbSearchQuery.Text);
ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;
// Iterate through the results:
BindingList<SearchResult> l = new BindingList<SearchResult>();
for (int i = 0; i < hits.Length; i++)
{
Document hitDoc = searcher.Doc(hits[i].doc);
SearchResult sr = new SearchResult(hitDoc.GetField("title").StringValue(),
hitDoc.GetField("path").StringValue(), hits[i].score);
l.Add(sr);
}
searcher.Close();
indexDirectory.Close();
dgvResults.DataSource = l;
}
示例2: TestSingleCharTokenAnalyzer
public void TestSingleCharTokenAnalyzer()
{
Analyzer analyzer = new SingleCharTokenAnalyzer();
IndexSearcher src = CreateIndex("[email protected] 1234567890 abcdefgh", analyzer);
QueryParser p = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "field", analyzer);
p.SetDefaultOperator(QueryParser.Operator.AND);
p.SetEnablePositionIncrements(true);
TopDocs td = null;
td = src.Search(p.Parse("usergmail"), 10);
Assert.AreEqual(0, td.totalHits);
td = src.Search(p.Parse("gmailcom"), 10);
Assert.AreEqual(0, td.totalHits);
td = src.Search(p.Parse("678"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("someuser"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("omeuse"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("omeuse 6789"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("user gmail"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("\"user gmail\""), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("[email protected]"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("gmail.com"), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("\"gmail.com 1234\""), 10);
Assert.AreEqual(1, td.totalHits);
td = src.Search(p.Parse("\"gmail.com defg\""), 10);
Assert.AreEqual(0, td.totalHits);
td = src.Search(p.Parse("gmail.com defg"), 10);
Assert.AreEqual(1, td.totalHits);
}
示例3: Search
public TextSearchResult Search(string query, Tenant tenant)
{
var result = new TextSearchResult(module);
if (string.IsNullOrEmpty(query) || !Directory.Exists(path))
{
return result;
}
var dir = Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo(path));
var searcher = new IndexSearcher(dir, false);
try
{
var analyzer = new AnalyzersProvider().GetAnalyzer(tenant.GetCulture().TwoLetterISOLanguageName);
var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Text", analyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
if (TextIndexCfg.MaxQueryLength < query.Length)
{
query = query.Substring(0, TextIndexCfg.MaxQueryLength);
}
Query q = null;
try
{
q = parser.Parse(query);
}
catch (Lucene.Net.QueryParsers.ParseException) { }
if (q == null)
{
q = parser.Parse(QueryParser.Escape(query));
}
#pragma warning disable 618
var hits = searcher.Search(q);
#pragma warning restore 618
for (int i = 0; i < hits.Length(); i++)
{
var doc = hits.Doc(i);
result.AddIdentifier(doc.Get("Id"));
}
}
finally
{
searcher.Close();
dir.Close();
}
return result;
}
示例4: SearchService
public SearchService()
{
var tmpluceneIndexDir = LuceneStore.FSDirectory.Open(INDEX_DIR);
luceneIndexDir = new LuceneStore.RAMDirectory(tmpluceneIndexDir);
tmpluceneIndexDir.Close();
analyzer = new StandardAnalyzer(LuceneUtil.Version.LUCENE_29);
reader = IndexReader.Open(luceneIndexDir, true); // only searching, so read-only=true
searcher = new IndexSearcher(reader);
// parser = new QueryParser(LuceneUtil.Version.LUCENE_29, "analized_path", analyzer);
parser = new QueryParser(LuceneUtil.Version.LUCENE_29, "name", analyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
parser.SetAllowLeadingWildcard(true);
}
示例5: PanguQuery
/// <summary>
/// 提供搜索的方法
/// </summary>
/// <param name="keyword">搜索关键字</param>
/// <param name="fileType">文档类型</param>
public static TopDocs PanguQuery(string keyword, string fileType)
{
ParallelMultiSearcher multiSearch = IndexManager.GenerateMultiSearcher(fileType);
if (!multiSearch.GetSearchables().Any())
{
return null;
}
#region 生成Query语句
var field = new string[2];
field[0] = "fileName";
field[1] = "fileContent";
var boolQuery = new BooleanQuery();
if (!string.IsNullOrEmpty(keyword))
{
var keywordQuery = new BooleanQuery();
string queryKeyword = GetKeyWordsSplitBySpace(keyword, new PanGuTokenizer());//对关键字进行分词处理
#region 查询fileName
var fileNameParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, field[0], IndexManager.Analyzer);
fileNameParser.SetDefaultOperator(QueryParser.Operator.AND);
var fileNameQuery = fileNameParser.Parse(queryKeyword + "~"); // ~ 提供模糊查询
keywordQuery.Add(fileNameQuery, BooleanClause.Occur.SHOULD);
#endregion
#region 查询fileContent
var fileContentParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, field[1], IndexManager.Analyzer);
fileContentParser.SetDefaultOperator(QueryParser.Operator.AND);
var fileContentQuery = fileContentParser.Parse(queryKeyword + "~");
keywordQuery.Add(fileContentQuery, BooleanClause.Occur.SHOULD);
#endregion
boolQuery.Add(keywordQuery, BooleanClause.Occur.MUST);
}
#endregion
return multiSearch.Search(boolQuery, null, 1000);
}
示例6: SearchButton_Click
protected void SearchButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(SearchTextBox.Text))
{
Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.RAMDirectory(luceneDBPath);
String srch = SearchTextBox.Text;
Lucene.Net.Search.IndexSearcher idx = new Lucene.Net.Search.IndexSearcher(ramDir);
Lucene.Net.QueryParsers.QueryParser qp = new Lucene.Net.QueryParsers.QueryParser("_searchtxt", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
qp.SetDefaultOperator(Lucene.Net.QueryParsers.QueryParser.Operator.AND);
Lucene.Net.Search.BooleanQuery.SetMaxClauseCount(100);
Lucene.Net.Search.Hits hits = idx.Search(qp.Parse(srch));
List<int> aIds = new List<int>();
for (int i = 0; i < hits.Length(); i++)
{
Lucene.Net.Documents.Document doc = hits.Doc(i);
int aid = 0;
if (int.TryParse(doc.Get("id"), out aid))
{
aIds.Add(aid);
}
}
using (DataClassesDataContext dtx = new DataClassesDataContext())
{
var arts = from a in dtx.Articles
where aIds.Contains(a.Id)
select a;
DataList1.DataSource = arts;
DataList1.DataBind();
}
idx.Close();
GC.Collect();
}
}
示例7: Translate
public static Lucene.Net.Search.Query Translate(Query query)
{
BooleanQuery bq = new BooleanQuery();
if (query.Text != null && query.Text.Trim().Length > 0)
{
// TODO: Clean up default field using schema, if available
QueryParser parser = new QueryParser("Text", new StandardAnalyzer());
parser.SetDefaultOperator(QueryParser.Operator.AND);
bq.Add(
parser.Parse(query.Text),
LBooleanClause.Occur.MUST
);
}
foreach (QueryClause term in query.Children)
{
ProcessClause(bq, term);
}
return bq;
}
示例8: GetParser
public virtual QueryParser GetParser(Analyzer a)
{
if (a == null)
a = new SimpleAnalyzer();
QueryParser qp = new QueryParser("field", a);
qp.SetDefaultOperator(QueryParser.OR_OPERATOR);
return qp;
}
示例9: SearchHistoryByUserPattern
public List<TextSearched> SearchHistoryByUserPattern(string pattern)
{
IndexSearcher searcher = new IndexSearcher(m_HistoryPath);
List<TextSearched> list = null;
try
{
list = new List<TextSearched>();
QueryParser parserUser = new QueryParser(Constants.VisitUser, new WhitespaceAnalyzer());
parserUser.SetDefaultOperator(QueryParser.AND_OPERATOR);
QueryParser parserPattern = new QueryParser(Constants.SearchedText, new WhitespaceAnalyzer());
parserPattern.SetDefaultOperator(QueryParser.AND_OPERATOR);
Query queryUser = parserUser.Parse(System.Threading.Thread.CurrentPrincipal.Identity.Name);
Query queryPattern = parserPattern.Parse(pattern);
BooleanQuery query = new BooleanQuery();
query.Add(queryUser, BooleanClause.Occur.MUST);
query.Add(queryPattern, BooleanClause.Occur.MUST);
Hits hits = searcher.Search(query);
for (int i = 0; i < hits.Length(); i++)
{
TextSearched st = new TextSearched();
Document doc = hits.Doc(i);
st.DateTicks = doc.Get(Constants.DateTicks);
st.VisitUser = doc.Get(Constants.VisitUser);
st.SearchedText = doc.Get(Constants.SearchedText);
list.Add(st);
}
list.Sort();
return list;
}
finally
{
searcher.Close();
}
}
示例10: Search
public List<IndexedFile> Search(string pattern)
{
IndexSearcher searcher = new IndexSearcher(m_SearchPath);
List<IndexedFile> list = null;
try
{
list = new List<IndexedFile>();
QueryParser parser = new QueryParser(Constants.Content, new StandardAnalyzer());
parser.SetDefaultOperator(QueryParser.AND_OPERATOR);
string tokenizedPattern = Tokenizer.Tokenize(pattern, false);
if (pattern != tokenizedPattern)
pattern = pattern + " OR \"" + tokenizedPattern + "\"";
Query query = parser.Parse(pattern);
Hits hits = searcher.Search(query);
for (int i = 0; i < hits.Length(); i++)
{
IndexedFile fi = new IndexedFile();
Document doc = hits.Doc(i);
fi.FileFullName = doc.Get(Constants.Full);
fi.FileName = doc.Get(Constants.File);
fi.FileFolderName = doc.Get(Constants.Folder);
fi.FileExtension = doc.Get(Constants.Extension);
fi.FileSizeinBytes = long.Parse(doc.Get(Constants.Size));
list.Add(fi);
}
return list;
}
finally
{
searcher.Close();
}
}
示例11: Index
public ActionResult Index(Models.Search model)
{
if (!string.IsNullOrEmpty(model.Phrase))
{
//// Use this style query to search for documents - it returns all results
//// including those matching only some of the terms
//Query query = new QueryParser(
// Lucene.Net.Util.Version.LUCENE_29,
// "title",
// new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
// ).Parse(model.Phrase);
// Use this style query for products - all of the terms entered must match.
QueryParser parser = new QueryParser(
Lucene.Net.Util.Version.LUCENE_29,
"title",
new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
);
//ISynonymEngine engine = new SpecialSynonymEngine();
//// Same as above, but modified to handle synonyms
//QueryParser parser = new QueryParser(
// Lucene.Net.Util.Version.LUCENE_29,
// "title",
// new SynonymAnalyzer(Lucene.Net.Util.Version.LUCENE_29, engine)
// );
// This ensures all words must match in the phrase
parser.SetDefaultOperator(QueryParser.Operator.AND);
// This ensures similar words will match
//parser.SetPhraseSlop(3);
// Sets the current culture
parser.SetLocale(System.Threading.Thread.CurrentThread.CurrentCulture);
Query query = parser.Parse(model.Phrase);
// Use query.Combine to merge this query with individual facets ??
//// Get the terms from the query
//string[] terms = model.Phrase.Split(" ".ToCharArray());
//PhraseQuery query = new PhraseQuery();
//query.SetSlop(4);
//foreach (var term in terms)
//{
// query.Add(new Term("title", term));
//}
BrowseResult result = this.PerformSearch(query, this.IndexDirectory, null);
//// Build results for display
//int totalHits = result.NumHits;
//BrowseHit[] hits = result.Hits;
//model.Hits = result.Hits;
//model.FacetMap = result.FacetMap;
////model.TotalHitCount = totalHits;
PopulateModelResult(model, result);
}
return View(model);
}
示例12: MultiFieldQuery
/// <summary>
/// Creates the basis query that all QueryTypes use
/// </summary>
/// <param name="queryTerm"></param>
/// <param name="fields"></param>
/// <param name="weighting"></param>
/// <param name="analyzer"></param>
/// <returns></returns>
private Query MultiFieldQuery(string queryTerm, IList<string> fields, IList<float> weighting, Lucene.Net.Analysis.Analyzer analyzer)
{
BooleanQuery combinedQuery = new BooleanQuery();
for (int i = 0; i < fields.Count; i++)
{
QueryParser qp = new QueryParser(fields[i], analyzer);
qp.SetDefaultOperator(QueryParser.Operator.AND);
Query fieldQuery;
try
{
fieldQuery = qp.Parse(queryTerm);
}
catch(ParseException)
{
fieldQuery = qp.Parse(QueryParser.Escape(queryTerm));
}
fieldQuery.SetBoost(weighting[i]);
combinedQuery.Add(fieldQuery, BooleanClause.Occur.SHOULD);
}
return combinedQuery;
}
示例13: RestrictSearch
/// <summary>
/// Add a restriction to an existing BooleanQuery
/// </summary>
/// <param name="boolQuery"></param>
/// <param name="field"></param>
/// <param name="term"></param>
/// <param name="analyzer"></param>
/// <returns></returns>
private Query RestrictSearch(BooleanQuery boolQuery, string field, string term, Lucene.Net.Analysis.Analyzer analyzer)
{
QueryParser qp = new QueryParser(field, analyzer);
qp.SetDefaultOperator(QueryParser.Operator.AND);
Query q = qp.Parse(term);
BooleanQuery restrictedQuery = new BooleanQuery();
restrictedQuery.Add(boolQuery, BooleanClause.Occur.MUST);
restrictedQuery.Add(q, BooleanClause.Occur.MUST);
return restrictedQuery;
}
示例14: GetQueryDOA
public virtual Query GetQueryDOA(System.String query, Analyzer a)
{
if (a == null)
a = new SimpleAnalyzer();
QueryParser qp = new QueryParser("field", a);
qp.SetDefaultOperator(QueryParser.AND_OPERATOR);
return qp.Parse(query);
}
示例15: Search
public Dictionary<MsDocEntryPoint, Dictionary<MsDocEntryPointMethod, string>> Search(string query, List<MsDocEntryPoint> points)
{
var parser = new QueryParser("summary", new StandardAnalyzer());
parser.SetDefaultOperator(QueryParser.Operator.AND);
Query q = null;
try
{
q = parser.Parse(MakeQuery(query));
}
catch (Lucene.Net.QueryParsers.ParseException)
{
q = parser.Parse(QueryParser.Escape(query));
}
var result = new Dictionary<MsDocEntryPoint, ICollection<MsDocEntryPointMethod>>();
var hits = searcher.Search(q, Sort.RELEVANCE);
for (int i = 0; i < hits.Length(); i++)
{
var doc = hits.Doc(i);
var point = doc.Get("point");
var method = doc.Get("path");
if (!string.IsNullOrEmpty(method))
{
var entryPoint = points.Single(x => x.Name == point);
var pointMethod = entryPoint.Methods.Single(x => x.ToString() == method);
ICollection<MsDocEntryPointMethod> foundPoints;
if (result.TryGetValue(entryPoint, out foundPoints))
{
foundPoints.Add(pointMethod);
}
else
{
foundPoints = new List<MsDocEntryPointMethod> { pointMethod };
result.Add(entryPoint, foundPoints);
}
}
}
return result.ToDictionary(x => x.Key, y => y.Value.ToDictionary(key => key, value => string.Empty));
}