No commit activity in last 3 years
No release in over 3 years
Convert user queries like `(London OR Paris) AND 'John Wick'` into elasticsearch JSON queries
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.9

Runtime

~> 2.0
 Project Readme

Elasticsearch query parser

Build Status Gem Version

  • I can search “London” and it will return everyone with “London”
  • And then a complex example could be a boolean search such as: ((('London' OR 'Paris') OR 'Madrid') AND 'Venture Capital') NOT VC

Hot to install

Add this line to your application's Gemfile:

gem "elasticsearch_query_parser"

And then execute:

$ bundle

How to use

require "elasticsearch_query_parser"

ElasticsearchQueryParser.parse_query("London")
# => { query: { bool: { should: [{ match: { text: { query: "London", operator: "and" } } }] } } }

ElasticsearchQueryParser.parse_query("London Madrid")
=begin
{
  query: {
    bool: {
      should: [
        { match: { text: { query: "London", operator: "and" } } },
        { match: { text: { query: "Madrid", operator: "and" } } }
      ]
    }
  }
}
=end

ElasticsearchQueryParser.parse_query("London OR Madrid")
=begin
{
  query: {
    bool: {
      should: [
        { match: { text: { query: "London", operator: "and" } } },
        { match: { text: { query: "Madrid", operator: "and" } } }
      ]
    }
  }
}
=end

ElasticsearchQueryParser.parse_query("(London OR Madrid) AND Paris")
=begin
{
  query: {
    bool: {
      must: [
        { match: { text: { query: "Paris", operator: "and" } } },
        {
          bool: {
            should:[
              { match: { text: { query: "Madrid", operator: "and" } } },
              { match: { text: { query: "London", operator:"and" } } }
            ]
          }
        }
      ]
    }
  }
}
=end

ElasticsearchQueryParser.parse_query("(((London OR Paris) OR Madrid) AND 'Venture Capital') NOT VC")
=begin
{
  query: {
    bool: {
      must: [
        { match: { text: { query: "Venture Capital", operator: "and" } } },
        {
          bool: {
            should: [
              { match: { text: { query: "Madrid", operator: "and" } } },
              {
                bool: {
                  should: [
                    { match: { text: { query: "Paris", operator: "and" }}},
                    { match: { text: { query: "London", operator: "and" } } }
                  ]
                }
              }
            ]
          }
        }
      ],
      must_not:[{ match: { text: { query: "VC", operator: "and" } } }]
    }
  }
}
=end

ElasticsearchQueryParser.parse_query("Paris AND (London OR Madrid)")
=begin
{
  query:{
    bool: {
      must:[
        { match: { text: { query: "Paris", operator: "and" } } },
        {
          bool: {
            should:[
              { match: { text: { query: "London", operator: "and" } } },
              { match: { text: { query: "Madrid", operator: "and" } } }
            ]
          }
        }
      ]
    }
  }
}
=end

ElasticsearchQueryParser.parse_query("VC AND ('Venture Capital' AND (Madrid OR (Paris AND London)))")
=begin
{
  query: {
    bool: {
      must:[
        { match: { text: { query: "VC", operator: "and" } } },
        {
          bool: {
            must: [
              { match: { text: { query: "Venture Capital", operator: "and" } } },
              {
                bool: {
                  should:[
                    { match: { text: { query: "Madrid", operator: "and" } } },
                    {
                      bool: {
                        must: [
                          { match: { text: { query: "Paris", operator: "and" } } },
                          { match: { text: { query: "London", operator: "and" } } }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
=end

ElasticsearchQueryParser.parse_query("((London OR Madrid) NOT VC) NOT 'Venture Capital'")
=begin
{
  query: {
    bool: {
      should: [
        { match: { text: { query: "Madrid", operator: "and" } } },
        { match: { text: { query: "London", operator: "and" } } }
      ],
      must_not: [
        { match: { text: { query: "VC", operator: "and" } } },
        { match: { text: { query: "Venture Capital", operator: "and" } } }
      ]
    }
  }
}
=end

How to change Elastic field name

By default #parse_query generates elastic query with text attribute. You can customize field name via config:

ElasticsearchQueryParser.configure do |config|
  # elastic_field_name can accepts symbol or string
  config.elastic_field_name = "title"
end

ElasticsearchQueryParser.parse_query("London")
# => { query: { bool: { should: [{ match: { "title": { query: "London", operator: "and" } } }] } } }