Last active
February 1, 2017 16:07
-
-
Save radzserg/fb6087e3ef2326f8d4c26ddad8b77ca3 to your computer and use it in GitHub Desktop.
An example of building search ES query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule App.EsDocument.EsAdvAccount do | |
| @behaviour App.Es.DocumentBehavior | |
| alias App.Repo | |
| alias App.AdvAccount | |
| alias App.Es.EsQuery | |
| alias App.Es.EsDocument | |
| use EsDocument | |
| def index do | |
| "adv_account" | |
| end | |
| # def build_body!(adv_account, opts \\ %{}) do | |
| defp build_query_dsl(params) do | |
| filter = [] | |
| category = EsQueryDsl.get_param(params, :category) | |
| filter = if category do | |
| filter ++ [%{"term": %{"adv_category_id": category}}] | |
| else | |
| filter | |
| end | |
| hash_tag_ids = EsQueryDsl.get_param(params, :hash_tag_ids) | |
| filter = if hash_tag_ids do | |
| filter ++ [%{"terms": %{"hash_tag_ids": hash_tag_ids}}] | |
| else | |
| filter | |
| end | |
| query_dsl = %EsQueryDsl{ | |
| query: %{ | |
| bool: %{ | |
| filter: filter | |
| } | |
| } | |
| } | |
| query_dsl = if Map.get(params, :autocomplete) do | |
| %{query_dsl | _source: ["id", "name"]} | |
| else | |
| query_dsl | |
| end | |
| query_dsl | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment