Skip to content

Commit be1e5be

Browse files
committed
move scripts to use new field data
1 parent 772ee9d commit be1e5be

13 files changed

+145
-60
lines changed

src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
package org.elasticsearch.index.fielddata;
2121

2222
import org.apache.lucene.util.BytesRef;
23+
import org.elasticsearch.common.unit.DistanceUnit;
2324
import org.elasticsearch.index.fielddata.util.*;
2425
import org.elasticsearch.index.mapper.geo.GeoPoint;
26+
import org.elasticsearch.index.search.geo.GeoDistance;
27+
import org.joda.time.MutableDateTime;
2528

2629
/**
2730
* Script level doc values, the assumption is that any implementation will implement a <code>getValue</code>
@@ -176,6 +179,7 @@ public IntArrayRef getValues() {
176179
static class NumericLong implements ScriptDocValues {
177180

178181
private final LongValues values;
182+
private final MutableDateTime date = new MutableDateTime(0);
179183
private int docId;
180184

181185
public NumericLong(LongValues values) {
@@ -196,6 +200,11 @@ public long getValue() {
196200
return values.getValue(docId);
197201
}
198202

203+
public MutableDateTime getDate() {
204+
date.setMillis(getValue());
205+
return date;
206+
}
207+
199208
public LongArrayRef getValues() {
200209
return values.getValues(docId);
201210
}
@@ -283,5 +292,80 @@ public GeoPoint getValue() {
283292
public GeoPointArrayRef getValues() {
284293
return values.getValues(docId);
285294
}
295+
296+
public double factorDistance(double lat, double lon) {
297+
GeoPoint point = getValue();
298+
return GeoDistance.FACTOR.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES);
299+
}
300+
301+
public double factorDistanceWithDefault(double lat, double lon, double defaultValue) {
302+
if (isEmpty()) {
303+
return defaultValue;
304+
}
305+
GeoPoint point = getValue();
306+
return GeoDistance.FACTOR.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES);
307+
}
308+
309+
public double factorDistance02(double lat, double lon) {
310+
GeoPoint point = getValue();
311+
return GeoDistance.FACTOR.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES) + 1;
312+
}
313+
314+
public double factorDistance13(double lat, double lon) {
315+
GeoPoint point = getValue();
316+
return GeoDistance.FACTOR.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES) + 2;
317+
}
318+
319+
public double arcDistance(double lat, double lon) {
320+
GeoPoint point = getValue();
321+
return GeoDistance.ARC.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES);
322+
}
323+
324+
public double arcDistanceWithDefault(double lat, double lon, double defaultValue) {
325+
if (isEmpty()) {
326+
return defaultValue;
327+
}
328+
GeoPoint point = getValue();
329+
return GeoDistance.ARC.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES);
330+
}
331+
332+
public double arcDistanceInKm(double lat, double lon) {
333+
GeoPoint point = getValue();
334+
return GeoDistance.ARC.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.KILOMETERS);
335+
}
336+
337+
public double arcDistanceInKmWithDefault(double lat, double lon, double defaultValue) {
338+
if (isEmpty()) {
339+
return defaultValue;
340+
}
341+
GeoPoint point = getValue();
342+
return GeoDistance.ARC.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.KILOMETERS);
343+
}
344+
345+
public double distance(double lat, double lon) {
346+
GeoPoint point = getValue();
347+
return GeoDistance.PLANE.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES);
348+
}
349+
350+
public double distanceWithDefault(double lat, double lon, double defaultValue) {
351+
if (isEmpty()) {
352+
return defaultValue;
353+
}
354+
GeoPoint point = getValue();
355+
return GeoDistance.PLANE.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.MILES);
356+
}
357+
358+
public double distanceInKm(double lat, double lon) {
359+
GeoPoint point = getValue();
360+
return GeoDistance.PLANE.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.KILOMETERS);
361+
}
362+
363+
public double distanceInKmWithDefault(double lat, double lon, double defaultValue) {
364+
if (isEmpty()) {
365+
return defaultValue;
366+
}
367+
GeoPoint point = getValue();
368+
return GeoDistance.PLANE.calculate(point.lat(), point.lon(), lat, lon, DistanceUnit.KILOMETERS);
369+
}
286370
}
287371
}

src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayAtomicFieldData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public StringValues getStringValues() {
9191

9292
@Override
9393
public ScriptDocValues getScriptValues() {
94-
return new ScriptDocValues.NumericDouble(getDoubleValues());
94+
return new ScriptDocValues.NumericByte(getByteValues());
9595
}
9696

9797
@Override
@@ -458,7 +458,7 @@ public long getMemorySizeInBytes() {
458458

459459
@Override
460460
public ScriptDocValues getScriptValues() {
461-
return new ScriptDocValues.NumericDouble(getDoubleValues());
461+
return new ScriptDocValues.NumericByte(getByteValues());
462462
}
463463

464464
@Override
@@ -733,7 +733,7 @@ public long getMemorySizeInBytes() {
733733

734734
@Override
735735
public ScriptDocValues getScriptValues() {
736-
return new ScriptDocValues.NumericDouble(getDoubleValues());
736+
return new ScriptDocValues.NumericByte(getByteValues());
737737
}
738738

739739
@Override

src/main/java/org/elasticsearch/index/fielddata/plain/FloatArrayAtomicFieldData.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import org.elasticsearch.common.RamUsage;
2424
import org.elasticsearch.index.fielddata.*;
2525
import org.elasticsearch.index.fielddata.ordinals.Ordinals;
26-
import org.elasticsearch.index.fielddata.util.*;
26+
import org.elasticsearch.index.fielddata.util.DoubleArrayRef;
27+
import org.elasticsearch.index.fielddata.util.FloatArrayRef;
28+
import org.elasticsearch.index.fielddata.util.IntArrayRef;
29+
import org.elasticsearch.index.fielddata.util.LongArrayRef;
2730

2831
/**
2932
*/
@@ -88,7 +91,7 @@ public StringValues getStringValues() {
8891

8992
@Override
9093
public ScriptDocValues getScriptValues() {
91-
return new ScriptDocValues.NumericDouble(getDoubleValues());
94+
return new ScriptDocValues.NumericFloat(getFloatValues());
9295
}
9396

9497
@Override
@@ -455,7 +458,7 @@ public long getMemorySizeInBytes() {
455458

456459
@Override
457460
public ScriptDocValues getScriptValues() {
458-
return new ScriptDocValues.NumericDouble(getDoubleValues());
461+
return new ScriptDocValues.NumericFloat(getFloatValues());
459462
}
460463

461464
@Override
@@ -730,7 +733,7 @@ public long getMemorySizeInBytes() {
730733

731734
@Override
732735
public ScriptDocValues getScriptValues() {
733-
return new ScriptDocValues.NumericDouble(getDoubleValues());
736+
return new ScriptDocValues.NumericFloat(getFloatValues());
734737
}
735738

736739
@Override

src/main/java/org/elasticsearch/index/fielddata/plain/IntArrayAtomicFieldData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public long getMemorySizeInBytes() {
457457

458458
@Override
459459
public ScriptDocValues getScriptValues() {
460-
return new ScriptDocValues.NumericDouble(getDoubleValues());
460+
return new ScriptDocValues.NumericInteger(getIntValues());
461461
}
462462

463463
@Override
@@ -731,7 +731,7 @@ public long getMemorySizeInBytes() {
731731

732732
@Override
733733
public ScriptDocValues getScriptValues() {
734-
return new ScriptDocValues.NumericDouble(getDoubleValues());
734+
return new ScriptDocValues.NumericInteger(getIntValues());
735735
}
736736

737737
@Override

src/main/java/org/elasticsearch/index/fielddata/plain/LongArrayAtomicFieldData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public StringValues getStringValues() {
9191

9292
@Override
9393
public ScriptDocValues getScriptValues() {
94-
return new ScriptDocValues.NumericDouble(getDoubleValues());
94+
return new ScriptDocValues.NumericLong(getLongValues());
9595
}
9696

9797
@Override
@@ -452,7 +452,7 @@ public long getMemorySizeInBytes() {
452452

453453
@Override
454454
public ScriptDocValues getScriptValues() {
455-
return new ScriptDocValues.NumericDouble(getDoubleValues());
455+
return new ScriptDocValues.NumericLong(getLongValues());
456456
}
457457

458458
@Override
@@ -723,7 +723,7 @@ public long getMemorySizeInBytes() {
723723

724724
@Override
725725
public ScriptDocValues getScriptValues() {
726-
return new ScriptDocValues.NumericDouble(getDoubleValues());
726+
return new ScriptDocValues.NumericLong(getLongValues());
727727
}
728728

729729
@Override

src/main/java/org/elasticsearch/index/fielddata/plain/ShortArrayAtomicFieldData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public StringValues getStringValues() {
9191

9292
@Override
9393
public ScriptDocValues getScriptValues() {
94-
return new ScriptDocValues.NumericDouble(getDoubleValues());
94+
return new ScriptDocValues.NumericShort(getShortValues());
9595
}
9696

9797
@Override
@@ -458,7 +458,7 @@ public long getMemorySizeInBytes() {
458458

459459
@Override
460460
public ScriptDocValues getScriptValues() {
461-
return new ScriptDocValues.NumericDouble(getDoubleValues());
461+
return new ScriptDocValues.NumericShort(getShortValues());
462462
}
463463

464464
@Override
@@ -733,7 +733,7 @@ public long getMemorySizeInBytes() {
733733

734734
@Override
735735
public ScriptDocValues getScriptValues() {
736-
return new ScriptDocValues.NumericDouble(getDoubleValues());
736+
return new ScriptDocValues.NumericShort(getShortValues());
737737
}
738738

739739
@Override

src/main/java/org/elasticsearch/index/get/ShardGetService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.elasticsearch.common.metrics.CounterMetric;
2929
import org.elasticsearch.common.metrics.MeanMetric;
3030
import org.elasticsearch.common.settings.Settings;
31-
import org.elasticsearch.index.cache.IndexCache;
3231
import org.elasticsearch.index.engine.Engine;
32+
import org.elasticsearch.index.fielddata.IndexFieldDataService;
3333
import org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor;
3434
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
3535
import org.elasticsearch.index.fieldvisitor.JustSourceFieldsVisitor;
@@ -62,7 +62,7 @@ public class ShardGetService extends AbstractIndexShardComponent {
6262

6363
private final MapperService mapperService;
6464

65-
private final IndexCache indexCache;
65+
private final IndexFieldDataService fieldDataService;
6666

6767
private IndexShard indexShard;
6868

@@ -72,11 +72,11 @@ public class ShardGetService extends AbstractIndexShardComponent {
7272

7373
@Inject
7474
public ShardGetService(ShardId shardId, @IndexSettings Settings indexSettings, ScriptService scriptService,
75-
MapperService mapperService, IndexCache indexCache) {
75+
MapperService mapperService, IndexFieldDataService fieldDataService) {
7676
super(shardId, indexSettings);
7777
this.scriptService = scriptService;
7878
this.mapperService = mapperService;
79-
this.indexCache = indexCache;
79+
this.fieldDataService = fieldDataService;
8080
}
8181

8282
public GetStats stats() {
@@ -213,7 +213,7 @@ public GetResult innerGet(String type, String id, String[] gFields, boolean real
213213
} else {
214214
if (field.contains("_source.")) {
215215
if (searchLookup == null) {
216-
searchLookup = new SearchLookup(mapperService, indexCache.fieldData(), new String[]{type});
216+
searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type});
217217
}
218218
if (sourceAsMap == null) {
219219
sourceAsMap = SourceLookup.sourceAsMap(source.source);
@@ -236,7 +236,7 @@ public GetResult innerGet(String type, String id, String[] gFields, boolean real
236236
}
237237
} else {
238238
if (searchLookup == null) {
239-
searchLookup = new SearchLookup(mapperService, indexCache.fieldData(), new String[]{type});
239+
searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type});
240240
searchLookup.source().setNextSource(source.source);
241241
}
242242

@@ -306,7 +306,7 @@ private GetResult innerGetLoadFromStoredFields(String type, String id, String[]
306306
Object value = null;
307307
if (field.contains("_source.") || field.contains("doc[")) {
308308
if (searchLookup == null) {
309-
searchLookup = new SearchLookup(mapperService, indexCache.fieldData(), new String[]{type});
309+
searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type});
310310
}
311311
SearchScript searchScript = scriptService.search(searchLookup, "mvel", field, null);
312312
searchScript.setNextReader(docIdAndVersion.reader);
@@ -324,7 +324,7 @@ private GetResult innerGetLoadFromStoredFields(String type, String id, String[]
324324
FieldMappers x = docMapper.mappers().smartName(field);
325325
if (x == null || !x.mapper().fieldType().stored()) {
326326
if (searchLookup == null) {
327-
searchLookup = new SearchLookup(mapperService, indexCache.fieldData(), new String[]{type});
327+
searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type});
328328
searchLookup.setNextReader(docIdAndVersion.reader);
329329
searchLookup.setNextDocId(docIdAndVersion.docId);
330330
}

src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.index.analysis.AnalysisService;
3636
import org.elasticsearch.index.cache.IndexCache;
3737
import org.elasticsearch.index.engine.IndexEngine;
38+
import org.elasticsearch.index.fielddata.IndexFieldDataService;
3839
import org.elasticsearch.index.mapper.MapperService;
3940
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
4041
import org.elasticsearch.index.settings.IndexSettings;
@@ -77,6 +78,8 @@ protected QueryParseContext initialValue() {
7778

7879
final IndexCache indexCache;
7980

81+
final IndexFieldDataService fieldDataService;
82+
8083
final IndexEngine indexEngine;
8184

8285
private final Map<String, QueryParser> queryParsers;
@@ -90,7 +93,7 @@ protected QueryParseContext initialValue() {
9093
public IndexQueryParserService(Index index, @IndexSettings Settings indexSettings,
9194
IndicesQueriesRegistry indicesQueriesRegistry,
9295
ScriptService scriptService, AnalysisService analysisService,
93-
MapperService mapperService, IndexCache indexCache, IndexEngine indexEngine,
96+
MapperService mapperService, IndexCache indexCache, IndexFieldDataService fieldDataService, IndexEngine indexEngine,
9497
@Nullable SimilarityService similarityService,
9598
@Nullable Map<String, QueryParserFactory> namedQueryParsers,
9699
@Nullable Map<String, FilterParserFactory> namedFilterParsers) {
@@ -100,6 +103,7 @@ public IndexQueryParserService(Index index, @IndexSettings Settings indexSetting
100103
this.mapperService = mapperService;
101104
this.similarityService = similarityService;
102105
this.indexCache = indexCache;
106+
this.fieldDataService = fieldDataService;
103107
this.indexEngine = indexEngine;
104108

105109
this.defaultField = indexSettings.get("index.query.default_field", AllFieldMapper.NAME);

src/main/java/org/elasticsearch/index/query/QueryParseContext.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.common.collect.Maps;
24-
2524
import org.apache.lucene.queryparser.classic.MapperQueryParser;
2625
import org.apache.lucene.queryparser.classic.QueryParserSettings;
2726
import org.apache.lucene.search.Filter;
@@ -34,6 +33,7 @@
3433
import org.elasticsearch.index.cache.IndexCache;
3534
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
3635
import org.elasticsearch.index.engine.IndexEngine;
36+
import org.elasticsearch.index.fielddata.IndexFieldDataService;
3737
import org.elasticsearch.index.mapper.FieldMapper;
3838
import org.elasticsearch.index.mapper.FieldMappers;
3939
import org.elasticsearch.index.mapper.MapperService;
@@ -130,6 +130,10 @@ public IndexCache indexCache() {
130130
return indexQueryParser.indexCache;
131131
}
132132

133+
public IndexFieldDataService fieldData() {
134+
return indexQueryParser.fieldDataService;
135+
}
136+
133137
public String defaultField() {
134138
return indexQueryParser.defaultField();
135139
}
@@ -291,7 +295,7 @@ public SearchLookup lookup() {
291295
return current.lookup();
292296
}
293297
if (lookup == null) {
294-
lookup = new SearchLookup(mapperService(), indexCache().fieldData(), null);
298+
lookup = new SearchLookup(mapperService(), fieldData(), null);
295299
}
296300
return lookup;
297301
}

0 commit comments

Comments
 (0)