use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.
the class SortSymbolVisitor method visitReference.
/**
* generate a SortField from a Reference symbol.
* <p>
* the implementation is similar to what {@link org.elasticsearch.search.sort.SortParseElement}
* does.
*/
@Override
public SortField visitReference(final Reference symbol, final SortSymbolContext context) {
// can't use the SortField(fieldName, type) constructor
// because values are saved using docValues and therefore they're indexed in lucene as binary and not
// with the reference valueType.
// this is why we use a custom comparator source with the same logic as ES
ColumnIdent columnIdent = symbol.ident().columnIdent();
if (columnIdent.isColumn()) {
if (SortParseElement.SCORE_FIELD_NAME.equals(columnIdent.name())) {
return !context.reverseFlag ? SORT_SCORE_REVERSE : SortParseElement.SORT_SCORE;
} else if (DocSysColumns.RAW.equals(columnIdent) || DocSysColumns.ID.equals(columnIdent)) {
return customSortField(DocSysColumns.nameForLucene(columnIdent), symbol, context, LUCENE_TYPE_MAP.get(symbol.valueType()), false);
}
}
MultiValueMode sortMode = context.reverseFlag ? MultiValueMode.MAX : MultiValueMode.MIN;
String indexName;
IndexFieldData.XFieldComparatorSource fieldComparatorSource;
MappedFieldType fieldType = fieldTypeLookup.get(columnIdent.fqn());
if (fieldType == null) {
indexName = columnIdent.fqn();
fieldComparatorSource = new NullFieldComparatorSource(LUCENE_TYPE_MAP.get(symbol.valueType()), context.reverseFlag, context.nullFirst);
} else {
indexName = fieldType.names().indexName();
fieldComparatorSource = context.context.fieldData().getForField(fieldType).comparatorSource(SortOrder.missing(context.reverseFlag, context.nullFirst), sortMode, null);
}
return new SortField(indexName, fieldComparatorSource, context.reverseFlag);
}
use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.
the class LuceneReferenceResolver method getImplementation.
@Override
public LuceneCollectorExpression<?> getImplementation(Reference refInfo) {
assert refInfo.granularity() == RowGranularity.DOC : "lucene collector expressions are required to be on DOC granularity";
ColumnIdent columnIdent = refInfo.ident().columnIdent();
String name = columnIdent.name();
if (RawCollectorExpression.COLUMN_NAME.equals(name)) {
if (columnIdent.isColumn()) {
return new RawCollectorExpression();
} else {
// TODO: implement an Object source expression which may support subscripts
throw new UnsupportedFeatureException(String.format(Locale.ENGLISH, "_source expression does not support subscripts %s", columnIdent.fqn()));
}
} else if (UidCollectorExpression.COLUMN_NAME.equals(name)) {
return new UidCollectorExpression();
} else if (IdCollectorExpression.COLUMN_NAME.equals(name)) {
return new IdCollectorExpression();
} else if (DocCollectorExpression.COLUMN_NAME.equals(name)) {
return DocCollectorExpression.create(refInfo);
} else if (FetchIdCollectorExpression.COLUMN_NAME.equals(name)) {
return new FetchIdCollectorExpression();
} else if (ScoreCollectorExpression.COLUMN_NAME.equals(name)) {
return new ScoreCollectorExpression();
}
String colName = columnIdent.fqn();
MappedFieldType fieldType = fieldTypeLookup.get(colName);
if (fieldType == null) {
return new NullValueCollectorExpression(colName);
}
switch(refInfo.valueType().id()) {
case ByteType.ID:
return new ByteColumnReference(colName);
case ShortType.ID:
return new ShortColumnReference(colName);
case IpType.ID:
return new IpColumnReference(colName);
case StringType.ID:
return new BytesRefColumnReference(colName, fieldType);
case DoubleType.ID:
return new DoubleColumnReference(colName, fieldType);
case BooleanType.ID:
return new BooleanColumnReference(colName);
case ObjectType.ID:
return new ObjectColumnReference(colName);
case FloatType.ID:
return new FloatColumnReference(colName, fieldType);
case LongType.ID:
case TimestampType.ID:
return new LongColumnReference(colName);
case IntegerType.ID:
return new IntegerColumnReference(colName);
case GeoPointType.ID:
return new GeoPointColumnReference(colName, fieldType);
case GeoShapeType.ID:
return new GeoShapeColumnReference(colName);
case ArrayType.ID:
case SetType.ID:
return DocCollectorExpression.create(DocReferenceConverter.toSourceLookup(refInfo));
default:
throw new UnhandledServerException(String.format(Locale.ENGLISH, "unsupported type '%s'", refInfo.valueType().getName()));
}
}
use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.
the class AggregatorTestCase method queryShardContextMock.
/**
* sub-tests that need a more complex mock can overwrite this
*/
protected QueryShardContext queryShardContextMock(MappedFieldType[] fieldTypes, IndexSettings indexSettings, CircuitBreakerService circuitBreakerService) {
QueryShardContext queryShardContext = mock(QueryShardContext.class);
for (MappedFieldType fieldType : fieldTypes) {
when(queryShardContext.fieldMapper(fieldType.name())).thenReturn(fieldType);
when(queryShardContext.getForField(fieldType)).then(invocation -> fieldType.fielddataBuilder().build(indexSettings, fieldType, new IndexFieldDataCache.None(), circuitBreakerService, mock(MapperService.class)));
}
return queryShardContext;
}
use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.
the class DiversifiedSamplerTests method testDiversifiedSampler.
public void testDiversifiedSampler() throws Exception {
String[] data = { // "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id",
"0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0", "0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0", "055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0", "0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1", "0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0", "0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1", "0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0", "0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0", "0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0", "080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" };
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
for (String entry : data) {
String[] parts = entry.split(",");
Document document = new Document();
document.add(new SortedDocValuesField("id", new BytesRef(parts[0])));
document.add(new StringField("cat", parts[1], Field.Store.NO));
document.add(new TextField("name", parts[2], Field.Store.NO));
document.add(new DoubleDocValuesField("price", Double.valueOf(parts[3])));
document.add(new StringField("inStock", parts[4], Field.Store.NO));
document.add(new StringField("author", parts[5], Field.Store.NO));
document.add(new StringField("series", parts[6], Field.Store.NO));
document.add(new StringField("sequence", parts[7], Field.Store.NO));
document.add(new SortedDocValuesField("genre", new BytesRef(parts[8])));
document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9])));
indexWriter.addDocument(document);
}
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType();
genreFieldType.setName("genre");
genreFieldType.setHasDocValues(true);
Consumer<InternalSampler> verify = result -> {
Terms terms = result.getAggregations().get("terms");
assertEquals(2, terms.getBuckets().size());
assertEquals("0805080481", terms.getBuckets().get(0).getKeyAsString());
assertEquals("0812550706", terms.getBuckets().get(1).getKeyAsString());
};
testCase(indexSearcher, genreFieldType, "map", verify);
testCase(indexSearcher, genreFieldType, "global_ordinals", verify);
testCase(indexSearcher, genreFieldType, "bytes_hash", verify);
genreFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
genreFieldType.setName("genre_id");
testCase(indexSearcher, genreFieldType, null, verify);
// wrong field:
genreFieldType = new KeywordFieldMapper.KeywordFieldType();
genreFieldType.setName("wrong_field");
genreFieldType.setHasDocValues(true);
testCase(indexSearcher, genreFieldType, null, result -> {
Terms terms = result.getAggregations().get("terms");
assertEquals(1, terms.getBuckets().size());
assertEquals("0805080481", terms.getBuckets().get(0).getKeyAsString());
});
indexReader.close();
directory.close();
}
use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.
the class DiversifiedSamplerTests method testCase.
private void testCase(IndexSearcher indexSearcher, MappedFieldType genreFieldType, String executionHint, Consumer<InternalSampler> verify) throws IOException {
MappedFieldType idFieldType = new KeywordFieldMapper.KeywordFieldType();
idFieldType.setName("id");
idFieldType.setHasDocValues(true);
SortedNumericDVIndexFieldData fieldData = new SortedNumericDVIndexFieldData(new Index("index", "index"), "price", IndexNumericFieldData.NumericType.DOUBLE);
FunctionScoreQuery query = new FunctionScoreQuery(new MatchAllDocsQuery(), new FieldValueFactorFunction("price", 1, FieldValueFactorFunction.Modifier.RECIPROCAL, null, fieldData));
DiversifiedAggregationBuilder builder = new DiversifiedAggregationBuilder("_name").field(genreFieldType.name()).executionHint(executionHint).subAggregation(new TermsAggregationBuilder("terms", null).field("id"));
InternalSampler result = search(indexSearcher, query, builder, genreFieldType, idFieldType);
verify.accept(result);
}
Aggregations