Class: Elasticband::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticband/query.rb,
lib/elasticband/query/match.rb,
lib/elasticband/query/filtered.rb,
lib/elasticband/query/multi_match.rb,
lib/elasticband/query/function_score.rb,
lib/elasticband/query/score_function.rb,
lib/elasticband/query/score_function/gauss.rb,
lib/elasticband/query/score_function/filtered.rb,
lib/elasticband/query/score_function/boost_mode.rb,
lib/elasticband/query/score_function/score_mode.rb,
lib/elasticband/query/score_function/boost_factor.rb,
lib/elasticband/query/score_function/geo_location.rb,
lib/elasticband/query/score_function/script_score.rb,
lib/elasticband/query/score_function/field_value_factor.rb

Direct Known Subclasses

Filtered, FunctionScore, Match, MultiMatch

Defined Under Namespace

Classes: Filtered, FunctionScore, Match, MultiMatch, ScoreFunction

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(query_text, options = {}) ⇒ Object

Parses a query text with options to a Elasticsearch syntax. Check Elasticband::Filter.parse for filter options.

#### Options

  • ‘on:` Defines which attributes will searched in documents.

  • ‘boost_by:` Boosts the score of a query result based on a attribute of the document. This score will be multiplied for the `boost_by` attribute over function `ln2p`.

  • ‘boost_where:` Boosts the score of a query result where some condition is `true`. This score will be multiplied by 1000 (arbitrary, based on gem `searchkick`)

  • ‘boost_function:` Boosts using the function passed.

  • ‘boost_mode:` Defines how the function_score will be used.

  • ‘score_mode:` Defines how the query score will be used.

  • ‘geo_location:` Defines query by geo location.

#### Examples “‘ Query.parse(’foo’)

> { match: { _all: ‘foo’ } }

Query.parse(‘foo’, on: :name)

> { match: { name: ‘foo’ } }

Query.parse(‘foo’, on: %i(name description))

> { multi_match: { query: ‘foo’, fields: [:name, :description] } }

Query.parse(‘foo’, only: { status: :published })

> { filtered: { query: …, filter: { term: { status: :published } } } }

Query.parse(‘foo’, except: { company: { id: 1 } })

> { filtered: { query: …, filter: { not: { term: { status: :published } } } } }

Query.parse(‘foo’, includes: [‘company’, on: :description])

> { filtered: { query: …, filter: { query: { match: { description: ‘company’ } } } } }

Query.parse(‘foo’, boost_by: :contents_count)

> { function_score: { query: …, field_value_factor: { field: :contents_count, modifier: :ln2p } } }

Query.parse(‘foo’, boost_function: “_score * doc.value”)

> { function_score: { query: …, script_score: { script: ‘_score * doc.value’ } } }

Query.parse(‘foo’, boost_function: …, boost_mode: :multiply)

> { function_score: { query: …, boost_mode: :multiply, script_score: { script: … } } }

Query.parse(‘foo’, boost_function: …, score_mode: :multiply)

> { function_score: { query: …, score_mode: :multiply, script_score: { script: … } } }

Query.parse(‘foo’, boost_where: { company: { id: 1 } })

> {

  function_score: {
    query: ...,
    functions: [
      { filter: { term: { 'company.id': 1 } }, boost_factor: 1000 }
    ]
  }
}

Query.parse(

'foo',
geo_location: {
  on: :location,
  latitude: 12,
  longitude: 34,
  distance: { same_score: '5km', half_score: '10km' }
}

)

> {

function_score: {
  query: ...,
  gauss: { location: { origin: { lat: 12, lon: 34 }, offset: '5km', scale: '10km' } } } }

Query.parse(‘foo’, near: { on: :location, latitude: 12, longitude: 34, distance: ‘5km’ })

> {

  filtered: {
    query: ...,
    filter: { geo_distance: { location: { lat: 12, lon: 34 } }, distance: '5km' }
  }
}

“‘



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/elasticband/query.rb', line 93

def parse(query_text, options = {})
  query = parse_on(query_text, options[:on])
  query = parse_query_filters(query, options)
  query = parse_boost(
    query,
    options[:geo_location],
    options.slice(:boost_by, :boost_where, :boost_function),
    options[:score_mode],
    options[:boost_mode]
  )
  query.to_h
end

Instance Method Details

#to_hObject



9
10
11
# File 'lib/elasticband/query.rb', line 9

def to_h
  { match_all: {} }
end