How to build dsl query in spring-data-elasticsearch 5 (ElasticsearchOperations , CriteriaQuery)

Im trying to write some query using spring data elasticsearch 5

and this is my environment

  1. elasticsearch 8
  2. spring boot 3
  3. spring-data-elasticsearch 5

and i can not make query like this so PLEASE help me with building dsl query using spring data elasticseaerch

[WHAT I WANT ]

POST someIndex/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {"A": "the challenge city"}},
        {"match": {"B": "the challenge city"}},
        {"match": {"C": "the challenge city"}}
      ],
      "must": [
        {"match": {"C": "City"}}
      ]
    }
  }

im trying to build this should query using Criteria in spring data ,
But it is not goes well.


What is did ( wrong) is here

private CriteriaQuery searchDsl(BoardEntrySearchParams params) {
		CriteriaQuery query = new CriteriaQuery(new Criteria());

		if (params == null)
			return query;

		if (params.searchWord() != null) {
			query.addCriteria(Criteria.or().where("title").contains(params.searchWord()));
			query.addCriteria(Criteria.or().where("content").contains(params.searchWord()));
			query.addCriteria(Criteria.or().where("tags").contains(params.searchWord()));
		}

		if (params.author() != null)
			query.addCriteria(Criteria.and().where("authorId").is(params.author()));

		if (params.tags() != null && params.tags().size() != 0) {
			StringBuilder tagParam = new StringBuilder();
			for (String elem : params.tags())
				tagParam.append(elem);
			query.addCriteria(Criteria.and().where("tags").contains(tagParam.toString()));
		}

		return query;
	}

.....

// when i call it 

	var hitResult = elasticsearchOperations.search(searchDsl(params), BoardEntryDocument.class);


But after i see trace log, it look likes

{
  "from": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "analyze_wildcard": true,
            "fields": [
              "title"
            ],
            "query": "*java*"
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "fields": [
              "content"
            ],
            "query": "*java*"
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "fields": [
              "tags"
            ],
            "query": "*java*"
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "fields": [
              "tags"
            ],
            "query": "*java*"
          }
        }
      ]
    }
  },
  "size": 10000,
  "track_scores": false,
  "version": true
}

So Please Help me
let me know how to make query of combination of should and must like below

Have a Good Day though

POST someIndex/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {"A": "the challenge city"}},
        {"match": {"B": "the challenge city"}},
        {"match": {"C": "the challenge city"}}
      ],
      "must": [
        {"match": {"C": "City"}}
      ]
    }
  }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.