搜索Api用法和BoolQueryDescriptor - Nest Client

时间:2016-05-02 05:34:28

标签: elasticsearch nest

Nest版本: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)
    )
  );
}

此处query.Query(q=>q.Bool(queryParameter))未将queryParameter作为参数,即使queryParameter的类型为BoolQueryDescriptor。不知道这里有什么问题。已复制下面的Generic类查询构建器类,即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; }
  }

谢谢, 帕

1 个答案:

答案 0 :(得分:1)

在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)
    )
);