目前我正在開發一個 Spring Boot 專案,使用 Spring Data ElasticSearch(版本 4.1.7)從特定的 Elastic 搜索索引中獲取資料。我能夠連接到彈性搜索并獲取與特定索引關聯的所有資料。但是索引中有一些欄位回傳空值。彈性搜索中的索引中存在實際值。
由于欄位型別不正確,我在檢索特定欄位時遇到問題。我創建了一個檔案類
@Document(indexName = "netstat*")
@Setting(settingPath = "static/es-settings.json")
public class NetworkStats {
@Id
@Field(type = FieldType.Keyword)
private String id;
@Field
private String timeStamp ;
@Field(type = FieldType.Text)
private String podName ;
@Field(type = FieldType.Text)
private String iporHost ;
@Field(type = FieldType.Auto)
private String connectionCount ;
@Field(type = FieldType.Text)
private String command ;
}
除上述內容外,我還為該欄位定義了 getter 和 setter。我無法檢索欄位 connectionCount、iporHost、podName、timeStamp 的值。盡管這些欄位在彈性索引中具有值。
下面來自 ElasticSearch 的示例資料
{
"_index": "netstat-2021.11.09",
"_type": "_doc",
"_id": "0sJmA30BW7LXXVrK0nCg",
"_score": 1.0,
"_source": {
"podname": "logindeployment-5479f7-4f2qr",
"input": {
"type": "log"
},
"iporhost": "6200",
"tags": [],
"command": "OUTESTAB",
"agent": {
"version": "7.15.1",
"id": "e8feeb4e-1f9e-41b2-bcba-38e507d176db",
"type": "filebeat",
"hostname": "BL1726L",
"name": "BL1726L",
"ephemeral_id": "c2859ece-3e95-4204-909d-af59322b9fa2"
},
"timestamp": "05-10-21T13:17:25.014719626",
"ecs": {
"version": "1.11.0"
},
"connectionscount": "2",
"@timestamp": "2021-11-09T06:31:29.104Z",
"log": {
"offset": 1002,
"file": {
"path": "D:\\elastic_stack\\data\\log\\netStats.log"
}
}
}
},
需要有關如何檢索上述欄位的資料的幫助。我猜欄位映射存在一些問題。非常感謝您對此的任何幫助。
uj5u.com熱心網友回復:
Elasticsearch 中的欄位似乎都有小寫名稱。您的房產有駝峰式案例。所以你需要定義名稱:
@Field(type = FieldType.Text, name ="podname")
private String podName ;
如果您沒有定義 aFieldType或 use FieldType.Auto,則不會寫入任何映射,Elasticsearch 將嘗試猜測型別。您應該定義正確的型別以確保按需要完成轉換。
特別是對于timestamp欄位,您應該定義與索引中的內容相匹配的日期格式。
由于索引存在,您應該確保在物體定義中使用與索引中已使用的型別相同的型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355192.html
