summaryrefslogtreecommitdiff
path: root/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/TemplateQueryBuilderTests.java
blob: 41242a693dda3c9a7a76dfa1bb011124703d0b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.script.mustache;

import org.apache.lucene.index.memory.MemoryIndex;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptPlugin;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.junit.After;
import org.junit.Before;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class TemplateQueryBuilderTests extends AbstractQueryTestCase<TemplateQueryBuilder> {

    /**
     * The query type all template tests will be based on.
     */
    private QueryBuilder templateBase;

    /**
     * All tests create deprecation warnings when an new {@link TemplateQueryBuilder} is created.
     * Instead of having to check them once in every single test, this is done here after each test is run
     */
    @After void checkWarningHeaders() throws IOException {
        checkWarningHeaders("[template] query is deprecated, use search template api instead");
    }

    @Override
    protected Collection<Class<? extends Plugin>> getPlugins() {
        return Arrays.asList(MustachePlugin.class, CustomScriptPlugin.class);
    }

    public static class CustomScriptPlugin extends MockScriptPlugin {

        @Override
        @SuppressWarnings("unchecked")
        protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
            Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();

            scripts.put("{ \"match_all\" : {}}",
                    s -> new BytesArray("{ \"match_all\" : {}}"));

            scripts.put("{ \"match_all\" : {\"_name\" : \"foobar\"}}",
                    s -> new BytesArray("{ \"match_all\" : {\"_name\" : \"foobar\"}}"));

            scripts.put("{\n" +
                    "  \"term\" : {\n" +
                    "    \"foo\" : {\n" +
                    "      \"value\" : \"bar\",\n" +
                    "      \"boost\" : 2.0\n" +
                    "    }\n" +
                    "  }\n" +
                    "}", s -> new BytesArray("{\n" +
                    "  \"term\" : {\n" +
                    "    \"foo\" : {\n" +
                    "      \"value\" : \"bar\",\n" +
                    "      \"boost\" : 2.0\n" +
                    "    }\n" +
                    "  }\n" +
                    "}"));
            return scripts;
        }
    }

    @Before
    public void before() {
        templateBase = new MatchQueryBuilder("field", "some values");
    }

    @Override
    protected boolean supportsBoostAndQueryName() {
        return false;
    }

    @Override
    protected TemplateQueryBuilder doCreateTestQueryBuilder() {
        return new TemplateQueryBuilder(new Script(templateBase.toString(), ScriptType.INLINE, "mustache", null, null));
    }

    @Override
    protected void doAssertLuceneQuery(TemplateQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
        QueryShardContext queryShardContext = context.getQueryShardContext();
        assertEquals(rewrite(QueryBuilder.rewriteQuery(templateBase, queryShardContext).toQuery(queryShardContext)), rewrite(query));
    }

    public void testIllegalArgument() {
        expectThrows(IllegalArgumentException.class, () -> new TemplateQueryBuilder((Script) null));
    }

    /**
     * Override superclass test since template query doesn't support boost and queryName, so
     * we need to mutate other existing field in the test query.
     */
    @Override
    public void testUnknownField() throws IOException {
        TemplateQueryBuilder testQuery = createTestQueryBuilder();
        String testQueryAsString = toXContent(testQuery, randomFrom(XContentType.JSON, XContentType.YAML)).string();
        String queryAsString = testQueryAsString.replace("inline", "bogusField");
        try {
            parseQuery(queryAsString);
            fail("ScriptParseException expected.");
        } catch (ElasticsearchParseException e) {
            assertTrue(e.getMessage().contains("bogusField"));
        }
    }

    public void testJSONGeneration() throws IOException {
        Map<String, Object> vars = new HashMap<>();
        vars.put("template", "filled");
        TemplateQueryBuilder builder = new TemplateQueryBuilder("I am a $template string", ScriptType.INLINE, vars);
        XContentBuilder content = XContentFactory.jsonBuilder();
        content.startObject();
        builder.doXContent(content, null);
        content.endObject();
        content.close();
        assertEquals("{\"template\":{\"inline\":\"I am a $template string\",\"lang\":\"mustache\",\"params\":{\"template\":\"filled\"}}}",
                content.string());
    }

    public void testRawEscapedTemplate() throws IOException {
        String expectedTemplateString = "{\"match_{{template}}\": {}}\"";
        String query = "{\"template\": {\"inline\": \"{\\\"match_{{template}}\\\": {}}\\\"\",\"params\" : {\"template\" : \"all\"}}}";
        Map<String, Object> params = new HashMap<>();
        params.put("template", "all");
        QueryBuilder expectedBuilder = new TemplateQueryBuilder(expectedTemplateString, ScriptType.INLINE, params);
        assertParsedQuery(query, expectedBuilder);
    }

    public void testRawTemplate() throws IOException {
        String expectedTemplateString = "{\"match_{{template}}\":{}}";
        String query = "{\"template\": {\"inline\": {\"match_{{template}}\": {}},\"params\" : {\"template\" : \"all\"}}}";
        Map<String, Object> params = new HashMap<>();
        params.put("template", "all");
        QueryBuilder expectedBuilder = new TemplateQueryBuilder(expectedTemplateString, ScriptType.INLINE, params, XContentType.JSON);
        assertParsedQuery(query, expectedBuilder);
    }

    @Override
    public void testMustRewrite() throws IOException {
        String query = "{ \"match_all\" : {}}";
        QueryBuilder builder = new TemplateQueryBuilder(new Script(query, ScriptType.INLINE, "mockscript",
        Collections.emptyMap(), XContentType.JSON));
        try {
            builder.toQuery(createShardContext());
            fail();
        } catch (UnsupportedOperationException ex) {
            assertEquals("this query must be rewritten first", ex.getMessage());
        }
        assertEquals(new MatchAllQueryBuilder(), builder.rewrite(createShardContext()));
    }

    public void testRewriteWithInnerName() throws IOException {
        final String query = "{ \"match_all\" : {\"_name\" : \"foobar\"}}";
        QueryBuilder builder = new TemplateQueryBuilder(new Script(query, ScriptType.INLINE, "mockscript",
             Collections.emptyMap(), XContentType.JSON));
        assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(createShardContext()));

        builder = new TemplateQueryBuilder(new Script(query, ScriptType.INLINE, "mockscript",
            Collections.emptyMap(), XContentType.JSON)).queryName("outer");
        assertEquals(new BoolQueryBuilder().must(new MatchAllQueryBuilder().queryName("foobar")).queryName("outer"),
            builder.rewrite(createShardContext()));
    }

    public void testRewriteWithInnerBoost() throws IOException {
        final TermQueryBuilder query = new TermQueryBuilder("foo", "bar").boost(2);
        QueryBuilder builder = new TemplateQueryBuilder(new Script(query.toString(), ScriptType.INLINE, "mockscript",
            Collections.emptyMap(), XContentType.JSON));
        assertEquals(query, builder.rewrite(createShardContext()));

        builder = new TemplateQueryBuilder(new Script(query.toString(), ScriptType.INLINE, "mockscript",
            Collections.emptyMap(), XContentType.JSON)).boost(3);
        assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(createShardContext()));
    }

    @Override
    protected Query rewrite(Query query) throws IOException {
        // TemplateQueryBuilder adds some optimization if the template and query builder have boosts / query names that wraps
        // the actual QueryBuilder that comes from the template into a BooleanQueryBuilder to give it an outer boost / name
        // this causes some queries to be not exactly equal but equivalent such that we need to rewrite them before comparing.
        if (query != null) {
            MemoryIndex idx = new MemoryIndex();
            return idx.createSearcher().rewrite(query);
        }
        return new MatchAllDocsQuery(); // null == *:*
    }
}