|
18 | 18 | */
|
19 | 19 | package org.elasticsearch.percolator;
|
20 | 20 |
|
| 21 | +import org.apache.lucene.search.IndexSearcher; |
| 22 | +import org.apache.lucene.search.Query; |
21 | 23 | import org.apache.lucene.search.join.ScoreMode;
|
22 | 24 | import org.elasticsearch.action.search.SearchResponse;
|
23 | 25 | import org.elasticsearch.action.support.WriteRequest;
|
|
28 | 30 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
29 | 31 | import org.elasticsearch.common.xcontent.XContentFactory;
|
30 | 32 | import org.elasticsearch.common.xcontent.XContentType;
|
| 33 | +import org.elasticsearch.index.IndexService; |
31 | 34 | import org.elasticsearch.index.cache.bitset.BitsetFilterCache;
|
| 35 | +import org.elasticsearch.index.engine.Engine; |
32 | 36 | import org.elasticsearch.index.fielddata.ScriptDocValues;
|
33 | 37 | import org.elasticsearch.index.query.Operator;
|
| 38 | +import org.elasticsearch.index.query.QueryBuilder; |
34 | 39 | import org.elasticsearch.index.query.QueryBuilders;
|
| 40 | +import org.elasticsearch.index.query.QueryShardContext; |
35 | 41 | import org.elasticsearch.plugins.Plugin;
|
36 | 42 | import org.elasticsearch.script.MockScriptPlugin;
|
37 | 43 | import org.elasticsearch.script.Script;
|
|
49 | 55 | import java.util.function.Function;
|
50 | 56 |
|
51 | 57 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
| 58 | +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; |
52 | 59 | import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
| 60 | +import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; |
| 61 | +import static org.elasticsearch.index.query.QueryBuilders.scriptQuery; |
| 62 | +import static org.elasticsearch.index.query.QueryBuilders.termQuery; |
53 | 63 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
54 | 64 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
|
| 65 | +import static org.hamcrest.Matchers.equalTo; |
55 | 66 |
|
56 | 67 | public class PercolatorQuerySearchTests extends ESSingleNodeTestCase {
|
57 | 68 |
|
@@ -241,4 +252,53 @@ public void testMapUnmappedFieldAsString() throws IOException {
|
241 | 252 | assertSettingDeprecationsAndWarnings(new Setting[]{PercolatorFieldMapper.INDEX_MAP_UNMAPPED_FIELDS_AS_STRING_SETTING});
|
242 | 253 | }
|
243 | 254 |
|
| 255 | + public void testRangeQueriesWithNow() throws Exception { |
| 256 | + IndexService indexService = createIndex("test", Settings.builder().put("index.number_of_shards", 1).build(), "_doc", |
| 257 | + "field1", "type=keyword", "field2", "type=date", "query", "type=percolator"); |
| 258 | + |
| 259 | + client().prepareIndex("test", "_doc", "1") |
| 260 | + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field2").from("now-1h").to("now+1h")).endObject()) |
| 261 | + .get(); |
| 262 | + client().prepareIndex("test", "_doc", "2") |
| 263 | + .setSource(jsonBuilder().startObject().field("query", boolQuery() |
| 264 | + .filter(termQuery("field1", "value")) |
| 265 | + .filter(rangeQuery("field2").from("now-1h").to("now+1h")) |
| 266 | + ).endObject()) |
| 267 | + .get(); |
| 268 | + |
| 269 | + |
| 270 | + Script script = new Script(ScriptType.INLINE, MockScriptPlugin.NAME, "1==1", Collections.emptyMap()); |
| 271 | + client().prepareIndex("test", "_doc", "3") |
| 272 | + .setSource(jsonBuilder().startObject().field("query", boolQuery() |
| 273 | + .filter(scriptQuery(script)) |
| 274 | + .filter(rangeQuery("field2").from("now-1h").to("now+1h")) |
| 275 | + ).endObject()) |
| 276 | + .get(); |
| 277 | + client().admin().indices().prepareRefresh().get(); |
| 278 | + |
| 279 | + try (Engine.Searcher engineSearcher = indexService.getShard(0).acquireSearcher("test")) { |
| 280 | + IndexSearcher indexSearcher = engineSearcher.searcher(); |
| 281 | + long[] currentTime = new long[] {System.currentTimeMillis()}; |
| 282 | + QueryShardContext queryShardContext = |
| 283 | + indexService.newQueryShardContext(0, engineSearcher.reader(), () -> currentTime[0], null); |
| 284 | + |
| 285 | + BytesReference source = BytesReference.bytes(jsonBuilder().startObject() |
| 286 | + .field("field1", "value") |
| 287 | + .field("field2", currentTime[0]) |
| 288 | + .endObject()); |
| 289 | + QueryBuilder queryBuilder = new PercolateQueryBuilder("query", source, XContentType.JSON); |
| 290 | + Query query = queryBuilder.toQuery(queryShardContext); |
| 291 | + assertThat(indexSearcher.count(query), equalTo(3)); |
| 292 | + |
| 293 | + currentTime[0] = currentTime[0] + 10800000; // + 3 hours |
| 294 | + source = BytesReference.bytes(jsonBuilder().startObject() |
| 295 | + .field("field1", "value") |
| 296 | + .field("field2", currentTime[0]) |
| 297 | + .endObject()); |
| 298 | + queryBuilder = new PercolateQueryBuilder("query", source, XContentType.JSON); |
| 299 | + query = queryBuilder.toQuery(queryShardContext); |
| 300 | + assertThat(indexSearcher.count(query), equalTo(3)); |
| 301 | + } |
| 302 | + } |
| 303 | + |
244 | 304 | }
|
0 commit comments