用c#编写ElasticSearch查询

本文关键字:查询 ElasticSearch 编写 | 更新日期: 2023-09-27 18:12:08

我已经从Sense转向用c#编写查询。我遇到的问题是将两个查询或过滤器连接在一起。

在某种意义上我使用了

"and":[

有什么好主意吗?

这是我试图做的查询之一

{
    "filtered" : {
        "query" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "and" : [
                {
                    "range" : {
                        "postDate" : {
                            "from" : "2010-03-01",
                            "to" : "2010-04-01"
                        }
                    }
                },
                {
                    "prefix" : { "name.second" : "ba" }
                }
            ]
        }
    }
}
尝试

////

filtered = new
{
    query = new
    {
        term = new
        {
            name.first = "shay"
        }
    },
    filter = new
    {
        and =   ? 
        [        ?
         {        ?
            range = new
            {
                postDate = new
                {
                    from = "2010-03-01",
                    to ="2010-04-01"
                }
            }
          },
          prefix = new
          {
            name.second = "ba"
          }
        ]
    }
};

用c#编写ElasticSearch查询

不知道你是什么意思,但看起来你想写一个json对象,然后不知道如何在and =之后写
我认为你应该这样写
and = new object[]{ new {range = new { a = "b"}}, new {prefix = new { c = "d"}} }
我猜这是你想要的。

首先," and"、"filtered"查询在2.0.0-beta1中已被弃用。请使用bool查询。

下面是查询 的方法
ElasticClient db = new ElasticClient(uri);
            var nestSearchRequest = db.Search<dynamic>(new SearchRequest(indexName)
            {
                Query = new BoolQuery
                {
                    Must = new QueryContainer[] { new TermQuery { Field = "name.first", Value = "shay" } },
                    Filter = new QueryContainer[] { new DateRangeQuery { Field = "postDate", GreaterThanOrEqualTo = new DateTime(2010, 3, 1), LessThan = new DateTime(2010, 4, 1) }, new PrefixQuery { Field = "name.second", Value = "ba" } }
                }
            });
            var nestFluent = db.Search<dynamic>(
                s => s.Index(indexName).Type("").Query(
                    q=>q.Bool(
                        bq=>bq.
                            Must(q.Term("name.first","shay")).
                            Filter(
                                q.DateRange(drq => drq.Field("postDate").GreaterThanOrEquals(new DateTime(2010, 3, 1)).LessThan(new DateTime(2010, 4, 1))),
                                q.Prefix("name.second", "ba")))));
            var lowLevelQuery = db.LowLevel.Search<dynamic>(indexName, new
            {
                query = new
                {
                    @bool = new
                    {
                        must = new { term = new Dictionary<string, string> { { "name.first", "shay" } } },
                        filter = new object[]
                        { 
                            new { range = new { postDate = new  { gte = new DateTime(2010, 3, 1), lt = new DateTime(2010, 4, 1) } } },
                            new { prefix = new Dictionary<string,string>{ { "name.second", "ba" }  } }
                        }
                    }
                }
            });
            var rawJSON = db.LowLevel.Search<dynamic>(indexName, @"{""query"":{ ""bool"":{ ""must"":{ ""term"":{ ""name.first"":""shay"" } },""filter"":[{ ""range"":{ ""postDate"":{ ""gte"":""2010-03-01"",""lt"":""2010-04-01"" } } },{ ""prefix"":{ ""name.second"":""ba"" } }] } }}");