3

I have got a working query for ElasticSearch, but I have problems to execute the same query with the Java API of ElasticSearch.

How can I express the query below with the Java API of ElasticSearch?

http://localhost:9200/mongoindex/files/_search?q=anyword&fields=file.file
1
  • Which version of ES client you use and which version of ES Java API ? Commented Mar 22, 2014 at 21:12

2 Answers 2

8

this type of query uses a query_string query. The java code should look like this:

SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client);
searchRequestBuilder.setIndices("mongoindex");
searchRequestBuilder.setTypes("files");
QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder("anyword");
queryStringQueryBuilder.field("file.file");
searchRequestBuilder.setQuery(queryStringQueryBuilder);
SearchResponse response = searchRequestBuilder.execute().actionGet();
Sign up to request clarification or add additional context in comments.

Comments

2

Thank you for your responses!

Think the following would be better)

SearchRequestBuilder searchRequestBuilder = client.prepareSearch()
            .setIndices("mongoindex")
            .setTypes("files")
            .setQuery(QueryBuilders.queryString("anyword"))
            .addField("file.file");

    SearchResponse response = searchRequestBuilder.execute().actionGet();

    System.out.println(response.toString());

Comments

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.