public void testKeyword() throws Exception { IndexService indexService = createIndex("index", Settings.EMPTY, "type", "bytes", "type=keyword"); client().prepareIndex("index", "type", "1") .setSource("bytes", "abc") .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); ValuesSourceConfig<ValuesSource.Bytes> config = ValuesSourceConfig.resolve( context, null, "bytes", null, null, null, null); ValuesSource.Bytes valuesSource = config.toValuesSource(context); LeafReaderContext ctx = searcher.reader().leaves().get(0); SortedBinaryDocValues values = valuesSource.bytesValues(ctx); values.setDocument(0); assertEquals(1, values.count()); assertEquals(new BytesRef("abc"), values.valueAt(0)); } }
static Query toQuery(QueryShardContext context, boolean mapUnmappedFieldsAsString, QueryBuilder queryBuilder) throws IOException { // This means that fields in the query need to exist in the mapping prior to registering this query // The reason that this is required, is that if a field doesn't exist then the query assumes defaults, which may be undesired. // // Even worse when fields mentioned in percolator queries do go added to map after the queries have been registered // then the percolator queries don't work as expected any more. // // Query parsing can't introduce new fields in mappings (which happens when registering a percolator query), // because field type can't be inferred from queries (like document do) so the best option here is to disallow // the usage of unmapped fields in percolator queries to avoid unexpected behaviour // // if index.percolator.map_unmapped_fields_as_string is set to true, query can contain unmapped fields which will be mapped // as an analyzed string. context.setAllowUnmappedFields(false); context.setMapUnmappedFieldAsString(mapUnmappedFieldsAsString); return queryBuilder.toQuery(context); }
public void testBlendNoTermQuery() { FakeFieldType ft1 = new FakeFieldType(); ft1.setName("foo"); FakeFieldType ft2 = new FakeFieldType() { @Override public Query termQuery(Object value, QueryShardContext context) { return new MatchAllDocsQuery(); } }; ft2.setName("bar"); Term[] terms = new Term[] { new Term("foo", "baz") }; float[] boosts = new float[] {2}; Query expectedClause1 = BlendedTermQuery.booleanBlendedQuery(terms, boosts, false); Query expectedClause2 = new BoostQuery(new MatchAllDocsQuery(), 3); Query expected = new BooleanQuery.Builder().setDisableCoord(true) .add(expectedClause1, Occur.SHOULD) .add(expectedClause2, Occur.SHOULD) .build(); Query actual = MultiMatchQuery.blendTerm( indexService.newQueryShardContext(randomInt(20), null, () -> { throw new UnsupportedOperationException(); }), new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3)); assertEquals(expected, actual); }
public void testLong() throws Exception { IndexService indexService = createIndex("index", Settings.EMPTY, "type", "long", "type=long"); client().prepareIndex("index", "type", "1") .setSource("long", 42) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( context, null, "long", null, null, null, null); ValuesSource.Numeric valuesSource = config.toValuesSource(context); LeafReaderContext ctx = searcher.reader().leaves().get(0); SortedNumericDocValues values = valuesSource.longValues(ctx); values.setDocument(0); assertEquals(1, values.count()); assertEquals(42, values.valueAt(0)); } }
public void testRangeQuery() throws Exception { Settings indexSettings = Settings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAsciiOfLengthBetween(1, 10), indexSettings); QueryShardContext context = new QueryShardContext(0, idxSettings, null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis); RangeFieldMapper.RangeFieldType ft = new RangeFieldMapper.RangeFieldType(type); ft.setName(FIELDNAME); ft.setIndexOptions(IndexOptions.DOCS); ShapeRelation relation = RandomPicks.randomFrom(random(), ShapeRelation.values()); boolean includeLower = random().nextBoolean(); boolean includeUpper = random().nextBoolean(); Object from = nextFrom(); Object to = nextTo(from); assertEquals(getExpectedRangeQuery(relation, from, to, includeLower, includeUpper), ft.rangeQuery(from, to, includeLower, includeUpper, relation, context)); }
@Override protected ScriptedMetricAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subfactoriesBuilder) throws IOException { QueryShardContext queryShardContext = context.getQueryShardContext(); Function<Map<String, Object>, ExecutableScript> executableInitScript; if (initScript != null) { executableInitScript = queryShardContext.getLazyExecutableScript(initScript, ScriptContext.Standard.AGGS); } else { executableInitScript = (p) -> null; } Function<Map<String, Object>, SearchScript> searchMapScript = queryShardContext.getLazySearchScript(mapScript, ScriptContext.Standard.AGGS); Function<Map<String, Object>, ExecutableScript> executableCombineScript; if (combineScript != null) { executableCombineScript = queryShardContext.getLazyExecutableScript(combineScript, ScriptContext.Standard.AGGS); } else { executableCombineScript = (p) -> null; } return new ScriptedMetricAggregatorFactory(name, searchMapScript, executableInitScript, executableCombineScript, reduceScript, params, context, parent, subfactoriesBuilder, metaData); }
protected static Nested resolveNested(QueryShardContext context, String nestedPath, QueryBuilder nestedFilter) throws IOException { Nested nested = null; if (nestedPath != null) { BitSetProducer rootDocumentsFilter = context.bitsetFilter(Queries.newNonNestedFilter()); ObjectMapper nestedObjectMapper = context.getObjectMapper(nestedPath); if (nestedObjectMapper == null) { throw new QueryShardException(context, "[nested] failed to find nested object under path [" + nestedPath + "]"); } if (!nestedObjectMapper.nested().isNested()) { throw new QueryShardException(context, "[nested] nested object under path [" + nestedPath + "] is not of nested type"); } Query innerDocumentsQuery; if (nestedFilter != null) { context.nestedScope().nextLevel(nestedObjectMapper); innerDocumentsQuery = QueryBuilder.rewriteQuery(nestedFilter, context).toFilter(context); context.nestedScope().previousLevel(); } else { innerDocumentsQuery = nestedObjectMapper.nestedTypeFilter(); } nested = new Nested(rootDocumentsFilter, innerDocumentsQuery); } return nested; }
public void testRangeQuery() throws IOException { Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build(); QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis); MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); String date1 = "2015-10-12T14:10:55"; String date2 = "2016-04-28T11:33:52"; long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date1).getMillis(); long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date2).getMillis() + 999; ft.setIndexOptions(IndexOptions.DOCS); Query expected = new IndexOrDocValuesQuery( LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newRangeQuery("field", instant1, instant2)); assertEquals(expected, ft.rangeQuery(date1, date2, true, true, context).rewrite(new MultiReader())); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery(date1, date2, true, true, context)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); }
private AbstractDistanceScoreFunction parseVariable(String fieldName, XContentParser parser, QueryShardContext context, MultiValueMode mode) throws IOException { //the field must exist, else we cannot read the value for the doc later MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType == null) { throw new ParsingException(parser.getTokenLocation(), "unknown field [{}]", fieldName); } // dates and time and geo need special handling parser.nextToken(); if (fieldType instanceof DateFieldMapper.DateFieldType) { return parseDateVariable(parser, context, fieldType, mode); } else if (fieldType instanceof GeoPointFieldType) { return parseGeoVariable(parser, context, fieldType, mode); } else if (fieldType instanceof NumberFieldMapper.NumberFieldType) { return parseNumberVariable(parser, context, fieldType, mode); } else { throw new ParsingException(parser.getTokenLocation(), "field [{}] is of type [{}], but only numeric types are supported.", fieldName, fieldType); } }
protected <A extends Aggregator, B extends AggregationBuilder> A createAggregator(B aggregationBuilder, IndexSearcher indexSearcher, MappedFieldType... fieldTypes) throws IOException { IndexSettings indexSettings = createIndexSettings(); SearchContext searchContext = createSearchContext(indexSearcher, indexSettings); CircuitBreakerService circuitBreakerService = new NoneCircuitBreakerService(); when(searchContext.bigArrays()).thenReturn(new MockBigArrays(Settings.EMPTY, circuitBreakerService)); // TODO: now just needed for top_hits, this will need to be revised for other agg unit tests: MapperService mapperService = mapperServiceMock(); when(mapperService.hasNested()).thenReturn(false); when(searchContext.mapperService()).thenReturn(mapperService); IndexFieldDataService ifds = new IndexFieldDataService(indexSettings, new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() { }), circuitBreakerService, mapperService); when(searchContext.fieldData()).thenReturn(ifds); SearchLookup searchLookup = new SearchLookup(mapperService, ifds, new String[]{"type"}); when(searchContext.lookup()).thenReturn(searchLookup); QueryShardContext queryShardContext = queryShardContextMock(fieldTypes, indexSettings, circuitBreakerService); when(searchContext.getQueryShardContext()).thenReturn(queryShardContext); @SuppressWarnings("unchecked") A aggregator = (A) aggregationBuilder.build(searchContext, null).create(null, true); return aggregator; }
public void testUnmappedLong() throws Exception { IndexService indexService = createIndex("index", Settings.EMPTY, "type"); client().prepareIndex("index", "type", "1") .setSource() .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( context, ValueType.NUMBER, "long", null, null, null, null); ValuesSource.Numeric valuesSource = config.toValuesSource(context); assertNull(valuesSource); config = ValuesSourceConfig.resolve( context, ValueType.NUMBER, "long", null, 42, null, null); valuesSource = config.toValuesSource(context); LeafReaderContext ctx = searcher.reader().leaves().get(0); SortedNumericDocValues values = valuesSource.longValues(ctx); values.setDocument(0); assertEquals(1, values.count()); assertEquals(42, values.valueAt(0)); } }
@Override public Query rangeQuery(String field, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, ShapeRelation relation, @Nullable DateTimeZone timeZone, @Nullable DateMathParser parser, QueryShardContext context) { DateTimeZone zone = (timeZone == null) ? DateTimeZone.UTC : timeZone; DateMathParser dateMathParser = (parser == null) ? new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER) : parser; Long low = lowerTerm == null ? Long.MIN_VALUE : dateMathParser.parse(lowerTerm instanceof BytesRef ? ((BytesRef) lowerTerm).utf8ToString() : lowerTerm.toString(), context::nowInMillis, false, zone); Long high = upperTerm == null ? Long.MAX_VALUE : dateMathParser.parse(upperTerm instanceof BytesRef ? ((BytesRef) upperTerm).utf8ToString() : upperTerm.toString(), context::nowInMillis, false, zone); return super.rangeQuery(field, low, high, includeLower, includeUpper, relation, zone, dateMathParser, context); }
protected FieldNameToVisibilityMap getFieldNameToVisibilityMap(QueryShardContext context) { Map<String, Object> elementMetadata = context.getMapperService().documentMapper(ELEMENT_DOCUMENT_MAPPER_NAME).meta(); if (elementMetadata == null) { throw new NullPointerException("Could not find " + ELEMENT_DOCUMENT_MAPPER_NAME + " metadata"); } Object memgraphMeta = elementMetadata.get("memgraph"); if (memgraphMeta == null) { throw new NullPointerException("Could not find memgraph metadata in field mapping"); } return FieldNameToVisibilityMap.createFrommemgraphMetadata(memgraphMeta); }
public ParserContext(String type, IndexAnalyzers indexAnalyzers, Function<String, SimilarityProvider> similarityLookupService, MapperService mapperService, Function<String, TypeParser> typeParsers, Version indexVersionCreated, Supplier<QueryShardContext> queryShardContextSupplier) { this.type = type; this.indexAnalyzers = indexAnalyzers; this.similarityLookupService = similarityLookupService; this.mapperService = mapperService; this.typeParsers = typeParsers; this.indexVersionCreated = indexVersionCreated; this.queryShardContextSupplier = queryShardContextSupplier; }
public void testCommonCaseIsOptimized() throws IOException { // make sure the below tests test something... assertFalse(SortField.class.equals(LatLonDocValuesField.newDistanceSort("random_field_name", 3.5, 2.1).getClass())); QueryShardContext context = createMockShardContext(); // The common case should use LatLonDocValuesField.newDistanceSort GeoDistanceSortBuilder builder = new GeoDistanceSortBuilder("", new GeoPoint(3.5, 2.1)); SortFieldAndFormat sort = builder.build(context); assertEquals(LatLonDocValuesField.newDistanceSort("random_field_name", 3.5, 2.1).getClass(), sort.field.getClass()); // however this might be disabled by fancy options builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1), new GeoPoint(3.0, 4)); sort = builder.build(context); assertEquals(SortField.class, sort.field.getClass()); // 2 points -> plain SortField with a custom comparator builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1)); builder.unit(DistanceUnit.KILOMETERS); sort = builder.build(context); assertEquals(SortField.class, sort.field.getClass()); // km rather than m -> plain SortField with a custom comparator builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1)); builder.order(SortOrder.DESC); sort = builder.build(context); assertEquals(SortField.class, sort.field.getClass()); // descending means the max value should be considered rather than min builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1)); builder.setNestedPath("some_nested_path"); sort = builder.build(context); assertEquals(SortField.class, sort.field.getClass()); // can't use LatLon optimized sorting with nested fields builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1)); builder.order(SortOrder.DESC); sort = builder.build(context); assertEquals(SortField.class, sort.field.getClass()); // can't use LatLon optimized sorting with DESC sorting }
private static SearchContext getSearchContext(String[] types, QueryShardContext context) { TestSearchContext testSearchContext = new TestSearchContext(context) { @Override public MapperService mapperService() { return serviceHolder.mapperService; // need to build / parse inner hits sort fields } @Override public IndexFieldDataService fieldData() { return serviceHolder.indexFieldDataService; // need to build / parse inner hits sort fields } }; testSearchContext.getQueryShardContext().setTypes(types); return testSearchContext; }
/** * This test ensures that queries that need to be rewritten have dedicated tests. * These queries must override this method accordingly. */ public void testMustRewrite() throws IOException { QueryShardContext context = createShardContext(); context.setAllowUnmappedFields(true); QB queryBuilder = createTestQueryBuilder(); queryBuilder.toQuery(context); }
public TestSearchContext(QueryShardContext queryShardContext) { this.bigArrays = null; this.indexService = null; this.indexFieldDataService = null; this.threadPool = null; this.fixedBitSetFilterCache = null; this.indexShard = null; this.queryShardContext = queryShardContext; }
@Before public void setup() { indexService = createIndex("test"); mapperRegistry = new MapperRegistry( Collections.singletonMap(Murmur3FieldMapper.CONTENT_TYPE, new Murmur3FieldMapper.TypeParser()), Collections.emptyMap()); Supplier<QueryShardContext> queryShardContext = () -> { return indexService.newQueryShardContext(0, null, () -> { throw new UnsupportedOperationException(); }); }; parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), indexService.getIndexAnalyzers(), indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext); }
@Override public SuggestionContext build(QueryShardContext context) throws IOException { Map<String, Object> options = new HashMap<>(); options.put(FIELDNAME_FIELD.getPreferredName(), field()); options.put(RANDOM_SUFFIX_FIELD.getPreferredName(), randomSuffix); CustomSuggester.CustomSuggestionsContext customSuggestionsContext = new CustomSuggester.CustomSuggestionsContext(context, options); customSuggestionsContext.setField(field()); assert text != null; customSuggestionsContext.setText(BytesRefs.toBytesRef(text)); return customSuggestionsContext; }
private static SearchScript createScript(Script script, QueryShardContext context) { if (script == null) { return null; } else { return context.getSearchScript(script, ScriptContext.Standard.AGGS); } }
public void testSeesFieldsFromPlugins() throws IOException { IndexService indexService = createIndex("test"); IndicesModule indicesModule = newTestIndicesModule( Collections.emptyMap(), Collections.singletonMap("_dummy", new DummyMetadataFieldMapper.TypeParser()) ); final MapperRegistry mapperRegistry = indicesModule.getMapperRegistry(); Supplier<QueryShardContext> queryShardContext = () -> { return indexService.newQueryShardContext(0, null, () -> { throw new UnsupportedOperationException(); }); }; MapperService mapperService = new MapperService(indexService.getIndexSettings(), indexService.getIndexAnalyzers(), indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext); DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), mapperService, indexService.getIndexAnalyzers(), indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string(); DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); ParsedDocument parsedDocument = mapper.parse("index", "type", "id", new BytesArray("{}")); IndexableField[] fields = parsedDocument.rootDoc().getFields(FieldNamesFieldMapper.NAME); boolean found = false; for (IndexableField f : fields) { if ("_dummy".equals(f.stringValue())) { found = true; break; } } assertTrue("Could not find the dummy field among " + Arrays.toString(fields), found); }
@Override public Query termsQuery(List<?> values, QueryShardContext context) { failIfNotIndexed(); BytesRef[] bytesRefs = new BytesRef[values.size()]; for (int i = 0; i < bytesRefs.length; i++) { bytesRefs[i] = indexedValueForSearch(values.get(i)); } return new TermInSetQuery(name(), bytesRefs); }
public SuggestionSearchContext build(QueryShardContext context) throws IOException { SuggestionSearchContext suggestionSearchContext = new SuggestionSearchContext(); for (Entry<String, SuggestionBuilder<?>> suggestion : suggestions.entrySet()) { SuggestionContext suggestionContext = suggestion.getValue().build(context); if (suggestionContext.getText() == null) { if (globalText == null) { throw new IllegalArgumentException("The required text option is missing"); } suggestionContext.setText(BytesRefs.toBytesRef(globalText)); } suggestionSearchContext.addSuggestion(suggestion.getKey(), suggestionContext); } return suggestionSearchContext; }
/** Build a constant-scoring query that matches all values. The default implementation uses a * {@link ConstantScoreQuery} around a {@link BooleanQuery} whose {@link Occur#SHOULD} clauses * are generated with {@link #termQuery}. */ public Query termsQuery(List<?> values, @Nullable QueryShardContext context) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (Object value : values) { builder.add(termQuery(value, context), Occur.SHOULD); } return new ConstantScoreQuery(builder.build()); }
/** * Validates an alias filter by parsing it using the * provided {@link org.elasticsearch.index.query.QueryShardContext} * @throws IllegalArgumentException if the filter is not valid */ public void validateAliasFilter(String alias, String filter, QueryShardContext queryShardContext, NamedXContentRegistry xContentRegistry) { assert queryShardContext != null; try (XContentParser parser = XContentFactory.xContent(filter).createParser(xContentRegistry, filter)) { validateAliasFilter(parser, queryShardContext); } catch (Exception e) { throw new IllegalArgumentException("failed to parse filter for alias [" + alias + "]", e); } }
public static Optional<SortAndFormats> buildSort(List<SortBuilder<?>> sortBuilders, QueryShardContext context) throws IOException { List<SortField> sortFields = new ArrayList<>(sortBuilders.size()); List<DocValueFormat> sortFormats = new ArrayList<>(sortBuilders.size()); for (SortBuilder<?> builder : sortBuilders) { SortFieldAndFormat sf = builder.build(context); sortFields.add(sf.field); sortFormats.add(sf.format); } if (!sortFields.isEmpty()) { // optimize if we just sort on score non reversed, we don't really // need sorting boolean sort; if (sortFields.size() > 1) { sort = true; } else { SortField sortField = sortFields.get(0); if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) { sort = false; } else { sort = true; } } if (sort) { return Optional.of(new SortAndFormats( new Sort(sortFields.toArray(new SortField[sortFields.size()])), sortFormats.toArray(new DocValueFormat[sortFormats.size()]))); } } return Optional.empty(); }
public void testMultiMatchPrefixWithAllField() throws IOException { QueryShardContext queryShardContext = indexService.newQueryShardContext( randomInt(20), null, () -> { throw new UnsupportedOperationException(); }); queryShardContext.setAllowUnmappedFields(true); Query parsedQuery = multiMatchQuery("foo").field("_all").type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX).toQuery(queryShardContext); assertThat(parsedQuery, instanceOf(MultiPhrasePrefixQuery.class)); assertThat(parsedQuery.toString(), equalTo("_all:\"foo*\"")); }
@Override public void rewrite(QueryShardContext context) throws IOException { SearchSourceBuilder source = this.source; SearchSourceBuilder rewritten = null; aliasFilter = aliasFilter.rewrite(context); while (rewritten != source) { rewritten = source.rewrite(context); source = rewritten; } this.source = source; }
public void testEmptyLong() throws Exception { IndexService indexService = createIndex("index", Settings.EMPTY, "type", "long", "type=long"); client().prepareIndex("index", "type", "1") .setSource() .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( context, null, "long", null, null, null, null); ValuesSource.Numeric valuesSource = config.toValuesSource(context); LeafReaderContext ctx = searcher.reader().leaves().get(0); SortedNumericDocValues values = valuesSource.longValues(ctx); values.setDocument(0); assertEquals(0, values.count()); config = ValuesSourceConfig.resolve( context, null, "long", null, 42, null, null); valuesSource = config.toValuesSource(context); values = valuesSource.longValues(ctx); values.setDocument(0); assertEquals(1, values.count()); assertEquals(42, values.valueAt(0)); } }
@Override public QueryRescoreContext build(QueryShardContext context) throws IOException { org.elasticsearch.search.rescore.QueryRescorer rescorer = new org.elasticsearch.search.rescore.QueryRescorer(); QueryRescoreContext queryRescoreContext = new QueryRescoreContext(rescorer); queryRescoreContext.setQuery(QueryBuilder.rewriteQuery(this.queryBuilder, context).toQuery(context)); queryRescoreContext.setQueryWeight(this.queryWeight); queryRescoreContext.setRescoreQueryWeight(this.rescoreQueryWeight); queryRescoreContext.setScoreMode(this.scoreMode); if (this.windowSize != null) { queryRescoreContext.setWindowSize(this.windowSize); } return queryRescoreContext; }
@Override protected ScoreFunction doToFunction(QueryShardContext context) { try { SearchScript searchScript = context.getSearchScript(script, ScriptContext.Standard.SEARCH); return new ScriptScoreFunction(script, searchScript); } catch (Exception e) { throw new QueryShardException(context, "script_score: the script could not be loaded", e); } }
@Override protected ScoreFunction doToFunction(QueryShardContext context) { MappedFieldType fieldType = context.getMapperService().fullName(field); IndexNumericFieldData fieldData = null; if (fieldType == null) { if(missing == null) { throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined."); } } else { fieldData = context.getForField(fieldType); } return new FieldValueFactorFunction(field, factor, modifier, missing, fieldData); }
@Override protected ScoreFunction doToFunction(QueryShardContext context) throws IOException { AbstractDistanceScoreFunction scoreFunction; // EMPTY is safe because parseVariable doesn't use namedObject try (XContentParser parser = XContentFactory.xContent(functionBytes).createParser(NamedXContentRegistry.EMPTY, functionBytes)) { scoreFunction = parseVariable(fieldName, parser, context, multiValueMode); } return scoreFunction; }
private AbstractDistanceScoreFunction parseNumberVariable(XContentParser parser, QueryShardContext context, MappedFieldType fieldType, MultiValueMode mode) throws IOException { XContentParser.Token token; String parameterName = null; double scale = 0; double origin = 0; double decay = 0.5; double offset = 0.0d; boolean scaleFound = false; boolean refFound = false; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { parameterName = parser.currentName(); } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) { scale = parser.doubleValue(); scaleFound = true; } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) { decay = parser.doubleValue(); } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) { origin = parser.doubleValue(); refFound = true; } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) { offset = parser.doubleValue(); } else { throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName); } } if (!scaleFound || !refFound) { throw new ElasticsearchParseException("both [{}] and [{}] must be set for numeric fields.", DecayFunctionBuilder.SCALE, DecayFunctionBuilder.ORIGIN); } IndexNumericFieldData numericFieldData = context.getForField(fieldType); return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode); }
private AbstractDistanceScoreFunction parseGeoVariable(XContentParser parser, QueryShardContext context, MappedFieldType fieldType, MultiValueMode mode) throws IOException { XContentParser.Token token; String parameterName = null; GeoPoint origin = new GeoPoint(); String scaleString = null; String offsetString = "0km"; double decay = 0.5; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { parameterName = parser.currentName(); } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) { scaleString = parser.text(); } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) { origin = GeoUtils.parseGeoPoint(parser); } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) { decay = parser.doubleValue(); } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) { offsetString = parser.text(); } else { throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName); } } if (origin == null || scaleString == null) { throw new ElasticsearchParseException("[{}] and [{}] must be set for geo fields.", DecayFunctionBuilder.ORIGIN, DecayFunctionBuilder.SCALE); } double scale = DistanceUnit.DEFAULT.parse(scaleString, DistanceUnit.DEFAULT); double offset = DistanceUnit.DEFAULT.parse(offsetString, DistanceUnit.DEFAULT); IndexGeoPointFieldData indexFieldData = context.getForField(fieldType); return new GeoFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), indexFieldData, mode); }
@Override protected ScoreFunction doToFunction(QueryShardContext context) { final MappedFieldType fieldType = context.getMapperService().fullName("_uid"); if (fieldType == null) { // mapper could be null if we are on a shard with no docs yet, so this won't actually be used return new RandomScoreFunction(); } final int salt = (context.index().getName().hashCode() << 10) | context.getShardId(); final IndexFieldData<?> uidFieldData = context.getForField(fieldType); return new RandomScoreFunction(this.seed == null ? hash(context.nowInMillis()) : seed, salt, uidFieldData); }
/** * Called on a data node, converts this ScoreFunctionBuilder into its corresponding Lucene function object. */ public final ScoreFunction toFunction(QueryShardContext context) throws IOException { ScoreFunction scoreFunction = doToFunction(context); if (weight == null) { return scoreFunction; } return new WeightFactorFunction(weight, scoreFunction); }
@Override public final Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); if (method != null) { query.setRewriteMethod(method); } return query; }
@Override public final Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { failIfNotIndexed(); RegexpQuery query = new RegexpQuery(new Term(name(), indexedValueForSearch(value)), flags, maxDeterminizedStates); if (method != null) { query.setRewriteMethod(method); } return query; }