1

TOPIC: tool: https://github.com/danpaz/bodybuilder An elasticsearch query body builder. Easily build complex queries for elasticsearch with a simple, predictable api.

Does someone know how to make such a query using bodybuilder js?

"query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "entityType": "CATEGORY"
                }
              },
              {
                "match_phrase": {
                  "entityType": "TAG"
                }
              },
              {
                "match_phrase": {
                  "entityType": "SHOP"
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }

I tough about this

 bodybuilder()
        .orQuery('match_phrase', 'entityType', 'CATEGORY')
        .orQuery('match_phrase', 'entityType', 'TAG')
        .orQuery('match_phrase', 'entityType', 'SHOP')
        .build();

but then I get out something like this

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "entityType": "CATEGORY"
          }
        },
        {
          "match_phrase": {
            "entityType": "TAG"
          }
        },
        {
          "match_phrase": {
            "entityType": "SHOP"
          }
        }
      ]
    }
  }
}

which is not same

Kibo_007
  • 171
  • 3
  • 10

1 Answers1

0

You can create must should query by providing a callback function as a second parameter to the query OR notQuery OR orQuery.

The above query you've provided in the must query, contains only one element ( bool -> should ) So it doesn't add any value. if must contain one more element, then you can build as follows.

Lets add one more attribute validation in the must query given above, lets put the country must from India then, you can build the query as follows,

const shouldQuery = (build) => {
    return build.orQuery('match_phrase', 'entityType', 'CATEGORY')
        .orQuery('match_phrase', 'entityType', 'TAG')
        .orQuery('match_phrase', 'entityType', 'SHOP')
        .queryMinimumShouldMatch(1)


}

bodybuilder()
    .query('bool', shouldQuery)
    .query('term', 'country.keyword', 'India')
    .build();

The result would be,

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "entityType": "CATEGORY"
                }
              },
              {
                "match_phrase": {
                  "entityType": "TAG"
                }
              },
              {
                "match_phrase": {
                  "entityType": "SHOP"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "term": {
            "country.keyword": "India"
          }
        }
      ]
    }
  }
}
Shiyas
  • 570
  • 5
  • 11