Skip to content

Instantly share code, notes, and snippets.

@clement-tourriere
Created August 28, 2015 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clement-tourriere/1ffe9eba5bfa0070e5d2 to your computer and use it in GitHub Desktop.
Save clement-tourriere/1ffe9eba5bfa0070e5d2 to your computer and use it in GitHub Desktop.
Simple external geo_point plugin
package com.opendatasoft.elasticsearch.index.mapper.geo;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.fielddata.FieldDataType;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.index.mapper.core.AbstractFieldMapper;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import static org.elasticsearch.index.mapper.MapperBuilders.stringField;
import static org.elasticsearch.index.mapper.core.TypeParsers.parseField;
import static org.elasticsearch.index.mapper.core.TypeParsers.parseMultiField;
public class ExternalMapper extends AbstractFieldMapper<Object> {
public static final String CONTENT_TYPE = "test_external";
@Override
public Object value(Object value) {
return null;
}
public static class Names {
public static final String FIELD_POINT = "point";
}
public static class Builder extends AbstractFieldMapper.Builder<Builder, ExternalMapper> {
private GeoPointFieldMapper.Builder pointBuilder = new GeoPointFieldMapper.Builder(Names.FIELD_POINT);
private Mapper.Builder stringBuilder;
public Builder(String name) {
super(name, new FieldType(Defaults.FIELD_TYPE));
this.stringBuilder = stringField(name).store(false);
this.builder = this;
}
public Builder string(Mapper.Builder content) {
this.stringBuilder = content;
return this;
}
@Override
public ExternalMapper build(BuilderContext context) {
ContentPath.Type origPathType = context.path().pathType();
context.path().pathType(ContentPath.Type.FULL);
context.path().add(name);
GeoPointFieldMapper pointMapper = pointBuilder.docValues(true).build(context);
context.path().remove();
context.path().pathType(origPathType);
return new ExternalMapper(buildNames(context), pointMapper, multiFieldsBuilder.build(this, context), copyTo);
}
}
public static class TypeParser implements Mapper.TypeParser {
@SuppressWarnings({"unchecked"})
@Override
public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
ExternalMapper.Builder builder = new ExternalMapper.Builder(name);
parseField(builder, name, node, parserContext);
for (Map.Entry<String, Object> entry : node.entrySet()) {
String propName = Strings.toUnderscoreCase(entry.getKey());
Object propNode = entry.getValue();
parseMultiField(builder, name, parserContext, propName, propNode);
}
return builder;
}
}
private final GeoPointFieldMapper pointMapper;
public ExternalMapper(FieldMapper.Names names, GeoPointFieldMapper pointMapper, MultiFields multiFields, CopyTo copyTo) {
super(names, 1.0f, Defaults.FIELD_TYPE, false, null, null, null, null, null, null, null, ImmutableSettings.EMPTY,
multiFields, copyTo);
this.pointMapper = pointMapper;
}
@Override
public FieldType defaultFieldType() {
return Defaults.FIELD_TYPE;
}
@Override
public FieldDataType defaultFieldDataType() {
return null;
}
@Override
public void parse(ParseContext context) throws IOException {
Random random = new Random();
int latLow = 0;
int latHigh = 80;
int lonLow = 0;
int lonHigh = 180;
GeoPoint point = new GeoPoint(random.nextInt(latHigh - latLow) + latLow, random.nextInt(lonHigh - lonLow) + lonLow);
pointMapper.parse(context.createExternalValueContext(point));
}
@Override
public void traverse(FieldMapperListener fieldMapperListener) {
super.traverse(fieldMapperListener);
pointMapper.traverse(fieldMapperListener);
}
@Override
public void close() {
super.close();
pointMapper.close();
}
@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
}
@Override
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
builder.field("type", contentType());
}
@Override
protected String contentType() {
return CONTENT_TYPE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment