Elastic Low Level Client搜索查询如何配置多个索引
Elastic Low Level Client多索引指定无效解决方案
根因定位
- 核心问题是你的
appsettings.json中indexesToSearch配置的多索引字符串,逗号后带有多余空格:"webapp-razor-*, systemconfig-api-*"。Elastic低级别客户端不会自动修剪索引名称前后的空白字符,会将空格后的systemconfig-api-*(前缀带空格)作为独立索引名匹配,无对应索引就直接忽略,最终仅第一个不带空格的索引返回结果。
修复方案
方案1:直接修改配置(推荐)
直接删除appsettings.json中索引配置的多余空格即可:
// System settings configured here for the WebApp. Applicable to all users. "SystemSettings": { // Sets the maximum number of distinct values returned by Elastic for a log property "_distinctPropertyValuesLimit": 1000, // See LogPropertiesController.cs // String for the list of Elastic Search indexes that are searched by default. "indexesToSearch": "webapp-razor-*,systemconfig-api-*" }
方案2:代码层兼容(可选,防止后续配置误输入空格)
读取配置后对索引字符串做标准化处理,自动去除所有索引前后的空白:
_indexesToSearch = configuration.GetSection("SystemSettings").GetSection("indexesToSearch").Value; // 新增索引名标准化处理逻辑 _indexesToSearch = string.Join(',', _indexesToSearch.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(idx => idx.Trim())); var searchResponse = await _elasticLowLevelClient.SearchAsync<StringResponse>(_indexesToSearch, @" { ""from"": """ + fromParameter + @""", ""size"": """ + rowsPerPage + @""", ""query"": { ""match"": { """ + searchColumn + @""": { ""query"": """ + searchString + @""" } } }, ""sort"": [ { ""@timestamp"": { ""order"": ""desc"" } } ] } ");
附加排查点
如果修改后仍不生效,可依次验证:
- 确认
systemconfig-api-*通配符匹配的索引真实存在于Elastic集群中 - 确认当前Elastic客户端账号拥有
systemconfig-api-*对应索引的读权限 - 临时写死索引参数为
"webapp-razor-*,systemconfig-api-*"测试,排除其他传参逻辑问题
内容的提问来源于stack exchange,提问作者OJB1
相关产品推荐
相关产品推荐

