summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java
blob: 9f202a3f161d88273f155ecb5c4f435526a0c53a (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
/*
 * 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.index.query;

import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.index.fielddata.IndexFieldDataService;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.TestSearchContext;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.search.fetch.innerhits.InnerHitsBuilder;
import org.elasticsearch.search.fetch.innerhits.InnerHitsContext;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.equalTo;

public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBuilder> {

    @Override
    public void setUp() throws Exception {
        super.setUp();
        MapperService mapperService = queryShardContext().getMapperService();
        mapperService.merge("nested_doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("nested_doc",
                STRING_FIELD_NAME, "type=string",
                INT_FIELD_NAME, "type=integer",
                DOUBLE_FIELD_NAME, "type=double",
                BOOLEAN_FIELD_NAME, "type=boolean",
                DATE_FIELD_NAME, "type=date",
                OBJECT_FIELD_NAME, "type=object",
                "nested1", "type=nested"
        ).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
    }

    @Override
    protected void setSearchContext(String[] types) {
        final MapperService mapperService = queryShardContext().getMapperService();
        final IndexFieldDataService fieldData = indexFieldDataService();
        TestSearchContext testSearchContext = new TestSearchContext() {

            @Override
            public MapperService mapperService() {
                return mapperService; // need to build / parse inner hits sort fields
            }

            @Override
            public IndexFieldDataService fieldData() {
                return fieldData; // need to build / parse inner hits sort fields
            }
        };
        testSearchContext.setTypes(types);
        SearchContext.setCurrent(testSearchContext);
    }

    /**
     * @return a {@link HasChildQueryBuilder} with random values all over the place
     */
    @Override
    protected NestedQueryBuilder doCreateTestQueryBuilder() {
        InnerHitsBuilder.InnerHit innerHit = new InnerHitsBuilder.InnerHit().setSize(100).addSort(STRING_FIELD_NAME, SortOrder.ASC);
        return new NestedQueryBuilder("nested1", RandomQueryBuilder.createQuery(random()),
                RandomPicks.randomFrom(random(), ScoreMode.values()),
                SearchContext.current() == null ? null : new QueryInnerHits("inner_hits_name", innerHit));
    }

    @Override
    protected void doAssertLuceneQuery(NestedQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
        QueryBuilder innerQueryBuilder = queryBuilder.query();
        if (innerQueryBuilder instanceof EmptyQueryBuilder) {
            assertNull(query);
        } else {
            assertThat(query, instanceOf(ToParentBlockJoinQuery.class));
            ToParentBlockJoinQuery parentBlockJoinQuery = (ToParentBlockJoinQuery) query;
            //TODO how to assert this?
        }
        if (queryBuilder.innerHit() != null) {
            assertNotNull(SearchContext.current());
            if (query != null) {
                assertNotNull(SearchContext.current().innerHits());
                assertEquals(1, SearchContext.current().innerHits().getInnerHits().size());
                assertTrue(SearchContext.current().innerHits().getInnerHits().containsKey("inner_hits_name"));
                InnerHitsContext.BaseInnerHits innerHits = SearchContext.current().innerHits().getInnerHits().get("inner_hits_name");
                assertEquals(innerHits.size(), 100);
                assertEquals(innerHits.sort().getSort().length, 1);
                assertEquals(innerHits.sort().getSort()[0].getField(), STRING_FIELD_NAME);
            } else {
                assertThat(SearchContext.current().innerHits().getInnerHits().size(), equalTo(0));
            }
        }
    }

    public void testValidate() {
        try {
            new NestedQueryBuilder(null, EmptyQueryBuilder.PROTOTYPE);
            fail("cannot be null");
        } catch (IllegalArgumentException e) {
            // expected
        }

        try {
            new NestedQueryBuilder("path", null);
            fail("cannot be null");
        } catch (IllegalArgumentException e) {
            // expected
        }

        NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", EmptyQueryBuilder.PROTOTYPE);
        try {
            nestedQueryBuilder.scoreMode(null);
            fail("cannot be null");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }

    public void testFromJson() throws IOException {
        String json =
                "{\n" + 
                "  \"nested\" : {\n" + 
                "    \"query\" : {\n" + 
                "      \"bool\" : {\n" + 
                "        \"must\" : [ {\n" + 
                "          \"match\" : {\n" + 
                "            \"obj1.name\" : {\n" + 
                "              \"query\" : \"blue\",\n" + 
                "              \"type\" : \"boolean\",\n" + 
                "              \"operator\" : \"OR\",\n" + 
                "              \"slop\" : 0,\n" + 
                "              \"prefix_length\" : 0,\n" + 
                "              \"max_expansions\" : 50,\n" + 
                "              \"fuzzy_transpositions\" : true,\n" + 
                "              \"lenient\" : false,\n" + 
                "              \"zero_terms_query\" : \"NONE\",\n" + 
                "              \"boost\" : 1.0\n" + 
                "            }\n" + 
                "          }\n" + 
                "        }, {\n" + 
                "          \"range\" : {\n" + 
                "            \"obj1.count\" : {\n" + 
                "              \"from\" : 5,\n" + 
                "              \"to\" : null,\n" + 
                "              \"include_lower\" : false,\n" + 
                "              \"include_upper\" : true,\n" + 
                "              \"boost\" : 1.0\n" + 
                "            }\n" + 
                "          }\n" + 
                "        } ],\n" + 
                "        \"disable_coord\" : false,\n" + 
                "        \"adjust_pure_negative\" : true,\n" + 
                "        \"boost\" : 1.0\n" + 
                "      }\n" + 
                "    },\n" + 
                "    \"path\" : \"obj1\",\n" + 
                "    \"score_mode\" : \"avg\",\n" + 
                "    \"boost\" : 1.0\n" + 
                "  }\n" + 
                "}";

        NestedQueryBuilder parsed = (NestedQueryBuilder) parseQuery(json);
        checkGeneratedJson(json, parsed);

        assertEquals(json, ScoreMode.Avg, parsed.scoreMode());
    }
}