【问题标题】:Translate raw elastic search query into NEST query将原始弹性搜索查询转换为 NEST 查询
【发布时间】:2020-04-09 00:17:10
【问题描述】:

我想知道,是否有人可以这么好心地将以下原始弹性搜索查询翻译成 NEST?谢谢!

var json = @"
{
    ""from"": 0,
    ""size"": 50,
    ""query"": {
        ""constant_score"": {
          ""filter"": {
        ""bool"": {
          ""must"": [
            {
              ""range"": {
            ""attachment.content_length"": {
              ""gt"": 0,
              ""lte"": 500
            }
              }
            },
            {
              ""term"": {
            ""ext"": {
              ""value"": ""pdf""
            }
              }
            },
            {
              ""term"": {
            ""check"": {
              ""value"": false
            }
              }
            }
          ]
        }
          }
        }
      }
    }
";

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:
    var searchResponse = await client.SearchAsync<object>(s => s
        .From(0)
        .Size(50)
        .Query(q => q.ConstantScore(cs => cs
            .Filter(f => f.Bool(b => b.Must(
                m => m.Range(r => r.Field("attachment.content_length").GreaterThan(0).LessThanOrEquals(500)),
                m => m.Term(t => t.Field("ext").Value("pdf")),
                m => m.Term(t => t.Field("check").Value(false))
            ))))));
    

    或稍微强类型的版本

    var searchResponse = await client.SearchAsync<Document>(s => s
        .From(0)
        .Size(50)
        .Query(q => q.ConstantScore(cs => cs
            .Filter(f => f.Bool(b => b.Must(
                m => m.Range(r => r.Field(field => field.Attachment.ContentLength).GreaterThan(0).LessThanOrEquals(500)),
                m => m.Term(t => t.Field(field => field.Ext).Value("pdf")),
                m => m.Term(t => t.Field(field => field.Check).Value(false))
            ))))));
    
    public class Document
    {
        public int Id { get; set; }
        public Nest.Attachment Attachment { get; set; }
        public string Ext { get; set; }
        public bool Check { get; set; }
    }
    

    都产生以下查询

    {
        "from": 0,
        "query": {
            "constant_score": {
                "filter": {
                    "bool": {
                        "must": [{
                                "range": {
                                    "attachment.content_length": {
                                        "gt": 0.0,
                                        "lte": 500.0
                                    }
                                }
                            }, {
                                "term": {
                                    "ext": {
                                        "value": "pdf"
                                    }
                                }
                            }, {
                                "term": {
                                    "check": {
                                        "value": false
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        },
        "size": 50
    }
    

    希望对您有所帮助。

    【讨论】:

    • 我认为official docs 非常详细并展示了良好的做法。
    猜你喜欢
    • 2018-11-09
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2015-07-17
    • 2018-09-12
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多