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.

2
  • 1
    This answer might help: stackoverflow.com/a/37042003/4604579 Commented May 13, 2016 at 3: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: elastic.co/guide/en/elasticsearch/client/java-api/1.6/…, but it doesn't show how exactly to use wildcard in java Commented May 13, 2016 at 16:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.