[英]Using Jest or Java API is there a way to tell Elasticsearch to create documents from json as a String?
Given this POJO:鉴于此 POJO:
public class People {
String sex;
long age;
String names;
}
The "names" property will be a json string for which I need to create nested documents for. “名称”属性将是一个 json 字符串,我需要为其创建嵌套文档。 Here is an example of an instance I need to save to Elasticsearch using Jest Client:
这是我需要使用 Jest Client 保存到 Elasticsearch 的实例的示例:
People people = new People();
people.setSex("Male");
people.setAge(21);
people.setNames("[{\"fname\": \"Bob\",\"lname\": \"Smith\"},[{\"fname\": \"Mike\",\"lname\": \"Johnson\"}");
Index index = new Index.Builder(people).index("indexName").type("aType").build();
jestClient.execute(index);
The result document in ES looks like: ES 中的结果文档如下所示:
"_source" : {
"sex" : "Male",
"age" : 21,
"names" : "[{\"fname\": \"Bob\",\"lname\": \"Smith\"},{\"fname\": \"Mike\",\"lname\": \"Johnson\"}]"
}
So it took the String names and inserted it as a literal String, which makes sense but I actually need to create documents from each name object.所以它采用了字符串名称并将其作为文字字符串插入,这是有道理的,但我实际上需要从每个名称对象创建文档。 In other words I want it to look like this:
换句话说,我希望它看起来像这样:
"_source" : {
"sex" : "Male",
"age" : 21,
"names" : [{
"fname": "Bob",
"lname": "Smith"
}, {
"fname": "Mike",
"lname": "Johnson"
}]
}
I tried adding a mapping to tell ES to treat it as "nested" but then I get a Mapper Parsing Exception saying "tried to parse field [names] as object, but found a concrete value".我尝试添加一个映射来告诉 ES 将其视为“嵌套”,但随后我收到一个映射器解析异常,说“试图将字段 [名称] 解析为对象,但找到了一个具体值”。
I know I should be able to do this if I create an actual Name POJO object and have a list of them, but unfortunately due to requirements I am unable to do this.我知道如果我创建一个实际的 Name POJO 对象并有一个列表,我应该能够做到这一点,但不幸的是,由于要求,我无法做到这一点。 I have to use the string of JSON provided in the format specified above.
我必须使用以上面指定的格式提供的 JSON 字符串。
Thanks to Vishal Rao for pointing me in the right direction.感谢 Vishal Rao 为我指明了正确的方向。
The solution was to change the "names" type to a JsonArray (Google GSON).解决方案是将“名称”类型更改为 JsonArray (Google GSON)。 Then used the Google GSON parser as such:
然后使用 Google GSON 解析器:
People people = new People();
people.setSex("Male");
people.setAge(21);
String json = "[{\"fname\": \"Bob\",\"lname\": \"Smith\"},[{\"fname\": \"Mike\",\"lname\": \"Johnson\"}"
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);
JsonArray jsonArray = jsonElement.getAsJsonArray();
people.setNames(jsonArray);
Index index = new Index.Builder(people).index("indexName").type("aType").build();
jestClient.execute(index);
In addition I also have a mapping that sets the names property to a nested type.此外,我还有一个将 names 属性设置为嵌套类型的映射。
You might want to try converting your string to a JSON object first, that's probably why you're getting that error.您可能想先尝试将字符串转换为 JSON 对象,这可能就是您收到该错误的原因。 Elasticsearch tries to parse it as an object but finds a string there instead.
Elasticsearch 尝试将其解析为一个对象,但在那里找到了一个字符串。 Maybe do something like:
也许做这样的事情:
JSONObject jsonObj = new JSONObject(names);
and then using that object.然后使用该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.