简体   繁体   English

使用 Jest 或 Java API 有没有办法告诉 Elasticsearch 从 json 作为字符串创建文档?

[英]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 字符串。

SOLUTION:解决方案:

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.

相关问题 使用列表创建 TermsQuery<string> 使用 elasticsearch java api 客户端</string> - Create TermsQuery with List<String> using elasticsearch java api client 如何使用Elasticsearch Java Api使用query_string创建查询 - How to create a query with query_string using Elasticsearch Java Api 使用 Java API 时从 JSON DSL 构造 QueryBuilder - Construct QueryBuilder from JSON DSL when using Java API in ElasticSearch? 如何使用Java手动展平Elasticsearch嵌套JSON文档? - How to manually flatten Elasticsearch nested JSON documents using Java? Elasticsearch-Java RestHighLevelClient-如何使用滚动API获取所有文档 - Elasticsearch - Java RestHighLevelClient - how to get all documents using scroll api 有没有办法使用 elasticsearch java api 使用多重前缀? - is there way to use multiprefix using elasticsearch java api? 使用Java API在Elasticsearch中创建MongoDB River - Create MongoDB river in Elasticsearch using Java API 使用Java API动态创建Json字符串吗? - Create Json String Dynamically using Java API's? 有没有办法用elasticsearch java api加载一个包含索引映射的json文件? - is there a way to load a json file containing the mapping of an index with the elasticsearch java api? 使用 Jest 在 Elasticsearch 中删除没有父级的子文档 - Delete child documents without parent in Elasticsearch using Jest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM