0

I have got the data from Elasticsearch index by using the below code:

SearchRequest request = new SearchRequest("football_sum_csv").scroll(new TimeValue(60000));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//    searchSourceBuilder.query(matchQuery("multi", "test"));
searchSourceBuilder.size(10000);
searchSourceBuilder.sort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
request.source(searchSourceBuilder);
SearchResponse scrollResp = client.search(request, RequestOptions.DEFAULT);
System.out.println(scrollResp);

This will return all the data from my index of ElasticSearch.
In here, I want to filter some data based on my query. In my case, I want to manually enter query and get results from ElasticSearch.

For example:
This below query will return all the data as like above

query = {'query': {'bool': {'must': [{'match_all': {}}], 'must_not': [], 'should': []}} }

This below query will return all the data in the Country named Spain

query = {"query": {"bool": {"must": [{"match_phrase": {"countryName": "Spain"}}], "must_not": [], "should": []}}}

This below query will return all the data in the os named Windows and sessionTime between 1000 and 5000

{"query": {"bool": {"must": [{"range": {"sessionTime": {"gte": 1000, "lt": 5000}}}, {"match_phrase": {"os": "Windows"}}], "must_not": [], "should": []}}}

In the above three queries, they perform the different operations in ElasticSearch. I want to use this like queries to get my data.

If I put query inside here in above java code,

searchSourceBuilder.query({"query": {"bool": {"must": [{"match_phrase": {"countryName": "Spain"}}], "must_not": [], "should": []}}});

It gives me an error. I know Querybuilders has lots of query options such as range, match and so on. But I want to manually enter query like the listed above and get my data from ElasticSearch. I don't know how to make it. Please help me with some solutions.

4
  • If that's not a typo, I believe your query should probably be a string? Something like this: searchSourceBuilder.query("{\"query\": {\"bool\": {\"must\": [{\"match_phrase\": {\"countryName\": \"Spain\"}}], \"must_not\": [], \"should\": []}}}"); Commented Feb 13, 2020 at 11:41
  • Yes....It is a string...How can i make it. Commented Feb 13, 2020 at 12:42
  • 2
    Can you check this stackoverflow.com/questions/28906749/… Commented Feb 13, 2020 at 13:29
  • @alexgids, Thank u so much..Wrapper query helped me..... Commented Feb 14, 2020 at 6:27

1 Answer 1

1

Found the Answer from the comment of alexgids :
wrapperQuery needs to be used for passing query as json

String query = "{"bool": {"must": [{"match_phrase": {"countryName": "Spain"}}], "must_not": [], "should": []}}";
QueryBuilder qb = QueryBuilders.wrapperQuery(query);
SearchSourceBuilder searchSourceBuilder1 = new SearchSourceBuilder();
searchSourceBuilder1.query(qb);
SearchRequest searchRequest = new SearchRequest("index_name");
searchRequest.source(searchSourceBuilder1);
SearchResponse scrollResp1 = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(scrollResp1);
Sign up to request clarification or add additional context in comments.

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.