ElasticSearch过滤了查询_search 400错误响应

时间:2017-02-25 17:39:17

标签: c# elasticsearch

我正在尝试使用带搜索查询的过滤器。搜索requset无需过滤即可正常工作。但是使用过滤器我得到400错误作为响应。 这是类型映射:

var mapp = new
            {
                mappings = new
                {
                    posts = new
                    {
                        properties = new
                        {
                            FullText = new
                            {
                                type = "string",
                                analyzer = "russian"
                            },
                            Title = new
                            {
                                type = "string",
                                analyzer = "russian"
                            },
                            PostPubDate = new
                            {
                                type = "date"
                            },
                            Link = new
                            {
                                type = "string",
                                index = "not_analyzed"
                            },
                            RubricsIds = new
                            {
                                type = "integer"
                            },
                            ObjectsIds = new
                            {
                                type = "integer"
                            },
                            SourceId = new
                            {
                                type = "integer"
                            }
                        }
                    }
                }
            };

这是使用筛选查询进行索引的请求:

string url = "http://localhost:9200/neg_collector/posts/_search";
            var request = (HttpWebRequest)HttpWebRequest.Create(url);
            var o = new
            {
                size = 20,
                query = new
                {
                    filtered = new
                    {
                        query = new
                        {
                            query_string = new
                            {
                                fields = new[] { "Title" },
                                query = search_query
                            }
                        },
                        filter = new
                        {
                            @bool = new
                            {
                                should = new
                                {
                                    term = new
                                    {
                                        SourceId = sIds
                                    }
                                }
                            }
                        }
                    }
                }                
            };


            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;

我想使用整数数组来过滤某些SourceId-s的结果。但我得到错误400。 我究竟做错了什么?谢谢

1 个答案:

答案 0 :(得分:0)

所以问题是这个语法适用于elasticsearch 2版本(并且它在另一台计算机上运行良好)。在这里,我有ElasticSearch 5,应该使用另一个过滤:

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

描述为HERE