【问题标题】:Elasticsearch Nest - simple query with multiple filtersElasticsearch Nest - 带有多个过滤器的简单查询
【发布时间】:2015-09-03 22:08:58
【问题描述】:

我正在尝试在我想要的地方做一个简单的查询:

select * from POC where Account_No = [accountno] and BUSINESS_UNIT = [business_unit] 

以下是我没有运气的情况。有什么想法吗?

var filters = new List<FilterContainer>();

filters.Add(new FilterDescriptor<poc>().Bool(b => b.Must(m => m.Term(i => i.Account_No, txtAccount.Text))));
filters.Add(new FilterDescriptor<poc>().Bool(b => b.Must(m => m.Term(i => i.BUSINESS_UNIT, txtBU.Text))));

var searchDescriptor = new SearchDescriptor<poc>();
// Filter with AND operator
searchDescriptor.Filter(f => f.And(filters.ToArray()));

var r = client.Search<poc>(searchDescriptor);

更新

第二个过滤器似乎不起作用。 当我运行它时,我没有得到我不确定的记录。我在查看帐户查询时运行记录时看到它

{
  "query": {
    "term": {
      "BUSINESS_UNIT": "CSPCN"
    }
  }
}

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    这应该做你想做的:

    FilterContainer filter = null;
    
    filter &= Filter<poc>.Term(i => i.Account_No, txtAccount.Text);
    filter &= Filter<poc>.Term(i => i.BUSINESS_UNIT, txtBU.Text);
    
    var r = client.Search<poc>(sd => sd.Filter(f => filter));
    

    【讨论】:

    • 我发现Business unit应该是小写的。感谢您的回复
    • 啊,很好。听起来你正在使用标准分析仪。您可以为您的类型指定架构并使用不区分大小写的不同分析器。
    【解决方案2】:

    我将过滤器更改为使用 Match 而不是 Term,它似乎有效。

    我不确定为什么会这样,但是当我使用 Business_Unit 使用 Term 进行搜索时,即使它本身也不会返回任何结果。

    var filters = new List<FilterContainer>();
    
    filters.Add(new FilterDescriptor<poc>().Bool(b => b.Must(m => m.Query(q => q.Wildcard(z => z.Account_No, txtAccount.Text + "*")))));
    filters.Add(new FilterDescriptor<poc>().Bool(b => b.Must(m => m.Query(q => q.Match(mat => mat.OnField(z => z.BUSINESS_UNIT).Query(txtBU.Text))))));
    
    var searchDescriptor = new SearchDescriptor<poc>();
    // Filter with AND operator
    searchDescriptor.Filter(f => f.And(filters.ToArray()));
    
    var r = client.Search<poc>(searchDescriptor);
    

    更新

    我可以使用 match,但我只需要将值设为小写,这就解释了为什么它没有返回任何结果。

    {
      "query": {
        "term": {
          "BUSINESS_UNIT": "cspcn"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2020-04-27
      相关资源
      最近更新 更多