【问题标题】:ElasticSearch filtered query _search 400 error responseElasticSearch 过滤查询_search 400 错误响应
【发布时间】:2017-02-26 03:19:35
【问题描述】:

我正在尝试对搜索查询使用过滤器。搜索请求在没有过滤器的情况下正常工作。但是使用过滤器我得到 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。 我究竟做错了什么?谢谢

【问题讨论】:

  • 你的 ES 版本是什么,你的原始反应是什么?请分享。
  • 另一方面,您应该使用 application/json 内容类型。
  • @hkulekci 是的,问题出在 ES 的版本上(我用 ES2 尝试了这段代码,它有效。现在它不适用于 ES 5)

标签: c# elasticsearch


【解决方案1】:

所以问题是这个语法适用于 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多