@Override public GeoShapeFieldMapper build(BuilderContext context) { setupFieldType(context); return new GeoShapeFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), ignoreZValue(), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } }
/** parsing logic for {@link XLatLonShape} indexing */ @Override public void parse(ParseContext context) throws IOException { try { Object shape = context.parseExternalValue(Object.class); if (shape == null) { ShapeBuilder shapeBuilder = ShapeParser.parse(context.parser(), this); if (shapeBuilder == null) { return; } shape = shapeBuilder.buildLucene(); } indexShape(context, shape); } catch (Exception e) { if (ignoreMalformed.value() == false) { throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(), fieldType().typeName()); } context.addIgnoredField(fieldType().name()); } }
private void indexFields(ParseContext context, Field[] fields) { ArrayList<IndexableField> flist = new ArrayList<>(Arrays.asList(fields)); createFieldNamesField(context, flist); for (IndexableField f : flist) { context.doc().add(f); } } }
private void indexShape(ParseContext context, Object luceneShape) { if (luceneShape instanceof GeoPoint) { GeoPoint pt = (GeoPoint) luceneShape; indexFields(context, XLatLonShape.createIndexableFields(name(), pt.lat(), pt.lon())); } else if (luceneShape instanceof double[]) { double[] pt = (double[]) luceneShape; indexFields(context, XLatLonShape.createIndexableFields(name(), pt[1], pt[0])); } else if (luceneShape instanceof Line) { indexFields(context, XLatLonShape.createIndexableFields(name(), (Line)luceneShape)); } else if (luceneShape instanceof Polygon) { indexFields(context, XLatLonShape.createIndexableFields(name(), (Polygon) luceneShape)); } else if (luceneShape instanceof double[][]) { double[][] pts = (double[][])luceneShape; for (int i = 0; i < pts.length; ++i) { indexFields(context, XLatLonShape.createIndexableFields(name(), pts[i][1], pts[i][0])); indexFields(context, XLatLonShape.createIndexableFields(name(), lines[i])); indexFields(context, XLatLonShape.createIndexableFields(name(), polys[i])); Polygon p = new Polygon(new double[]{r.minLat, r.minLat, r.maxLat, r.maxLat, r.minLat}, new double[]{r.minLon, r.maxLon, r.maxLon, r.minLon, r.minLon}); indexFields(context, XLatLonShape.createIndexableFields(name(), p)); } else if (luceneShape instanceof Object[]) { indexShape(context, o);
@Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); if (includeDefaults || fieldType().tree().equals(Defaults.TREE) == false) { builder.field(Names.TREE, fieldType().tree()); } if (includeDefaults || fieldType().treeLevels() != 0) { builder.field(Names.TREE_LEVELS, fieldType().treeLevels()); } if (includeDefaults || fieldType().precisionInMeters() != -1) { builder.field(Names.TREE_PRESISION, DistanceUnit.METERS.toString(fieldType().precisionInMeters())); } if (includeDefaults || fieldType().strategyName() != Defaults.STRATEGY) { builder.field(Names.STRATEGY, fieldType().strategyName()); } if (includeDefaults || fieldType().distanceErrorPct() != fieldType().defaultDistanceErrorPct) { builder.field(Names.DISTANCE_ERROR_PCT, fieldType().distanceErrorPct()); } if (includeDefaults || fieldType().orientation() != Defaults.ORIENTATION) { builder.field(Names.ORIENTATION, fieldType().orientation()); } if (includeDefaults || fieldType().pointsOnly() != GeoShapeFieldMapper.Defaults.POINTS_ONLY) { builder.field(Names.STRATEGY_POINTS_ONLY, fieldType().pointsOnly()); } if (includeDefaults || coerce.explicit()) { builder.field("coerce", coerce.value()); } }
(shapeMapper == null) ? ShapeBuilder.Orientation.RIGHT : shapeMapper.fieldType().orientation(); Explicit<Boolean> coerce = (shapeMapper == null) ? GeoShapeFieldMapper.Defaults.COERCE : shapeMapper.coerce(); Explicit<Boolean> ignoreZValue = (shapeMapper == null) ? GeoShapeFieldMapper.Defaults.IGNORE_Z_VALUE : shapeMapper.ignoreZValue();
private void indexShape(ParseContext context, Shape shape) { List<IndexableField> fields = new ArrayList<>(Arrays.asList(fieldType().defaultStrategy().createIndexableFields(shape))); createFieldNamesField(context, fields); for (IndexableField field : fields) { context.doc().add(field); } }
if (fieldType().pointsOnly() && !(shape instanceof Point)) { throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry)shape).getGeom().getGeometryType() : shape.getClass()) + " was found"); Field[] fields = fieldType().defaultStrategy().createIndexableFields(shape); if (fields == null || fields.length == 0) { return null; if (!customBoost() && fieldType.boost() != 1f && Version.indexCreated(context.indexSettings()).before(Version.V_5_0_0_alpha1)) { field.setBoost(fieldType().boost()); throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
/** throws an exception if the parsed geometry type does not match the expected shape type */ public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType, final GeoShapeFieldMapper shapeMapper) throws IOException, ElasticsearchParseException { StringReader reader = new StringReader(parser.text()); try { boolean ignoreZValue = (shapeMapper != null && shapeMapper.ignoreZValue().value() == true); // setup the tokenizer; configured to read words w/o numbers StreamTokenizer tokenizer = new StreamTokenizer(reader); tokenizer.resetSyntax(); tokenizer.wordChars('a', 'z'); tokenizer.wordChars('A', 'Z'); tokenizer.wordChars(128 + 32, 255); tokenizer.wordChars('0', '9'); tokenizer.wordChars('-', '-'); tokenizer.wordChars('+', '+'); tokenizer.wordChars('.', '.'); tokenizer.whitespaceChars(0, ' '); tokenizer.commentChar('#'); ShapeBuilder builder = parseGeometry(tokenizer, shapeType, ignoreZValue); checkEOF(tokenizer); return builder; } finally { reader.close(); } }
@Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); if (includeDefaults || fieldType().tree().equals(Defaults.TREE) == false) { builder.field(Names.TREE, fieldType().tree()); if (fieldType().treeLevels() != 0) { builder.field(Names.TREE_LEVELS, fieldType().treeLevels()); } else if(includeDefaults && fieldType().precisionInMeters() == -1) { // defaults only make sense if precision is not specified if ("geohash".equals(fieldType().tree())) { builder.field(Names.TREE_LEVELS, Defaults.GEOHASH_LEVELS); } else if ("legacyquadtree".equals(fieldType().tree())) { builder.field(Names.TREE_LEVELS, Defaults.QUADTREE_LEVELS); } else if ("quadtree".equals(fieldType().tree())) { builder.field(Names.TREE_LEVELS, Defaults.QUADTREE_LEVELS); } else { throw new IllegalArgumentException("Unknown prefix tree type [" + fieldType().tree() + "]"); if (fieldType().precisionInMeters() != -1) { builder.field(Names.TREE_PRESISION, DistanceUnit.METERS.toString(fieldType().precisionInMeters())); } else if (includeDefaults && fieldType().treeLevels() == 0) { // defaults only make sense if tree levels are not specified builder.field(Names.TREE_PRESISION, DistanceUnit.METERS.toString(50)); if (includeDefaults || fieldType().strategyName().equals(Defaults.STRATEGY) == false) { builder.field(Names.STRATEGY, fieldType().strategyName()); if (includeDefaults || fieldType().distanceErrorPct() != fieldType().defaultDistanceErrorPct) { builder.field(Names.DISTANCE_ERROR_PCT, fieldType().distanceErrorPct());
(shapeMapper == null) ? ShapeBuilder.Orientation.RIGHT : shapeMapper.fieldType().orientation(); Explicit<Boolean> coerce = (shapeMapper == null) ? GeoShapeFieldMapper.Defaults.COERCE : shapeMapper.coerce(); Explicit<Boolean> ignoreZValue = (shapeMapper == null) ? GeoShapeFieldMapper.Defaults.IGNORE_Z_VALUE : shapeMapper.ignoreZValue();
private void indexShape(ParseContext context, Shape shape) { List<IndexableField> fields = new ArrayList<>(Arrays.asList(fieldType().defaultStrategy().createIndexableFields(shape))); createFieldNamesField(context, fields); for (IndexableField field : fields) { context.doc().add(field); } }
/** throws an exception if the parsed geometry type does not match the expected shape type */ public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType, final GeoShapeFieldMapper shapeMapper) throws IOException, ElasticsearchParseException { StringReader reader = new StringReader(parser.text()); try { boolean ignoreZValue = (shapeMapper != null && shapeMapper.ignoreZValue().value() == true); // setup the tokenizer; configured to read words w/o numbers StreamTokenizer tokenizer = new StreamTokenizer(reader); tokenizer.resetSyntax(); tokenizer.wordChars('a', 'z'); tokenizer.wordChars('A', 'Z'); tokenizer.wordChars(128 + 32, 255); tokenizer.wordChars('0', '9'); tokenizer.wordChars('-', '-'); tokenizer.wordChars('+', '+'); tokenizer.wordChars('.', '.'); tokenizer.whitespaceChars(0, ' '); tokenizer.commentChar('#'); ShapeBuilder builder = parseGeometry(tokenizer, shapeType, ignoreZValue); checkEOF(tokenizer); return builder; } finally { reader.close(); } }
if (fieldType().pointsOnly() == true) { indexShape(context, s); throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry)shape).getGeom().getGeometryType() : shape.getClass()) + " was found"); indexShape(context, shape); } catch (Exception e) { if (ignoreMalformed.value() == false) { throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(), fieldType().typeName());
@Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); if (includeDefaults || fieldType().tree().equals(Defaults.TREE) == false) { builder.field(Names.TREE, fieldType().tree()); if (fieldType().treeLevels() != 0) { builder.field(Names.TREE_LEVELS, fieldType().treeLevels()); } else if(includeDefaults && fieldType().precisionInMeters() == -1) { // defaults only make sense if precision is not specified if ("geohash".equals(fieldType().tree())) { builder.field(Names.TREE_LEVELS, Defaults.GEOHASH_LEVELS); } else if ("legacyquadtree".equals(fieldType().tree())) { builder.field(Names.TREE_LEVELS, Defaults.QUADTREE_LEVELS); } else if ("quadtree".equals(fieldType().tree())) { builder.field(Names.TREE_LEVELS, Defaults.QUADTREE_LEVELS); } else { throw new IllegalArgumentException("Unknown prefix tree type [" + fieldType().tree() + "]"); if (fieldType().precisionInMeters() != -1) { builder.field(Names.TREE_PRESISION, DistanceUnit.METERS.toString(fieldType().precisionInMeters())); } else if (includeDefaults && fieldType().treeLevels() == 0) { // defaults only make sense if tree levels are not specified builder.field(Names.TREE_PRESISION, DistanceUnit.METERS.toString(50)); if (includeDefaults || fieldType().strategyName().equals(Defaults.STRATEGY) == false) { builder.field(Names.STRATEGY, fieldType().strategyName()); if (includeDefaults || fieldType().distanceErrorPct() != fieldType().defaultDistanceErrorPct) { builder.field(Names.DISTANCE_ERROR_PCT, fieldType().distanceErrorPct());
@Override public GeoShapeFieldMapper build(BuilderContext context) { GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType)fieldType; if (geoShapeFieldType.treeLevels() == 0 && geoShapeFieldType.precisionInMeters() < 0) { geoShapeFieldType.setDefaultDistanceErrorPct(Defaults.LEGACY_DISTANCE_ERROR_PCT); } setupFieldType(context); return new GeoShapeFieldMapper(name, fieldType, coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } }
if (fieldType().pointsOnly() == true) { indexShape(context, s); throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry)shape).getGeom().getGeometryType() : shape.getClass()) + " was found"); indexShape(context, shape); } catch (Exception e) { if (ignoreMalformed.value() == false) { throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
@Override public GeoShapeFieldMapper build(BuilderContext context) { GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType)fieldType; if (geoShapeFieldType.treeLevels() == 0 && geoShapeFieldType.precisionInMeters() < 0) { geoShapeFieldType.setDefaultDistanceErrorPct(Defaults.LEGACY_DISTANCE_ERROR_PCT); } setupFieldType(context); return new GeoShapeFieldMapper(name, fieldType, ignoreMalformed(context), coerce(context), ignoreZValue(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } }
@Override public GeoShapeFieldMapper build(BuilderContext context) { GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType)fieldType; if (geoShapeFieldType.treeLevels() == 0 && geoShapeFieldType.precisionInMeters() < 0) { geoShapeFieldType.setDefaultDistanceErrorPct(Defaults.LEGACY_DISTANCE_ERROR_PCT); } setupFieldType(context); return new GeoShapeFieldMapper(name, fieldType, ignoreMalformed(context), coerce(context), ignoreZValue(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } }