Skip to content

Commit f96eae9

Browse files
committed
Added elastic search queries through elasticsearch json
1 parent 49c74b5 commit f96eae9

4 files changed

+41
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-module(controller_admin_elasticsearch_edit).
2+
-author("Driebit <[email protected]>").
3+
4+
%% API
5+
-export([
6+
event/2
7+
]).
8+
9+
-include("zotonic.hrl").
10+
11+
%% Previewing the results of a elastic query in the admin edit
12+
event(#postback{message={elastic_query_preview, Opts}}, Context) ->
13+
DivId = proplists:get_value(div_id, Opts),
14+
Index = proplists:get_value(index, Opts),
15+
Id = proplists:get_value(rsc_id, Opts),
16+
QueryType = list_to_atom(proplists:get_value(query_type, Opts)),
17+
18+
Q = z_convert:to_binary(z_context:get_q("triggervalue", Context)),
19+
20+
case jsx:is_json(Q) of
21+
false ->
22+
z_render:growl_error("There is an error in your query", Context);
23+
true ->
24+
S = z_search:search({QueryType, [{elastic_query, Q}, {index, Index}]}, Context),
25+
{Html, Context1} = z_template:render_to_iolist({cat, "_admin_query_preview.tpl"}, [{result,S}, {id, Id}], Context),
26+
z_render:update(DivId, Html, Context1)
27+
end.

support/elasticsearch_search.erl

+5-3
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,16 @@ map_query({text, Text}, Context) ->
136136
{query, Text},
137137
{fields, z_notifier:foldr(#elasticsearch_fields{query = Text}, DefaultFields, Context)}
138138
]}]};
139-
map_query({query_id, Id}, Context) ->
140-
ElasticQuery = z_html:unescape(m_rsc:p(Id, <<"elastic_query">>, Context)),
139+
map_query({elastic_query, ElasticQuery}, _Context) ->
141140
case jsx:is_json(ElasticQuery) of
142141
true ->
143142
{true, jsx:decode(ElasticQuery)};
144143
false ->
145144
false
146145
end;
146+
map_query({query_id, Id}, Context) ->
147+
ElasticQuery = z_html:unescape(m_rsc:p(Id, <<"elastic_query">>, Context)),
148+
map_query({elastic_query, ElasticQuery}, Context);
147149
map_query({match_objects, Id}, Context) ->
148150
%% Look up all outgoing edges of this resource
149151
Clauses = lists:map(
@@ -279,7 +281,7 @@ map_must({content_group, Id}, Context) ->
279281
%% TODO Add support for other filter types
280282
%% http://docs.zotonic.com/en/latest/developer-guide/search.html#filter
281283
%% Use regular fields where Zotonic uses pivot_ fields
282-
%% @see z_pivot_rsc:pivot_resource/2
284+
%% @see z_pivot_rsc:pivot_resourcesource/2
283285
map_must({filter, [[Key | _] | _] = Filters}, Context) when is_list(Key); is_binary(Key); is_atom(Key) ->
284286
%% Multiple filters: OR
285287
{true, #{<<"bool">> => #{<<"should">> =>

templates/_admin_edit_content.elastic_query.tpl

+8-16
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,29 @@
1313
{% with m.rsc[id] as r %}
1414
<fieldset>
1515
<p class="notification notice">
16-
{_ Here you can edit the arguments of the elasticsearch query. _}
16+
{_ Here you can edit your elasticsearch query. _}
1717
</p>
1818

1919
<div class="form-group">
2020
<label class="control-label" for="elastic_query">{_ Elasticsearch query _}</label>
2121
<div>
22-
{% with "cat='text'" as placeholder %}
22+
{% with "[]" as placeholder %}
2323
<textarea class="form-control" id="{{ #elastic_query }}" name="elastic_query" rows="15" placeholder="{{ placeholder }}">{{ r.elastic_query }}</textarea>
2424
{% endwith %}
25-
{# {% wire id=#elastic_query type="change" postback={query_preview rsc_id=id div_id=#querypreview target_id=#query} delegate="controller_admin_edit" %} #}
25+
{% wire id=#elastic_query type="change" postback={elastic_query_preview query_type="query" rsc_id=id div_id=#elastic_query_preview target_id=#elastic_query index=m.config.mod_elasticsearch.index.value} delegate="controller_admin_elasticsearch_edit" %}
2626
</div>
2727
</div>
28-
{#
29-
<div class="form-group">
30-
<a id="{{ #test_query }}" class="btn btn-default">{_ Test query _}</a>
31-
{% wire id=#test_query type="click" action={script script="$('#query').trigger('change')"} %}
32-
</div>
3328

3429
<div class="form-group">
35-
<div class="checkbox"><label>
36-
<input type="checkbox" id="is_query_live" name="is_query_live" {% if r.is_query_live %}checked{% endif %}/>
37-
{_ Live query, send notifications when matching items are updated or inserted. _}
38-
</label></div>
30+
<a id="{{ #test_elastic_query }}" class="btn btn-default">{_ Test query _}</a>
31+
{% wire id=#test_elastic_query type="click" action={script script="$('#elastic_query').trigger('change')"} %}
3932
</div>
4033

4134
<h4>{_ Query preview _}</h4>
4235

43-
<div class="query-results" id="{{ #querypreview }}">
44-
{% include "_admin_query_preview.tpl" result=m.search[{query query_id=id pagelen=20}] %}
45-
</div>
46-
#}
36+
<div class="elastic-query-results" id="{{ #elastic_query_preview }}">
37+
{% catinclude "_admin_query_preview.tpl" id result=m.search[{query query_id=id id=id index=m.config.mod_elasticsearch.index.value pagelen=20}] %}
38+
</div>
4739
</fieldset>
4840
{% endwith %}
4941
{% endblock %}

templates/_admin_edit_main_parts.elastic_query.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{% catinclude "_admin_edit_basics.tpl" id is_editable=is_editable languages=languages show_header %}
66

7-
{% include "_admin_edit_content.elastic_query.tpl" id=id is_editable=is_editable languages=languages %}
7+
{% catinclude "_admin_edit_content.tpl" id is_editable=is_editable languages=languages %}
88

99
{% if id.category_id.feature_show_address|if_undefined:`true` %}
1010
{% catinclude "_admin_edit_content_address.tpl" id is_editable=is_editable languages=languages %}

0 commit comments

Comments
 (0)