c# - 使用乘法过滤器的ElasticSearch 5.1过滤查询

标签 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}
                            }
                        }
                    }
                }                
            };

要么
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();

我究竟做错了什么?谢谢

最佳答案

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

    ...
                    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"
                            }
                        }
                      }
                    }

关于c# - 使用乘法过滤器的ElasticSearch 5.1过滤查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42460647/

相关文章:

c# - 向现有函数添加功能(C#、C++)

c# - HubConnection.Start 仅在从单例对象调用时抛出错误

c# - 如何从添加 '\n\r' 作为行尾的多行 TextBox 中将字符串拆分为 List<string>?

elasticsearch - 嵌套聚合:如何获取聚合对象的属性

elasticsearch - 如何在elasticsearch中从节点数据到master的连接?

elasticsearch - 多重匹配并将查询一起匹配

c# - 如何在 DateTimePicker 控件中自定义日历?

c# - LINQ:使用 INNER JOIN、Group 和 SUM

json - 如何使用 logstash 将压缩的 json 导入 elasticsearch?

elasticsearch - 如果转换返回NULL,kafka接收器连接器是否插入记录?