巢状版本:2.10
Elasticsearch.Net版本:2.10
我正在尝试编写如下查询
BoolQueryDescriptor<ErrorLog> queryParameter;
queryParameter = QueryFilterBuilder.BuildQueryFilter<ErrorLog>(searchInputs.SearchParameters);
if (queryParameter != null)
{
var searchResultnew = elasticClient.Search<ErrorLog>(query => query
.Query(q=>q.Bool(queryParameter))
.Index("errorlogs")
.Size(searchInputs.ResultCount)
.From(searchInputs.From)
.Sort(s => s.OnField(sortField)
.Order(sortOrder)
)
);
}
在这里,即使queryParameter的类型为
query.Query(q=>q.Bool(queryParameter))
,BoolQueryDescriptor
也不将queryParameter作为参数。不知道这里出了什么问题。已在下面复制了通用类查询构建器类,即QueryFilterBuilder
请让我知道是否需要更多详细信息。此类是使用Nest 1.x编写的,现在我们尝试使用Nest 2.0修复损坏的问题。 public static class QueryFilterBuilder
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
public static BoolQueryDescriptor<T> BuildQueryFilter<T>(IList<SearchParameters> searchParameterList) where T : class
{
QueryContainer filterContainer = null;
BoolQueryDescriptor<T> bqd = new BoolQueryDescriptor<T>();
if (searchParameterList == null)
{
throw (new ArgumentNullException("searchParameterList"));
}
var fieldGroups = from parameter in searchParameterList
group parameter by parameter.FieldName into fieldList
orderby fieldList.Key
select fieldList;
foreach (var fgrp in fieldGroups)
{
string fieldName = string.Empty;
string fieldDataType = string.Empty;
string searchText = string.Empty;
SearchFilterType operand = SearchFilterType.Unknown;
foreach (var sp in fgrp.ToList())
{
switch (sp.Operand)
{
case SearchFilterType.Equals:
bqd.Must(EqualTo(searchText, sp.FieldName, sp.Analysed));
operand = sp.Operand;
break;
case SearchFilterType.Contains:
bqd.Must(Contains(searchText, sp.FieldName, sp.Analysed, fieldDataType));
operand = sp.Operand;
break;
}
}
}
return bqd;
}
public static QueryContainer EqualTo(string searchText, string field, bool fieldanalysis)
{
QueryContainer queryContainer = null;
if (searchText == null)
{
throw (new ArgumentNullException("searchText"));
}
if (fieldanalysis)
{
searchText = searchText.ToLower();
queryContainer = new TermQuery()
{
Field = field + ".sort",
Value = searchText
};
}
else
{
queryContainer &= new TermQuery()
{
Field = field,
Value = searchText
};
}
return queryContainer;
}
}
---------------------
public class SearchParameters
{
/// <summary>
/// Gets or sets SearchText
/// </summary>
public String SearchText { get; set; }
/// <summary>
/// Gets or sets FieldName
/// </summary>
public String FieldName { get; set; }
/// <summary>
/// Gets or sets Operand
/// </summary>
public SearchFilterType Operand { get; set; }
/// <summary>
/// Gets or sets FieldName
/// </summary>
public String FieldDataType { get; set; }
/// <summary>
/// Gets or sets Analyzed status
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Analysed")]
public bool Analysed { get; set; }
}
谢谢,
帕万
最佳答案
在NEST 2.x中, .Bool()
takes a Func<BoolQueryDescriptor<T>, IBoolQuery>
和BoolQueryDescriptor<T>
实现了IBoolQuery
,因此您可以执行
elasticClient.Search<ErrorLog>(query => query
.Query(q=>q.Bool(b => queryParameter))
.Index("errorlogs")
.Size(searchInputs.ResultCount)
.From(searchInputs.From)
.Sort(s => s.OnField(sortField)
.Order(sortOrder)
)
);
关于elasticsearch - 搜索Api使用情况和BoolQueryDescriptor-Nest Client,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36975571/