Built-based on the documentation: https://www.elastic.co/guide/en/elasticsearch/reference/5.x/mapping.html
Elasticsearch Mapping Builder | Elasticsearch |
---|---|
Version: 1.0.7 | Version: 5.x.x |
<dependency>
<groupId>org.frekele.elasticsearch</groupId>
<artifactId>elasticsearch-mapping-builder</artifactId>
<version>1.0.7</version>
</dependency>
implementation 'org.frekele.elasticsearch:elasticsearch-mapping-builder:1.0.7'
@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();
}
@ElasticDocument("book")
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;
.........
}
@ElasticDocument(value = "author")
public class AuthorEntity {
@ElasticLongField
private Long id;
@ElasticTextField
private String name;
@ElasticTextField
@ElasticKeywordField
private String artisticName;
.........
}
Person parent and Employee:
@Inject
MappingBuilder mappingBuilder;
public String getMapping() {
return mappingBuilder.build(PersonEntity.class, EmployeeEntity.class).getContentAsString();
}
public class AddressEntity {
@ElasticKeywordField
private String postalCode;
@ElasticTextField
@ElasticKeywordField
@ElasticCompletionField
private String street;
@ElasticLongField
private Long number;
.........
}
public class PersonEntity {
@ElasticLongField
private Long id;
@ElasticTextField
@ElasticKeywordField
private String name;
@ElasticTextField
@ElasticKeywordField
private String fullName;
@ElasticTextField(copyTo = {"name", "fullName"})
private String fistName;
@ElasticTextField(copyTo = {"fullName"})
private String lastName;
@ElasticObjectField
private List<AddressEntity> multipleAddress;
.........
}
@ElasticDocument(value = "employee", parent = "person")
public class EmployeeEntity {
@ElasticLongField
private Long id;
@ElasticKeywordField
private String documentNumber;
@ElasticTextField
@ElasticKeywordField
private String registerNumber;
.........
}
@ElasticDocument(
value = "my_doc_type",
dynamic = @BoolValue(true),
includeInAll = @BoolValue(true),
parent = "my_parent_doc_type",
//add eager_global_ordinals into _parent
eagerGlobalOrdinalsParent = @BoolValue(true),
enabledAll = @BoolValue(true),
//add store into _all
storeAll = @BoolValue(true),
//add required into _routing
requiredRouting = @BoolValue(true)
)
public class MyDocumentEntity {
.........
@ElasticBinaryField(
suffixName = "binary",
docValues = @BoolValue(true),
store = @BoolValue(true))
private String binaryValue;
@ElasticBooleanField(
boost = @FloatValue(1),
docValues = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true))
private Boolean booleanValue;
@ElasticByteField(
suffixName = "byte",
coerce = @BoolValue(true),
boost = @FloatValue(0.2f),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
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),
includeInAll = @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),
includeInAll = @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),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Double doubleValue;
@ElasticDoubleRangeField(
suffixName = "doubleRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Double doubleRangeValue;
@ElasticFloatField(
suffixName = "float",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Float floatValue;
@ElasticFloatRangeField(
suffixName = "floatRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
includeInAll = @BoolValue(true),
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),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Float halfFloatValue;
@ElasticIntegerField(
suffixName = "integer",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Integer integerValue;
@ElasticIntegerRangeField(
suffixName = "integerRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
store = @BoolValue(true)
)
private Integer integerRangeValue;
@ElasticIpField(
suffixName = "ip",
boost = @FloatValue(1),
docValues = @BoolValue(true),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private String ipValue;
@ElasticIpRangeField(
suffixName = "ipRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
includeInAll = @BoolValue(true),
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),
includeInAll = @BoolValue(true),
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),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true)
)
private Long longValue;
@ElasticLongRangeField(
suffixName = "longRange",
coerce = @BoolValue(true),
boost = @FloatValue(1),
includeInAll = @BoolValue(true),
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),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
store = @BoolValue(true),
scalingFactor = @IntValue(100)
)
private Float scaledFloatValue;
@ElasticShortField(
suffixName = "short",
coerce = @BoolValue(true),
boost = @FloatValue(1),
docValues = @BoolValue(true),
ignoreMalformed = @BoolValue(true),
includeInAll = @BoolValue(true),
index = @BoolValue(true),
nullValue = "NULL",
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)
),
includeInAll = @BoolValue(true),
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),
includeInAll = @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)
),
includeInAll = @BoolValue(true),
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),
includeInAll = @BoolValue(true),
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;
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.