用C#编写ElasticSearch查询

时间:2016-10-09 08:47:25

标签: c# elasticsearch sense

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

首先,“和”,“已过滤”的查询已为Deprecated in 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"" } }] } }}");