0

我已经从 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"
          }
        ]
    }
};
4

2 回答 2

0

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

于 2016-10-09T09:12:15.943 回答
0

首先,“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"" } }] } }}");
于 2016-10-12T11:19:15.363 回答