2

I want to update some of my indexed data with a particular field using Java Api. I have referred this document https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update.html and created a java program. My Java program looks like this:

public class App {

  public static void main(String[] args) throws Exception {
    Client client = TransportClient.builder().build().addTransportAddress(
        new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),
            9300));
UpdateRequest updateRequest = new UpdateRequest("pibindex", "SearchTech",
        "http://www.sea.com/bundle")
            .script(new Script("ctx._source.default_collection = \"true\""));
    client.update(updateRequest).get();
   }
}

This program is updating a single document based on a script. "pibindex" is the name of my index, "SearchTech" is the type of my index and "http://www.sea.com/bundle" is the id of my document. I would like to update multiple documents with a new field "wiki_collection":true which matches a pattern like "http://www.sea.com/bundle". I want to use wildcard query.

I am trying to implement this elasticsearch query in Java:

POST /pibindex2/_update_by_query
{
  "script": {
    "inline": "ctx._source.wiki_collection=true"
  },
      "query": {
        "wildcard": {
          "url": {
            "value": "http://www.sea.com/bundle/*"
          }
        }
      }
}

Sorry if I’m missing something obvious. Thank you.

Rose
  • 1,490
  • 5
  • 25
  • 56
  • 1
    This answer might help: http://stackoverflow.com/a/37042003/4604579 – Val May 13 '16 at 03:59
  • @Val I referred your answer, now I am able to update multiple documents by using "termquery". But I have to use "wildcard query" to update multiple documents which matches a pattern. I have referred this link: https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.6/wildcard.html, but it doesn't show how exactly to use wildcard in java – Rose May 13 '16 at 16:47

1 Answers1

3

You can try this way:

import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery;

BulkIndexByScrollResponse r = ubqrb.source("twitter")
    .script(script)
    .filter(wilcardQuery("url", "http://www.sea.com/bundle/*"))
    .get();

Although the result will depend on whether your url field is analyzed or not.

Val
  • 207,596
  • 13
  • 358
  • 360
  • My "url" field is not analyzed, but your code is working. Thanks a lot. I was stuck on this thing from last two days and the official document was not that helpful. Thanks again. – Rose May 13 '16 at 17:02