0

I am trying to implement below elasticsearch query

{
"query": {
    "bool": {
        "must": [
            {"match": {"collegeName": "XXXX"}},
            {"match": {"userType": "STUDENT"}},
            {"match": {"details.blocklisted":"TRUE"}}
            ]
    }
}

}

How do I implement it in java. I thought of doing this

BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    searchSourceBuilder.query(boolQueryBuilder.must(new 
MatchQueryBuilder("collegeName",loggedInUserCollegeName)).must(new MatchQueryBuilder("userType",studentsSearchRequestDTO.getUserType())).must(new MatchQueryBuilder("details.blocklisted",studentsSearchRequestDTO.isBlockListed())));

Is there a better way to do???? Help!!

2 Answers 2

2

you can use MultiMatchQueryBuilder something like

SearchQuery searchQuery = new NativeSearchQueryBuilder()
              .withQuery(multiMatchQuery("college")
                .field("collegeName")
                .field("userType")
                .type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
              .build();
Sign up to request clarification or add additional context in comments.

1 Comment

i'm trying to use multiMatchQuery but this method is not found
0

Yes,

Use Search Template (ES official doc). It's like a stored procedure. A query with a replaceable variable. Plus changing the query is just a simple update. Create template beforehand (POST /_search/template/templateName {BODY} ) then you can call it (POST /twitter/tweet/_search/templateName {BODY containing dynamic variables}).

similarly in JAVA

Map<String, Object> params = new HashMap<String, Object>();
params.put("search_term", "elasticsearch");
params.put("since", "now-30d");

Template template = new Template("tweets", ScriptService.ScriptType.FILE, 
MustacheScriptEngineService.NAME, null, params);
SearchRequestBuilder request = 
client.prepareSearch(INDEX).setTemplate(template);

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

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.