我正在尝试使用以下代码从我的Elasticsearch获取索引数据。但始终结果为0。我该怎么办?我下面的查询返回空结果。
var list = new List<Monitor>();
if (_client.IndexExists(indexName).Exists)
{
var esurl = "http://monitoring1.xxx.software:9200/,http://monitoring2.xxx.software:9200/";
string[] urls = esurl.Split(',');
var nodes = new Uri[2];
for (int i = 0; i < urls.Length; i++)
{
nodes.SetValue(new Uri(urls[i]), i);
}
var connectionPool = new SniffingConnectionPool(nodes);
var connectionSettings = new ConnectionSettings(connectionPool).DefaultIndex(indexName)
.SniffOnConnectionFault(false)
.SniffOnStartup(false)
.SniffLifeSpan(TimeSpan.FromMinutes(1));
var client = new ElasticClient(connectionSettings);
var hits = client.Search<Monitor>(s => s.Index(indexName)).Hits;
QueryContainer query = new TermQuery
{
Field = "DbName",
Value = "Mydb"
};
var searchRequest = new SearchRequest
{
From = 0,
Size = 10,
Query = query
};
var searchResults = client.Search<Monitor>(searchRequest);// Result => 0
list = client.Search<Monitor>(s=>s.Index(indexName)).Hits.Select(q => q.Source).ToList<Monitor>(); // Result => 0 again
}
return list;
**
以下代码与我的映射有关。我认为这很有帮助。
**
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.0,
"hits": [
{
"_index": "monitorsystemdetail-2019",
"_type": "monitorsystemdetailtype",
"_id": "989d7859-3f43-48d2-a978-edb7aaa4c1af",
"_score": 1.0,
"_source": {
"logDate": "2019-12-04T12:58:34Z",
"totalVisitorCountLast5minutes": 120,
"totalVisitorCountLast1Hour": 803,
"totalRequestCountLast5Minutes": 1661,
"totalRequestCountLast1Hour": 25768,
"timeStamp": "4.12.2019 13:58:26",
"problemId": "Script error.",
"operationName": "/",
"customDimensions": "{\"url\":\"https://www.xxx.com.tr/\"}",
"timeStamp2": "4.12.2019 13:58:25",
"problemId2": "Script error.",
"operationName2": "/",
"customDimensions2": "{\"url\":\"https://www.xxx.com.tr/\"}",
"dbName": "TourismDb",
"bufferPage": 2659249.0,
"dbBufferUsedMB": 20775.0,
"dbBufferFreeMB": 0.0,
"dbBufferPercent": 96.153,
"dbBufferPercentAverage": 0.0
}
},
]
},
"aggregations": {
"duplicateCount": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
结果如下:
答案 0 :(得分:1)
DbName
的类型可能为text
,而您正在使用TermQuery
。
如果您想使用TermQuery
来查找DbName
作为完全匹配项,则应该进行更改
Field = "DbName"
更改为Field = "DbName.keyword"
(相应地更改映射),否则将TermQuery
更改为MatchQuery
。
映射
PUT my_index
{
"mappings": {
"properties": {
"DbName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
更新索引-更新映射时
POST my_index/_update_by_query?
conflicts=proceed
希望这会有所帮助