Skip to content

prysmex/body_builder_ruby

Repository files navigation

BodyBuilder

An elasticsearch query body builder. Easily build complex queries for elasticsearch with a simple, predictable api. Based on body builder js, try it online here!

Installation

Add this line to your application's Gemfile:

gem 'body_builder'

And then execute:

bundle

Or install it yourself as:

gem install body_builder

TODOs

  • support aggregations
  • support suggestions

Basic Usage

Lets create our first query by using the BodyBuilder::Builder class

builder = BodyBuilder::Builder.new
builder.filter('terms', 'tags', ['Emerging'])
builder.build
# => {
#   "query": {
#     "bool": {
#       "filter": {
#         "terms": {
#           "tags": [
#             "Emerging"
#           ]
#         }
#       }
#     }
#   }
# }

That was easy, let's do something more interesting

builder.filter('terms', 'state', ['done'])
builder.query('match', 'name', 'John')
builder.build
# => {
#   "query": {
#     "bool": {
#       "filter": {
#         "bool": {
#           "must": [
#             {
#               "terms": {
#                 "tags": [
#                   "Emerging"
#                 ]
#               }
#             },
#             {
#               "terms": {
#                 "state": [
#                   "done"
#                 ]
#               }
#             }
#           ]
#         }
#       },
#       "must": {
#         "match": {
#           "name": "John"
#         }
#       }
#     }
#   }
# }

Most methods return the Builder instance to allow easy chaining. Lets redo the previous example.

BodyBuilder::Builder.new
    .filter('terms', 'tags', ['Emerging'])
    .filter('terms', 'state', ['done'])
    .query('match', 'name', 'John')
    .build

When using the BodyBuilder::Builder class contains multiple methods that help create powerful queries. They are mainly divided into 2 categories:

  • filter context

    • filter (alias: and_filter)
    • or_filter
    • not_filter
    • set_filter_minimum_should_match
  • query context

    • query (alias: and_query)
    • or_query
    • not_query
    • set_query_minimum_should_match

You can combine the previous methods all you want to create complex queries in just a few lines of code. For more examples, refer to the specs.

From / Size

use set_size and set_from for pagination

BodyBuilder::Builder.new
  .query('match_all')
  .set_size(25)
  .set_from(10)
  .build
# => {
#   "query": {
#     "match_all": {}
#   },
#   "size": 25,
#   "from": 10
# }

Sorting

BodyBuilder::Builder.new
  .query('match_all')
  .sort_field('id', 'desc')
  .sort_field('updated_at', 'desc')
  .build
# {
#   "query": {
#     "match_all": {}
#   },
#   "sort": [
#     {
#       "id": "desc"
#     },
#     {
#       "updated_at": "desc"
#     }
#   ]
# }

Raw option

BodyBuilder::Builder.new
  .query('match_all')
  .raw_option('source', ['id'])
  .build
# {
#   "query": {
#     "match_all": {}
#   },
#   "source": [
#     "id"
#   ]
# }

Other methods

  • queries?
  • filters?

Reset

This methods allow to remove previously added data to a Builder instance:

  • reset!
  • reset_queries!
  • reset_filters!
  • reset_raw_options!
  • reset_sort_fields!

About

Create elasticsearch easily! based on https://bodybuilder.js.org/

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published