本文整理汇总了C#中Lucene.Net.QueryParsers.MultiFieldQueryParser类的典型用法代码示例。如果您正苦于以下问题:C# MultiFieldQueryParser类的具体用法?C# MultiFieldQueryParser怎么用?C# MultiFieldQueryParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MultiFieldQueryParser类属于Lucene.Net.QueryParsers命名空间,在下文中一共展示了MultiFieldQueryParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Search
public ActionResult Search(string query)
{
ViewData["Message"] = "query : " + query;
var searcher = new IndexSearcher(
new Lucene.Net.Store.SimpleFSDirectory(new DirectoryInfo(Configuration.IndexDirectory)),
readOnly: true);
var fieldsToSearchIn = new[] {Configuration.Fields.Name, Configuration.Fields.Description};
var queryanalizer = new MultiFieldQueryParser(Version.LUCENE_CURRENT,
fieldsToSearchIn,
new BrazilianAnalyzer());
var numberOfResults = 10;
var top10Results = searcher.Search(queryanalizer.Parse(query), numberOfResults);
var docs = new List<DocumentViewModel>();
foreach (var scoreDoc in top10Results.scoreDocs)
{
var document = searcher.Doc(scoreDoc.doc);
var name = document.GetField(Configuration.Fields.Name).StringValue();
var description = document.GetField(Configuration.Fields.Description).StringValue();
var link = document.GetField(Configuration.Fields.Link).StringValue();
docs.Add(new DocumentViewModel(name, description, link));
}
return View(new SearchViewModel(docs));
}
示例2: Get
public Query Get(ProductSearchQuery searchQuery)
{
var booleanQuery = new BooleanQuery { ProductSearchPublishedDefinition.PublishedOnly };
if (!searchQuery.Options.Any() && !searchQuery.Specifications.Any() && Math.Abs(searchQuery.PriceFrom - 0) < 0.01 && !searchQuery.PriceTo.HasValue &&
!searchQuery.CategoryId.HasValue && string.IsNullOrWhiteSpace(searchQuery.SearchTerm)
&& !searchQuery.BrandId.HasValue)
{
return booleanQuery;
}
if (searchQuery.Options.Any())
booleanQuery.Add(GetOptionsQuery(searchQuery.Options), Occur.MUST);
if (searchQuery.Specifications.Any())
booleanQuery.Add(GetSpecificationsQuery(searchQuery.Specifications), Occur.MUST);
if (searchQuery.CategoryId.HasValue)
booleanQuery.Add(GetCategoriesQuery(searchQuery.CategoryId.Value), Occur.MUST);
if (searchQuery.PriceFrom > 0 || searchQuery.PriceTo.HasValue)
booleanQuery.Add(GetPriceRangeQuery(searchQuery), Occur.MUST);
if (!String.IsNullOrWhiteSpace(searchQuery.SearchTerm))
{
IndexDefinition indexDefinition = IndexingHelper.Get<ProductSearchIndex>();
Analyzer analyser = indexDefinition.GetAnalyser();
var parser = new MultiFieldQueryParser(Version.LUCENE_30, indexDefinition.SearchableFieldNames, analyser);
Query query = searchQuery.SearchTerm.SafeGetSearchQuery(parser, analyser);
booleanQuery.Add(query, Occur.MUST);
}
if (searchQuery.BrandId.HasValue)
booleanQuery.Add(GetBrandQuery(searchQuery.BrandId.Value), Occur.MUST);
return booleanQuery;
}
示例3: searchLucene
public Data searchLucene(Data data)
{
Search_gl search = new Search_gl();
List<string> item = new List<string>();
Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndex"));
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//QueryParser queryParser = new QueryParser(Version.LUCENE_29, "summary", analyzer); //search for single field
MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] {"name", "summary"}, analyzer); //search for multifield
Query query = parser.Parse((data.getString("search")) + "*"); //cant search blank text with wildcard as first character
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
searcher.Search(query, collector);
ScoreDoc[] hits = collector.TopDocs().ScoreDocs;
int count = hits.Length;
for (int i = 0; i < count; i++)
{
int docId = hits[i].doc;
float score = hits[i].score;
Document doc = searcher.Doc(docId);
string id = doc.Get("id");
item.Add(id);
}
Data list = search.search(data, item.ToArray());
reader.Close();
searcher.Close();
return list;
}
示例4: Query
public static SearchResults Query(HttpContext ctx, string query, int page = 1) {
if (string.IsNullOrWhiteSpace(query)) {
return new SearchResults {
Documents = new SearchResult[0],
TotalCount = 0
};
}
var indexPath = ctx.Server.MapPath("~/App_Data/Index");
var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(indexPath));
using (var searchService = new SearchService(indexSearcher)) {
var parser = new MultiFieldQueryParser(
Lucene.Net.Util.Version.LUCENE_29,
new[] { "Text" },
new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
Query multiQuery = GetSafeQuery(parser, query);
var result = searchService.SearchIndex(multiQuery);
return new SearchResults {
Documents = result.Results
.Skip(PageSize*(page - 1))
.Take(PageSize)
.Select(d => new SearchResult {
Url = d.Get("Url"),
Title = d.Get("Title"),
Summary = d.Get("Summary")
}),
TotalCount = result.Results.Count()
};
}
}
示例5: SearchAds
public List<Advert> SearchAds(string query)
{
if (_searcher == null) return null;
ICollection fields = _searcher.Reader.GetFieldNames(IndexReader.FieldOption.ALL);
List<string> fldList = new List<string>();
foreach (DictionaryEntry f in fields)
{
fldList.Add(f.Key.ToString());
}
List<Advert> adverts = new List<Advert>();
MultiFieldQueryParser parser = new MultiFieldQueryParser(fldList.ToArray(), _analyzer);
Query q = parser.Parse(query);
Hits hits = _searcher.Search(q);
PropertyDescriptors desc = new PropertyDescriptors();
desc.LoadData(System.Windows.Forms.Application.StartupPath + "\\PropertyDescriptors.xml");
for (int i = 0; i < hits.Length(); i++)
{
Advert ad = new Advert();
Document doc = hits.Doc(i);
foreach (Field f in doc.Fields())
{
string temp = desc.GetDisplayableFormat(f.Name(), f.StringValue());
ad[f.Name()] = temp;
}
adverts.Add(ad);
}
return adverts;
}
示例6: Search
public IList<MedicalConsultory> Search(string searchText)
{
if (string.IsNullOrWhiteSpace(searchText))
return new List<MedicalConsultory>();
using (var searcher = new IndexSearcher(_indexDirectoryPath, true))
{
var analyzer = new StandardAnalyzer(_version);
// Multi field search
var query = new BooleanQuery();
var parser = new MultiFieldQueryParser(_version, new[] { "SpecialtyName", "City" }, analyzer);
searchText.RemoveIrrelevantTerms()
.Split(' ')
.ToList()
.ForEach(word =>
{
query.Add(parser.Parse(word), Occur.SHOULD);
});
var hits = searcher.Search(query, null, searcher.MaxDoc, Sort.RELEVANCE).ScoreDocs;
// Simple field search
//var parser = new QueryParser(Version.LUCENE_30, "SpecialtyName", analyzer);
//var query = parser.Parse(searchText.Trim());
//var hits = searcher.Search(query, searcher.MaxDoc).ScoreDocs;
var results = hits.Select(hit => MapMedicalConsultory(hit, searcher)).ToList();
analyzer.Close();
searcher.Dispose();
return results;
}
}
示例7: PerformExplain
/// <summary>
/// Performs the explanation.
/// </summary>
/// <param name="luceneVersion">The lucene version.</param>
/// <param name="fsDirectory">The fs directory.</param>
/// <param name="searchQuery">The search query.</param>
/// <param name="resultId">The result identifier.</param>
/// <returns></returns>
protected virtual string PerformExplain(Version luceneVersion, FSDirectory fsDirectory, string searchQuery, int resultId)
{
/*
* The obvious problem here is that we're not using the exact same search as the real one.
*/
var explanation = string.Empty;
using (var indexSearcher = new IndexSearcher(fsDirectory, false))
{
var analyzer = new StandardAnalyzer(luceneVersion);
var queryParser = new MultiFieldQueryParser(luceneVersion, new[] { "Id".ToLowerInvariant() }, analyzer)
{
DefaultOperator = QueryParser.Operator.AND
};
var query = this.searchQueryParser.ParseQuery(searchQuery, queryParser);
explanation = indexSearcher.Explain(query, resultId).ToHtml();
analyzer.Close();
}
return explanation;
}
示例8: Search
public SearchResults Search(DocumentRoot root, string term)
{
var results = new SearchResults();
var indexPath = _settings.GetSearchIndexPath();
var version = Lucene.Net.Util.Version.LUCENE_30;
using (var directory = FSDirectory.Open(new DirectoryInfo(indexPath)))
using (var indexReader = IndexReader.Open(directory, true))
using (var indexSearch = new IndexSearcher(indexReader))
{
var analyzer = new StandardAnalyzer(version);
var queryParser = new MultiFieldQueryParser(version, new[] { "Title", "Body" }, analyzer);
var query = queryParser.Parse(term);
var resultDocs = indexSearch.Search(query, indexReader.MaxDoc);
var hits = resultDocs.ScoreDocs;
foreach (var hit in hits)
{
var doc = indexSearch.Doc(hit.Doc);
results.Documents.Add(new SearchResult
{
Score = hit.Score,
Snippet = doc.Get("Snippet"),
Title = doc.Get("Title")
});
}
}
return results;
}
示例9: GetQuery
public Query GetQuery()
{
var booleanQuery = new BooleanQuery
{
{
new TermRangeQuery(
FieldDefinition.GetFieldName<PublishedOnFieldDefinition>(), null,
DateTools.DateToString(CurrentRequestData.Now, DateTools.Resolution.SECOND), false, true),
Occur.MUST
}
};
if (!String.IsNullOrWhiteSpace(Term))
{
var indexDefinition = IndexingHelper.Get<WebpageSearchIndexDefinition>();
var analyser = indexDefinition.GetAnalyser();
var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, indexDefinition.SearchableFieldNames, analyser);
Query query = Term.SafeGetSearchQuery(parser, analyser);
booleanQuery.Add(query, Occur.MUST);
}
if (CreatedOnFrom.HasValue || CreatedOnTo.HasValue)
booleanQuery.Add(GetDateQuery(), Occur.MUST);
if (!string.IsNullOrEmpty(Type))
booleanQuery.Add(new TermQuery(new Term(FieldDefinition.GetFieldName<TypeFieldDefinition>(), Type)),
Occur.MUST);
if (Parent != null)
booleanQuery.Add(
new TermQuery(new Term(FieldDefinition.GetFieldName<ParentIdFieldDefinition>(), Parent.Id.ToString())), Occur.MUST);
return booleanQuery;
}
示例10: RemoveFromIndex
public void RemoveFromIndex(string[] keys, string value)
{
var analyzer = new AccentedAnalyzer();
var query = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, keys, analyzer);
var directory = FSDirectory.Open(System.IO.Directory.GetParent(IndexPath));
IndexReader idxReader = IndexReader.Open(indexPath);
var searcher = new IndexSearcher(directory, true);
query.SetDefaultOperator(QueryParser.Operator.AND);
var q = query.Parse(value);
int top = idxReader.MaxDoc();
var results = TopScoreDocCollector.create(top, true);
searcher.Search(q, results);
ScoreDoc[] hits = results.TopDocs().scoreDocs;
Document[] documents = new Document[hits.Length];
IndexReader indexReader = null;
for (int i = 0; i < hits.Length; i++)
{
int docId = hits[i].doc;
indexReader = IndexReader.Open(directory, false);
indexReader.DeleteDocument(docId);
indexReader.Commit();
indexReader.Flush();
indexReader.Close();
}
searcher.Close();
directory.Close();
}
示例11: Search
public ProjectData[] Search(string searchTerm)
{
IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory);
IntegralCollector searcherCollector = new IntegralCollector();
// Setup the fields to search through
string[] searchfields = new string[] { "name", "vessel" };
// Build our booleanquery that will be a combination of all the queries for each individual search term
var finalQuery = new BooleanQuery();
var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, searchfields, analyzer);
// Split the search string into separate search terms by word
string[] terms = searchTerm.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string term in terms)
finalQuery.Add(parser.Parse(term.Replace("~", "") + "~"),Occur.SHOULD);
searcher.Search(finalQuery, searcherCollector);
var results = new ProjectData[searcherCollector.Docs.Count];
for (int i = 0; i < searcherCollector.Docs.Count; i++)
{
var doc = searcher.Doc(searcherCollector.Docs[i]);
results[i] = new ProjectData(doc.Get("name"), doc.Get("vessel"));
}
return results;
}
示例12: _search
// main search method
private static IEnumerable<SampleData> _search(string searchQuery, string searchField = "") {
// validation
if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<SampleData>();
// set up lucene searcher
using (var searcher = new IndexSearcher(_directory, false)) {
var hits_limit = 1000;
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
// search by single field
if (!string.IsNullOrEmpty(searchField)) {
var parser = new QueryParser(Version.LUCENE_29, searchField, analyzer);
var query = parseQuery(searchQuery, parser);
var hits = searcher.Search(query, hits_limit).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Close();
searcher.Dispose();
return results;
}
// search by multiple fields (ordered by RELEVANCE)
else {
var parser = new MultiFieldQueryParser
(Version.LUCENE_29, new[] {"Id", "Name", "Description"}, analyzer);
var query = parseQuery(searchQuery, parser);
var hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Close();
searcher.Dispose();
return results;
}
}
}
示例13: searchLucene
public Data searchLucene(Data data)
{
Account_lg account = new Account_lg();
List<string> item = new List<string>();
Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo("C:\\Visual Studio 2010\\Transaction" + "\\LuceneIndex"));
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "name", "username" }, analyzer); //search for multifield
Query query = parser.Parse((data.getString("search")) + "*"); //cant search blank text with wildcard as first character
TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true);
searcher.Search(query, collector);
ScoreDoc[] hits = collector.TopDocs().ScoreDocs;
int count = hits.Length;
for (int i = 0; i < count; i++)
{
int docId = hits[i].Doc;
float score = hits[i].Score;
Document doc = searcher.Doc(docId);
string id = doc.Get("id");
item.Add(id);
}
Data list = account.selectUser(data, item.ToArray());
reader.Dispose();
searcher.Dispose();
return list;
}
示例14: Search
IEnumerable<string> Search(string textToFind)
{
var reader = IndexReader.Open(_ramDirectory, true);
var searcher = new IndexSearcher(reader);
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
var parser = new MultiFieldQueryParser(Version.LUCENE_29, _searchFields, analyzer);
var query = parser.Parse(textToFind);
var collector = TopScoreDocCollector.create(100, true);
searcher.Search(query, collector);
var hits = collector.TopDocs().scoreDocs;
var foundKeys = new List<string>();
foreach (ScoreDoc scoreDoc in hits)
{
var document = searcher.Doc(scoreDoc.doc);
var key = document.Get(_keyField);
if (key != null && !foundKeys.Contains(key))
{
foundKeys.Add(key);
}
}
reader.Close();
searcher.Close();
analyzer.Close();
return foundKeys;
}
示例15: ParseQuery
private static Query ParseQuery(string searchTerm)
{
var fields = new Dictionary<string, float> { { "Id", 1.2f }, { "Title", 1.0f }, { "Tags", 1.0f}, { "Description", 0.8f }, { "Author", 0.6f } };
var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion);
searchTerm = QueryParser.Escape(searchTerm).ToLowerInvariant();
var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields.Keys.ToArray(), analyzer, fields);
var conjuctionQuery = new BooleanQuery();
conjuctionQuery.SetBoost(1.5f);
var disjunctionQuery = new BooleanQuery();
var wildCardQuery = new BooleanQuery();
wildCardQuery.SetBoost(0.7f);
var exactIdQuery = new TermQuery(new Term("Id-Exact", searchTerm));
exactIdQuery.SetBoost(2.5f);
foreach(var term in searchTerm.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
conjuctionQuery.Add(queryParser.Parse(term), BooleanClause.Occur.MUST);
disjunctionQuery.Add(queryParser.Parse(term), BooleanClause.Occur.SHOULD);
foreach (var field in fields)
{
var wildCardTermQuery = new WildcardQuery(new Term(field.Key, term + "*"));
wildCardTermQuery.SetBoost(0.7f * field.Value);
wildCardQuery.Add(wildCardTermQuery, BooleanClause.Occur.SHOULD);
}
}
return conjuctionQuery.Combine(new Query[] { exactIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery });
}