1 package org.wikimedia.search.extra;
2
3 import java.io.IOException;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.stream.Collectors;
7
8 import org.elasticsearch.common.settings.Settings;
9 import org.elasticsearch.common.xcontent.DeprecationHandler;
10 import org.elasticsearch.common.xcontent.NamedXContentRegistry;
11 import org.elasticsearch.common.xcontent.XContentParser;
12 import org.elasticsearch.common.xcontent.json.JsonXContent;
13 import org.elasticsearch.index.query.AbstractQueryBuilder;
14 import org.elasticsearch.index.query.QueryBuilder;
15 import org.elasticsearch.plugins.SearchPlugin.QuerySpec;
16 import org.elasticsearch.search.SearchModule;
17
18
19
20
21 public class QueryBuilderTestUtils {
22 public static final QueryBuilderTestUtils FULLY_FEATURED = new QueryBuilderTestUtils();
23
24 private final NamedXContentRegistry xContentRegistry;
25
26 private QueryBuilderTestUtils() {
27 SearchModule module = new SearchModule(Settings.EMPTY, false, Collections.singletonList(new ExtraCorePlugin(Settings.EMPTY)));
28 xContentRegistry = new NamedXContentRegistry(module.getNamedXContents());
29 }
30
31 public QueryBuilderTestUtils(NamedXContentRegistry xContentRegistry) {
32 this.xContentRegistry = xContentRegistry;
33 }
34
35 public QueryBuilderTestUtils(List<QuerySpec<?>> querySpecs) {
36 List<NamedXContentRegistry.Entry> entries = querySpecs.stream()
37 .map((spec) -> new NamedXContentRegistry.Entry(QueryBuilder.class,
38 spec.getName(),
39 p -> spec.getParser().fromXContent(p)))
40 .collect(Collectors.toList());
41 this.xContentRegistry = new NamedXContentRegistry(entries);
42 }
43
44 public QueryBuilder parseQuery(String query) throws IOException {
45 XContentParser parser = JsonXContent.jsonXContent.createParser(xContentRegistry,
46 DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
47 query);
48 return AbstractQueryBuilder.parseInnerQueryBuilder(parser);
49 }
50 }