<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="22457" data-post-id="22457">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="matt212" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/120/3957_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  matt212
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I did earlier ,</p>
<pre><code>user_param = [%{"age" =&gt; 19}, %{"name" =&gt; "matt213"}, %{"sex" =&gt; "male"}]
  user_param
  |&gt; Enum.map(&amp;Enum.to_list/1)
  |&gt; Enum.reduce(User, fn thing, query -&gt;
IO.inspect thing 
  end)
</code></pre>
<p>output:-</p>
<pre><code>[{"name", "matt213"}]
[{"sex", "male"}]

user_params = [%{"age" =&gt; 19,"name" =&gt; "matt213"}]

 user_params
  |&gt; Enum.map(&amp;Enum.to_list/1)
  |&gt; Enum.reduce(User, fn thing, query -&gt;
IO.inspect thing
  end)
</code></pre>
<p>output<br>
<code>[{"age", 19}, {"name", "matt213"}]</code></p>
<p>hence i updated my codebase by putting for each loop</p>
<pre><code>for {key, value} &lt;- thing do

    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query, where: like(field(q, ^field), ^search)

end
</code></pre>
<p><code>Enum.map(&amp;Enum.to_list/1)</code><br>
and reduce in any language require some sort of array to loop that is why it is there and mapping is for conversion array like structure for key value pairs</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="22457" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-22457" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="22457"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #25"></div>
  </section>
</div>
    <div class="postbit" id="22459" data-post-id="22459">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You’re skipping the most important step here.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(1)&gt; %{"age" =&gt; 19,"name" =&gt; "matt213"} |&gt; Enum.map(fn x -&gt; IO.inspect(x) end)
{"age", 19}
{"name", "matt213"}
[{"age", 19}, {"name", "matt213"}]
</code></pre>
<p>You can Enum.map directly over a map, and then each item is a key value pair. This is true with a reduce as well.</p>
<p>Doing a for loop inside reduce won’t help by the way, you end up just returning a list of queries instead of reducing into a single query.</p>
<p>To bring this all to a close the easiest way to do this is:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">user_params = %{"age" =&gt; 19,"name" =&gt; "matt213"}

query =
  user_params
  |&gt; Enum.reduce(User, fn {key, value}, query -&gt;
    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query, where: like(field(q, ^field), ^search)
  end)

users = Repo.all(query)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="22459" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-22459" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="22459"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #26"></div>
  </section>
</div>
    <div class="postbit" id="22461" data-post-id="22461">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="matt212" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/120/3957_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  matt212
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="matt212" data-post="26" data-topic="1219">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/48/3957_2.png" class="avatar"> matt212:</div>
<blockquote>
<p>user_params = [%{“age” =&gt; 19,“name” =&gt; “matt213”}]</p>
</blockquote>
</aside>
<p>my userparams is<br>
<code>user_params = [%{"age" =&gt; 19,"name" =&gt; "matt213"}]</code><br>
not like this<br>
<code>user_params = %{"age" =&gt; 19,"name" =&gt; "matt213"}</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="22461" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-22461" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="22461"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #27"></div>
  </section>
</div>
    <div class="postbit" id="22462" data-post-id="22462">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="matt212" data-post="22" data-topic="1219">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/48/3957_2.png" class="avatar"> matt212:</div>
<blockquote>
<p>Affirmative , now it seems quite clear  the functional usage of repo, thanks !one more query what  modification is required in code if the incoming user_params look like this</p>
<p>user_params =  %{“age” =&gt; 19, “fullname” =&gt; “matt213”, “sex” =&gt; “male”}</p>
</blockquote>
</aside>
<p>This is what got me working on just a map. Why is there a wrapping list?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="22462" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-22462" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="22462"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #28"></div>
  </section>
</div>
    <div class="postbit" id="22468" data-post-id="22468">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="matt212" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/120/3957_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  matt212
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>my bad from start</p>
<p>before  i asked for query,I had hardcoded the values for earlier payload method hence i was getting error in my earlier version of map i tried,</p>
<pre><code> user_params
  |&gt; Enum.reduce(User, fn {k,x}, query -&gt;
IO.inspect(k)
IO.inspect(x)
  end)
</code></pre>
<p>but it was throwing error since i forgot to remove <code>[]</code> from earlier payload<br>
legacy payload<br>
<code>[%{"age" =&gt; 19}, %{"name" =&gt; "matt213"}, %{"sex" =&gt; "male"}]</code><br>
updated  harcoded payload for evaluation purpose<br>
<code>user_params = [%{"age" =&gt; 19,"name" =&gt; "matt213"}]</code><br>
actual payload<br>
<code>user_params = %{"age" =&gt; 19,"name" =&gt; "matt213"}</code></p>
<p>hence, by which you have suggested by that time i forgot to remove <code>[]</code> from harcoded payload params</p>
<p>Really apologize for inconvenience in context with so much time and intellectual programming power put into ! <img src="https://forum.elixirforum.com/uploads/default/original/3X/4/f/4f18d8c5fb91348b81287d77f3409347569e5e3c.png?v=15" title=":101:" class="emoji emoji-custom" alt=":101:" loading="lazy" width="20" height="20">  would be really carefull moving forword !</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="22468" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-22468" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="22468"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #29"></div>
  </section>
</div>
    <div class="postbit" id="22552" data-post-id="22552">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Glad we could get things sorted out <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="22552" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-22552" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="22552"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #30"></div>
  </section>
</div>
    <div class="postbit" id="23174" data-post-id="23174">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="matt212" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/120/3957_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  matt212
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi ,<br>
what modification is required to put date range in below code base</p>
<pre><code>payload=%{"payload" =&gt; %{"PageNum" =&gt; 1, "PageSize" =&gt; 5,
 "daterange" =&gt; %{"enddate" =&gt; "2017-02-10", "startdate" =&gt; "2017-02-02"},
 "searchparam" =&gt; %{"fullname" =&gt; "poli"}}}

searchparam=payload["searchparam"]
pageSize=payload["PageSize"]
pageNum=payload["PageNum"]
daterange=payload["daterange"]
startdate=daterange["startdate"]
enddate=daterange["enddate"]
next_offset = pageSize*pageNum;


query =
  searchparam
  |&gt; Enum.reduce(User, fn {key, value}, query -&gt;
    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query,where: like(field(q, ^field), ^search),offset: ^next_offset,limit: ^pageSize
  end)
IO.inspect query
users = Repo.all(query)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="23174" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-23174" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="23174"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="23290" data-post-id="23290">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="matt212" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/matt212/120/3957_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  matt212
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi,</p>
<p>I resolved my query by adding below code snippets</p>
<blockquote>
<pre><code>{:ok, formattedstartdate}=Timex.parse(startdate, "%Y-%m-%d", :strftime)
{:ok, formattedenddate}=Timex.parse(enddate, "%Y-%m-%d", :strftime)
</code></pre>
</blockquote>
<pre><code>stardust=from q in query,where: q.inserted_at &gt;=^formattedstartdate,
where: q.inserted_at &lt;=^formattedenddate,offset: ^next_offset,limit: ^pageSize
users = Repo.all(stardust)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="23290" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/map-filter-and-reduce/1219/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-23290" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="23290"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>