Skip to content

Instantly share code, notes, and snippets.

@imranismail
Created June 23, 2018 17:09
Show Gist options
  • Save imranismail/6d5b57096f91d9a6400c1673562654b6 to your computer and use it in GitHub Desktop.
Save imranismail/6d5b57096f91d9a6400c1673562654b6 to your computer and use it in GitHub Desktop.
POC Elasticsearch Query Builder

POC elixir query builder

References:

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html

Activity
|> Search.new()
|> Search.query(
  bool(
    filter: ([
      term(:activity_status, :active),
      term(:any_active_outlets, true),
      term(:available, true),
      term(:company_enabled, true),
      terms(:app_name, [:fave, :all]),
      bool(should: [
        bool(must: [term(:listing_type, :time_slot), term(:has_active_class_session, true)]),
        bool(must: term(:listing_type, :open_voucher), must_not: [exists(:field, :start_date), exists(:field, :end_date),
      ])
    ])
  )
)
|> Search.query(
  bool(
    must: function_score(
      query: dis_max(
        queries: [
          multi_match(
            query: query,
            fields: @cross_fields,
            type: :cross_fields
          ),
          multi_match(
            query: query,
            fields: @best_fields,
            type: :best_fields,
            fuzziness: "AUTO"
          )
        ]
      ),
      score_mode: :sum,
      functions: [
        score_function(:field_value_factor,
          field: :last_week_purchases_count,
          modifier: :ln2p,
          factor: 1.0,
          filter: exists(:field, :last_week_purchases_count)
        ),
        filter_function(
          filter: match_all(name: "imran"),
          random_score: 1,
          weight: 23
        )
      ]
    )
  )
)
|> Search.from(0)
|> Search.size(100)
|> Search.timeout(1500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment