Zatvobor/tirexs

Merge/Extend query

radzserg opened this issue · 6 comments

Sorry for question - but I didn't find the better way where to ask it? What is the recommended way to dynamically build queries. For example I have a list of filter parameters before I build the query

if filter_params.first_name do 
  query = search [index: "my-index"] do
       query do
         match "first_name", filter_params.first_name
       end
  end
end

if filter_params.last_name do 
  # At this point I want to inject    match "last_name", filter_params.last_name
  # to basic query 
end

This is very simple example in real life queries can be much more complex with different nesting level.

Thanks.

Hi.
did you try like this?

query = search [index: "my-index"] do
  if filter_params.last_name do 
    query do
         match "first_name", filter_params.first_name
       end
  end
end

Trying... and getting this error


iex(2)> query = search [index: "my-index"] do
...(2)> if 1 == 1 do
...(2)> query do match "first_name", "John"  
...(2)> end
...(2)> end
...(2)> end
** (CaseClauseError) no case clause matching: {:if, [line: 3], [{:==, [line: 3], [1, 1]}, [do: {:query, [line: 4], [[do: {:match, [line: 4], ["first_name", "John"]}]]}]]}
    lib/tirexs/search.ex:48: Tirexs.Search.transpose/1
    lib/tirexs/search.ex:28: Tirexs.Search.extract/2
    expanding macro: Tirexs.Search.search/2
    iex:2: (file)

@radzserg don't use dsl, use just elixir ;)

@OpakAlex :) thanks for advice.

I'm still a new guy in elixir world. Do it for fan. I'm just curious - is no one faced a similar problem? I guess it's very common. I'm fine to deal with raw elixir. But still need to create some kind or deep merge for queries.


query = %{}
if filter.param_1 do 
   query = Module.deep_merge(query, %{ new condition } ... 
end

Did you have a chance to deal with such case ? Or it's good time to train my elixir skills and oo it ;) ?

@radzserg i use always filters with and. (забей на этот ебучий dsl)