Socket
Book a DemoInstallSign in
Socket

elasticsearch_query_parser

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elasticsearch_query_parser

0.0.4
bundlerRubygems
Version published
Maintainers
1
Created
Source

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" } } }] } } }

FAQs

Package last updated on 25 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.