ElasticSearch 5.1使用乘法过滤器过滤查询

时间:2017-02-25 19:29:11

标签: c# elasticsearch

我需要使用ElasticSearch 5.1为查询请求使用多个过滤器(范围和术语)。如果我单独使用它们(只有一个过滤器)就可以了:

var o = new
            {
                size = 20,
                query = new
                {
                    @bool = new
                    {
                        must = new
                        {
                            query_string = new
                            {
                                fields = new[] { "Title" },
                                query = search_query
                            }
                        },
                        filter = new
                        {
                            terms = new
                            {
                                SourceId = new[] {10,11,12}
                            }
                        }
                    }
                }                
            };

OR

var o = new
            {
                size = 20,
                query = new
                {
                    @bool = new
                    {
                        must = new
                        {
                            query_string = new
                            {
                                fields = new[] { "Title" },
                                query = search_query
                            }
                        },
                        filter = new
                        {
                            range = new
                            {
                                PostPubDate = new
                                {
                                    gte = "2015-10-01T00:00:00",
                                    lte = "2015-11-01T12:00:00"
                                }
                            }
                        }
                    }
                }                
            };

如果我同时使用它们,我会得到400响应错误:

string url = "http://localhost:9200/neg_collector/posts/_search";
            var request = (HttpWebRequest)HttpWebRequest.Create(url);
            var o = new
            {
                size = 20,
                query = new
                {
                    @bool = new
                    {
                        must = new
                        {
                            query_string = new
                            {
                                fields = new[] { "Title" },
                                query = search_query
                            }
                        },
                        filter = new
                        {
                            terms = new
                            {
                                SourceId = new[] {10,11,12}
                            },
                            range = new
                            {
                                PostPubDate = new
                                {
                                    gte = "2015-10-01T00:00:00",
                                    lte = "2015-11-01T12:00:00"
                                }
                            }
                        }
                    }
                }                
            };


            request.Method = "POST";
            var jsonObj = JsonConvert.SerializeObject(o);
            var data = Encoding.UTF8.GetBytes(jsonObj);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            string responseString = string.Empty;
            var response = (HttpWebResponse)request.GetResponse();

我做错了什么?谢谢

1 个答案:

答案 0 :(得分:1)

您的过滤器只需要是一个对象数组:

    ...
                    filter = new object[]
                    {
                      new {
                        terms = new
                        {
                            SourceId = new[] {10,11,12}
                        }
                      },
                      new {
                        range = new
                        {
                            PostPubDate = new
                            {
                                gte = "2015-10-01T00:00:00",
                                lte = "2015-11-01T12:00:00"
                            }
                        }
                      }
                    }