【问题标题】:How to add conditional expressions while querying Elastic Search?查询 Elastic Search 时如何添加条件表达式?
【发布时间】:2018-02-03 07:05:27
【问题描述】:

我想知道我们是否可以在从弹性搜索中获取数据的同时编写条件表达式。

例如 - 我拥有的记录中有一个列。该列具有从 1 到 100 的整数值。我想获取该列具有偶数值的所有此类记录。目前,我正在这样做,构建一个我想要的值列表,然后将它们传递给 TermsQuery :

 for(int i = 1; i <= 100; i++)
        {
            if (i%2 == 0)
                possibleMatches.Add(i);
        }

        Func<TermsQueryDescriptor<MyClass>, ITermsQuery> TermsQuery = t =>
        {
            return t.Field(f => f.MyColumn).Terms(possibleMatches);
        };

        var results = collection.Search(s =>
                      s.Index(MyIndex).Type(MyElasticsearchType)
                     .Query(qu=>qu.Terms(TermsQuery));

是否可以在术语查询或其他查询类型中编写条件表达式?说类似

t.Field(f=> f.MyColumn%2 == 0)

【问题讨论】:

    标签: c# .net elasticsearch nest


    【解决方案1】:

    不可能按照建议的方式编写条件表达式。你有两个选择:

    1. 使用a script query 并将条件写入Painless scripting language
    2. 索引MyColumn 值是奇数还是偶数到单独的字段中。

    第一种方法更灵活,而第二种方法性能更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      相关资源
      最近更新 更多