0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Build a query hash by a simple query string
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.7
~> 10.0
>= 0
 Project Readme

EsQueryBuilder

Build Status Code Climate Coverage Status Dependency Status

A query builder for Elasticsearch in Ruby.

Usage

gem 'es-query-builder'
builder = EsQueryBuilder.new(
  # Fields allowed searching with match query.
  query_fields: ['field1'],
  # Fields for filtering. Queries for these fields do not affect search score.
  filter_fields: ['field2']
)

query = builder.build(query_string_given_by_user)

body = 
  if query.nil?
    # Empty query
    { size: 0 }
  else
    # Add other conditions, such as sort, highlight, fields and so on.
    {
      query: query,
      sort: { ... }
    }
  end

client = Elasticsearch::Client.new(host: 'http://server:9200')
client.search({
  index: 'index_name',
  type: 'type_name',
  body: body
})
# => #<Hash>

Description

EsQueryBuilder converts a query string into a corresponding hash object for elasticsearch-ruby.

Elasticsearch supports query_string query dsl which is very useful to use internally, but too powerful to use as public interface. Allowing anonymous users to use the dsl may cause not only performance problems but also security risks if your index includes secret types.

This gem accepts the query_string-query-dsl-like string and converts the string into a query object using other query dsls. At the same time it sanitizes fields in the query.