Built-based on the documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
Elasticsearch Mapping Builder | Elasticsearch |
---|---|
Version: 1.0.7 | Version: 5.x |
Version: 1.1.0 | Version: 7.x |
<dependency>
<groupId>org.frekele.elasticsearch</groupId>
<artifactId>elasticsearch-mapping-builder</artifactId>
<version>1.1.0</version>
</dependency>
implementation 'org.frekele.elasticsearch:elasticsearch-mapping-builder:1.1.0'
@Inject
MappingBuilder mappingBuilder;
//or
MappingBuilder mappingBuilder = new MappingBuilderImpl();
ObjectMapping mapping = mappingBuilder.build(MyEntity1.class, MyEntity2.class, MyEntity3.class);
String jsonMapping = mapping.getContentAsString();
//or
XContentBuilder contentBuilder = mapping.getContent();
String jsonMapping =contentBuilder.string();
Book:
@Inject
MappingBuilder mappingBuilder;
public String getMapping() {
return mappingBuilder.build(BookEntity.class).getContentAsString();
}
public class BookEntity {
@ElasticKeywordField
private String isbn;
@ElasticTextField
@ElasticKeywordField
@ElasticCompletionField
private String name;
@ElasticTextField
private String description;
@ElasticDateField
private OffsetDateTime releaseDate;
@ElasticBooleanField
private Boolean active;
@ElasticBinaryField
private String imageBlob;
@ElasticObjectField
private AuthorEntity author;
.........
}
public class AuthorEntity {
@ElasticLongField
private Long id;
@ElasticTextField
private String name;
@ElasticTextField
@ElasticKeywordField
private String artisticName;
.........
}
Employee:
@Inject
MappingBuilder mappingBuilder;
public String getMapping() {
return mappingBuilder.build(EmployeeEntity.class).getContentAsString();
}
public class AddressEntity {
@ElasticKeywordField
private String postalCode;
@ElasticTextField
@ElasticKeywordField
@ElasticCompletionField
private String street;
@ElasticLongField
private Long number;
.........
}
public class EmployeeEntity {
@ElasticLongField
private Long id;
@ElasticKeywordField
private String documentNumber;
@ElasticTextField
@ElasticKeywordField
private String registerNumber;
.........
}
@ElasticBinaryField(
suffixName = "binary",
docValues = @BoolValue(true),
store = @BoolValue(true))
private String binaryValue;
@ElasticBooleanField(
boost = @FloatValue(1),
docValues = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true))
private Boolean booleanValue;
@ElasticByteField(
suffixName = "byte",
coerce = @BoolValue(true),
boost = @FloatValue(0.2f),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Byte byteValue;
@ElasticCompletionField(
suffixName = "completion",
analyzer = "myAnalyzer",
searchAnalyzer = "mySearchAnalyzer",
preserveSeparators = @BoolValue(true),
preservePositionIncrements = @BoolValue(true),
maxInputLength = @IntValue(50)
)
private String completionValue;
@ElasticDateField(
suffixName = "date",
boost = @FloatValue(1),
docValues = @BoolValue(true),
format = "basic_date_time",
locale = "en-US",
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Date dateValue;
@ElasticDateRangeField(
suffixName = "dateRange",
boost = @FloatValue(1),
docValues = @BoolValue(true),
format = "basic_date_time",
locale = "en-US",
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Date dateRangeValue;
@ElasticDoubleField(
suffixName = "double",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Double doubleValue;
@ElasticDoubleRangeField(
suffixName = "doubleRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Double doubleRangeValue;
@ElasticFloatField(
suffixName = "float",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Float floatValue;
@ElasticFloatRangeField(
suffixName = "floatRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Float floatRangeValue;
@ElasticGeoPointField(
suffixName = "geoPoint",
ignoreMalformed = @BoolValue(true)
)
private String geoPointValue;
@ElasticGeoShapeField(
suffixName = "geoShape",
tree = "geohash",
precision = "kilometers",
treeLevels = "50m",
strategy = "recursive",
distanceErrorPct = @FloatValue(0.025f),
orientation = "ccw",
pointsOnly = @BoolValue(false)
)
private String geoShapeValue;
@ElasticHalfFloatField(
suffixName = "halfFloat",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Float halfFloatValue;
@ElasticIntegerField(
suffixName = "integer",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Integer integerValue;
@ElasticIntegerRangeField(
suffixName = "integerRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Integer integerRangeValue;
@ElasticIpField(
suffixName = "ip",
boost = @FloatValue(1),
docValues = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private String ipValue;
@ElasticIpRangeField(
suffixName = "ipRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
index = @BoolValue(true),
store = @BoolValue(true)
)
private String ipRangeValue;
@ElasticKeywordField(
suffixName = "keyword",
analyzer = "myAnalyzer",
boost = @FloatValue(1),
docValues = @BoolValue(true),
eagerGlobalOrdinals = @BoolValue(true),
ignoreAbove = @IntValue(350),
index = @BoolValue(true),
indexOptions = "docs",
norms = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true),
similarity = "BM25",
normalizer = "my_normalizer"
)
private String keywordValue;
@ElasticLongField(
suffixName = "long",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Long longValue;
@ElasticLongRangeField(
suffixName = "longRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Long longRangeValue;
@ElasticPercolatorField(
suffixName = "percolator"
)
private String percolatorValue;
@ElasticScaledFloatField(
suffixName = "scaledFloat",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true),
scalingFactor = @IntValue(100)
)
private Float scaledFloatValue;
@ElasticShortField(
suffixName = "short",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Short shortValue;
@ElasticTextField(
suffixName = "text",
analyzer = "myAnalyzer",
boost = @FloatValue(1),
eagerGlobalOrdinals = @BoolValue(true),
fielddata = @BoolValue(true),
fielddataFrequencyFilter = @FielddataFrequencyFilterValue(
min = @FloatValue(0.001f),
max = @FloatValue(0.1f),
minSegmentSize = @IntValue(500)
),
index = @BoolValue(true),
indexOptions = "",
norms = @BoolValue(true),
positionIncrementGap = @IntValue(100),
store = @BoolValue(true),
searchAnalyzer = "mySearchAnalyzer",
searchQuoteAnalyzer = "my_analyzer",
similarity = "BM25",
termVector = "no",
copyTo = {"anotherField"}
)
private String textValue;
@ElasticTokenCountField(
suffixName = "tokenCount",
analyzer = "myAnalyzer",
enablePositionIncrements = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private String tokenCountValue;
@ElasticNestedField(
dynamic = @BoolValue(true),
includeInAll = @BoolValue(true)
)
private InnerDocumentEntity nestedValue;
@ElasticObjectField(
dynamic = @BoolValue(true),
enabledJson = @BoolValue(true),
includeInAll = @BoolValue(true)
)
private InnerDocumentEntity objectValue;
@ElasticCustomJsonField(
path = "/custom/mapping/test-custom-field.json",
classLoader = ClassLoader.class)
private String customValue;
@ElasticTextField(
suffixName = "text",
analyzer = "myAnalyzer",
boost = @FloatValue(1),
eagerGlobalOrdinals = @BoolValue(true),
fielddata = @BoolValue(true),
fielddataFrequencyFilter = @FielddataFrequencyFilterValue(
min = @FloatValue(0.001f),
max = @FloatValue(0.1f),
minSegmentSize = @IntValue(500)
),
index = @BoolValue(true),
indexOptions = "",
norms = @BoolValue(true),
positionIncrementGap = @IntValue(100),
store = @BoolValue(true),
searchAnalyzer = "mySearchAnalyzer",
searchQuoteAnalyzer = "my_analyzer",
similarity = "BM25",
termVector = "no",
copyTo = {"anotherField"}
)
@ElasticKeywordField(
suffixName = "keyword",
analyzer = "myAnalyzer",
boost = @FloatValue(1),
docValues = @BoolValue(true),
eagerGlobalOrdinals = @BoolValue(true),
ignoreAbove = @IntValue(350),
index = @BoolValue(true),
indexOptions = "docs",
norms = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true),
similarity = "BM25",
normalizer = "my_normalizer"
)
@ElasticCompletionField(
suffixName = "completion",
analyzer = "myAnalyzer",
searchAnalyzer = "mySearchAnalyzer",
preserveSeparators = @BoolValue(true),
preservePositionIncrements = @BoolValue(true),
maxInputLength = @IntValue(50)
)
private String multiFieldValue;
import java.time.OffsetDateTime
import org.frekele.elasticsearch.mapping.MappingBuilderImpl
import org.frekele.elasticsearch.mapping.annotations._
import org.frekele.elasticsearch.mapping.annotations.values.IntValue
import scala.annotation.meta.field
case class BookEntity(
@(ElasticKeywordField@field)
isbn: String,
@(ElasticTextField@field)
@(ElasticKeywordField@field)(ignoreAbove = new IntValue(256))
@(ElasticCompletionField@field)
name: String,
@(ElasticTextField@field)
description: String,
@(ElasticDateField@field)
releaseDate: OffsetDateTime,
@(ElasticBooleanField@field)
active: Boolean,
@(ElasticBinaryField@field)
imageBlob: String,
@(ElasticObjectField@field)
author: AuthorEntity
)
case class AuthorEntity(
@(ElasticLongField@field)
id: Long,
@(ElasticTextField@field)
name: String,
@(ElasticTextField@field)
@(ElasticKeywordField@field)
artisticName: String
)
object Main extends App {
println(new MappingBuilderImpl().build(true, classOf[BookEntity]).getContentAsString)
}
Copyright © 2017-2019 - @frekele<Leandro Kersting de Freitas>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.