Insert Nested object for indexing in elasticsearch using Java API


(Pritom Ahmed) #1

Hi,

I have the following two classes.

public class Authors implements Serializable {
    private String name;
    private String surname;
    private int numberOfArticles;
}

public class Blog implements Serializable {
    private String name;
    private List<Authors> authors;
}

I use the following code to populate some test data.

public static XContentBuilder getXContentBuilder() throws IOException {
        Blog blog=new Blog();
        blog.setName("www.tfasun.com");
        blog.setAuthors(new ArrayList<Authors>());
        blog.getAuthors().add(new Authors("tarik","fasun",3));
        blog.getAuthors().add(new Authors("tarik1","fasun1",4));
        XContentBuilder contentBuilder = null;
        try {
            contentBuilder = jsonBuilder().startObject();
            contentBuilder.field("name", blog.getName());
            contentBuilder.startArray("authors");
            for (Authors authors : blog.getAuthors()) {
                contentBuilder.startObject();
                contentBuilder.field("name", authors.getName());
                contentBuilder.field("surname", authors.getSurname());
                contentBuilder.field("numberOfArticles", authors.getNumberOfArticles());
                contentBuilder.endObject();
            }
            contentBuilder.endArray().endObject();
        } catch (IOException ex) {
            throw new RuntimeException("Error", ex);
        }
        return contentBuilder;
    }

Then I use elasticsearch's Java API to insert them in elasticsearch.

XContentBuilder contentBuilder = Utilities.getXContentBuilder();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("tfasun", "document_type");
indexRequestBuilder.setSource(contentBuilder).execute().actionGet();

I end up with the following index mapping.

{
  "tfasun" : {
    "aliases" : { },
    "mappings" : {
      "document_type" : {
        "properties" : {
          "authors" : {
            "properties" : {
              "name" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "numberOfArticles" : {
                "type" : "long"
              },
              "surname" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          },
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1504748301549",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "_AJmgF6fSDmDupf84FWYkw",
        "version" : {
          "created" : "5050199"
        },
        "provided_name" : "tfasun"
      }
    }
  }
}

But I need the attributes of the class "Author" to be nested so that I can make nested queries as mentioned in the following document

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#_using_literal_nested_literal_fields_for_arrays_of_objects

How can I use the Java API of elasticsearch and still able to create a mapping so that I can make nested queries ?

Thanks
Pritom


(Thiago Souza) #2

There is no auto-detection for nested type. You need to define the mapping with the proper nested type prior to indexing.


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.