0

Using spring data elasticsearch I want to do the following (bulk Indexing)

Step 1

PUT surname
{
   "settings" : {
                "index" : {
                  "number_of_shards" : 1,
                  "number_of_replicas" : 0
                }
              },
            "mappings": {
                "properties": {
                  "message": {
                    "type": "text"
                  },
                  "query": {
                    "type": "percolator"
                  }
                }
              }
}

Step 2:

PUT surname/_bulk
{ "index" : { "_index" : "surname", "_type" : "_doc", "_id" : "1" } }
{ "query" : { "match_phrase" : { "message" : "Aal" } }}
{ "index" : { "_index" : "surname", "_type" : "_doc", "_id" : "2" } }
{ "query" : { "match_phrase" : { "message" : "Aalbers" } }}

Step 1 is done with the help of - https://stackoverflow.com/a/67724048/4068218

For Step 2 I tried

IndexQuery and UpdateQuery

Query query = new CriteriaQuery(new Criteria("match_phrase").subCriteria(new Criteria("message").is("Aal")));
        UpdateQuery updateQuery = builder(query).build();
        elasticsearchOperations.bulkUpdate(ImmutableList.of(updateQuery),IndexCoordinates.of(indexName.get()));

but both do not work. If I use UpdateQuery I get Validation Failed: 1: id is missing;2: script or doc is missing.

If I use IndexQuery I get malformed query.

How do I go about Step 2 in spring data elasticsearch? Any lead is much appreciated.

Hari Rao
  • 2,990
  • 3
  • 21
  • 34

1 Answers1

1

Below code worked for me. Sharing here as it might help someone

IndexQuery indexQuery = new IndexQueryBuilder()
                                        .withId("12")
                                        .withSource(String.format("{ \"query\" : { \"match_phrase\" : { \"message\" : \"%s\" } }}", "Aal"))
                                        .build();

        elasticsearchOperations.bulkIndex(ImmutableList.of(indexQuery),IndexCoordinates.of(indexName.get()));
Hari Rao
  • 2,990
  • 3
  • 21
  • 34