添加第二个筛选查询时,搜索停止工作

时间:2017-11-16 23:36:58

标签: c# asp.net-mvc elasticsearch nest

我希望你能解决我遇到的问题。我是ElasticSearch的新手,我目前正在尝试实现一个日志搜索方法,该方法将通过大量日志进行过滤。目前,我可以根据选定的日志类型进行过滤,并且工作正常,但只要我添加第二个过滤器,它就不再返回任何结果。

到目前为止我添加的两个滤镜之间的唯一区别是,一个是int数组,另一个是当前代码中看到的字符串列表,我也尝试将其转换为字符串数组但是没有帮助。

    [Route("logs/search")]
    [HttpGet]
    public ActionResult Index(int[] logTypes, int entityType, string entitySource)
    {
        using (var db = new mtaEntities())
        {
            var targetSources = new List<string>();
            switch (entityType)
            {
                // Username/charactername
                case 0:
                    var characters = db.characters.Where(i => i.charactername.ToLower().Replace("_", " ") == entitySource.ToLower()).ToList();
                    accounts user = null;
                    if (characters.Any())
                    {
                        var accountId = characters.FirstOrDefault().account;
                        if (accountId != null)
                        {
                            user = db.accounts.FirstOrDefault(i => i.id == accountId);
                            characters.AddRange(db.characters.Where(i => i.account == user.id));
                        }
                    }
                    else
                    {
                        user = db.accounts.FirstOrDefault(i => i.username.ToLower() == entitySource.ToLower());
                        if (user != null)
                        {
                            characters.AddRange(db.characters.Where(i => i.account == user.id).ToList());
                        }
                    }
                    if (user != null)
                    {
                        targetSources.Add("AC-" + user.id);
                        foreach (var item in characters)
                        {
                            targetSources.Add("CH-" + item.id);
                        }
                    }
                    break;
            }
            var filters = new List<Func<QueryContainerDescriptor<Log>, QueryContainer>>();
            filters.Add(fq => fq.Terms(te => te.Field(fi => fi.LogType).Terms(logTypes)));
            filters.Add(fq => fq.Terms(te => te.Field(fi => fi.SourceUser).Terms(targetSources)));
            var searchDescriptor = new SearchDescriptor<Log>();
            searchDescriptor.Query(q => q.Bool(b => b.Filter(filters)));
            var query = ElasticClient.Search<Log>(searchDescriptor);
            var results = query.Documents;
            return View(new Logs
            {
                IndexLastRebuilt = db.settings.FirstOrDefault(i => i.name == "elasticIndexLastRebuilt").value,
                LogResults = results.ToList(),
                LogTypes = db.logtypes.ToList()
            });
        }
    }

0 个答案:

没有答案