本文整理汇总了C#中QueryBuilder.Query方法的典型用法代码示例。如果您正苦于以下问题:C# QueryBuilder.Query方法的具体用法?C# QueryBuilder.Query怎么用?C# QueryBuilder.Query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QueryBuilder
的用法示例。
在下文中一共展示了QueryBuilder.Query方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildQuery
public object BuildQuery(ISearchCriteria criteria)
{
var builder = new QueryBuilder<ESDocument>();
var mainFilter = new Filter<ESDocument>();
var mainQuery = new BoolQuery<ESDocument>();
#region Sorting
// Add sort order
if (criteria.Sort != null)
{
var fields = criteria.Sort.GetSort();
foreach (var field in fields)
{
builder.Sort(d => d.Field(field.FieldName, field.IsDescending ? SortDirection.desc : SortDirection.asc, ignoreUnmapped: field.IgnoredUnmapped));
}
}
#endregion
#region Filters
// Perform facet filters
if (criteria.CurrentFilters != null)
{
var combinedFilter = new BoolFilter<ESDocument>();
// group filters
foreach (var filter in criteria.CurrentFilters)
{
// Skip currencies that are not part of the filter
if (filter.GetType() == typeof(PriceRangeFilter)) // special filtering
{
var priceRangeFilter = filter as PriceRangeFilter;
if (priceRangeFilter != null)
{
var currency = priceRangeFilter.Currency;
if (!currency.Equals(criteria.Currency, StringComparison.OrdinalIgnoreCase))
continue;
}
}
var filterQuery = ElasticQueryHelper.CreateQuery(criteria, filter);
if (filterQuery != null)
{
combinedFilter.Must(c => c.Bool(q=>filterQuery));
}
}
mainFilter.Bool(bl => combinedFilter);
}
#endregion
#region CatalogItemSearchCriteria
if (criteria is CatalogItemSearchCriteria)
{
var c = criteria as CatalogItemSearchCriteria;
mainQuery.Must(m => m
.Range(r => r.Field("startdate").To(c.StartDate.ToString("s")))
);
if (c.StartDateFrom.HasValue)
{
mainQuery.Must(m => m
.Range(r => r.Field("startdate").From(c.StartDateFrom.Value.ToString("s")))
);
}
if (c.EndDate.HasValue)
{
mainQuery.Must(m => m
.Range(r => r.Field("enddate").From(c.EndDate.Value.ToString("s")))
);
}
mainQuery.Must(m => m.Term(t => t.Field("__hidden").Value("false")));
if (c.Outlines != null && c.Outlines.Count > 0)
AddQuery("__outline", mainQuery, c.Outlines);
if (!String.IsNullOrEmpty(c.SearchPhrase))
{
AddQueryString("__content", mainQuery, c);
}
if (!String.IsNullOrEmpty(c.Catalog))
{
AddQuery("catalog", mainQuery, c.Catalog);
}
}
#endregion
if (criteria is ElasticSearchCriteria)
{
var c = criteria as ElasticSearchCriteria;
mainQuery.Must(m => m.Custom(c.RawQuery));
}
builder.Query(q => q.Bool(b => mainQuery));
//.........这里部分代码省略.........
示例2: Search
public virtual ISearchResults Search(string scope, ISearchCriteria criteria)
{
var builder = new QueryBuilder<ESDocument>();
var command = new SearchCommand(scope, criteria.DocumentType);
// Add sort order
if (criteria.Sort != null)
{
var fields = criteria.Sort.GetSort();
foreach (var field in fields)
{
command.Sort(field.FieldName, field.IsDescending ? SortDirection.desc : SortDirection.asc);
}
}
command.Size(criteria.RecordsToRetrieve);
command.From(criteria.StartingRecord);
// Add search facets
var facets = GetFacets(criteria);
builder.Facets(f => facets);
// Add spell checking
// TODO: options.SpellCheck = new SpellCheckingParameters { Collate = true };
// Build query
var query = (BoolQuery<ESDocument>)_queryBuilder.BuildQuery(criteria);
builder.Query(q => q.Bool(b => query));
SearchResult<ESDocument> resultDocs;
// Add some error handling
try
{
resultDocs = Client.Search(command, builder);
}
catch (Exception ex)
{
throw new ElasticSearchException("Search using Elastic Search server failed, check logs for more details.", ex);
}
// Parse documents returned
var documents = new ResultDocumentSet { TotalCount = resultDocs.hits.total };
var docList = new List<ResultDocument>();
foreach (var indexDoc in resultDocs.Documents)
{
var document = new ResultDocument();
foreach (var field in indexDoc.Keys)
document.Add(new DocumentField(field, indexDoc[field]));
docList.Add(document);
}
documents.Documents = docList.ToArray();
// Create search results object
var results = new SearchResults(criteria, new [] { documents });
// Now add facet results
var groups = new List<FacetGroup>();
if (resultDocs.facets != null)
{
foreach (var filter in criteria.Filters)
{
var groupCount = 0;
var group = new FacetGroup(filter.Key);
var attributeFilter = filter as AttributeFilter;
if (attributeFilter != null)
{
var myFilter = attributeFilter;
var values = myFilter.Values;
if (values != null)
{
var key = filter.Key.ToLower();
if (!resultDocs.facets.ContainsKey(key))
continue;
var facet = resultDocs.facets[key] as TermsFacetResult;
if (facet != null)
{
foreach (var value in values)
{
//facet.terms
var termCount = from f in facet.terms where f.term.Equals(value.Id, StringComparison.OrdinalIgnoreCase) select f.count;
var enumerable = termCount as int[] ?? termCount.ToArray();
if (!enumerable.Any())
continue;
//var facet = from resultFacet
var newFacet = new Facet(@group, value.Id, GetDescription(value, criteria.Locale), enumerable.SingleOrDefault());
@group.Facets.Add(newFacet);
}
groupCount++;
}
}
}
//.........这里部分代码省略.........