#589 - ElasticSearch - extend mapping options

This commit is contained in:
Robin Bygrave
2016-03-07 22:00:41 +13:00
parent ae31117b8f
commit da8766533f
5 changed files with 239 additions and 67 deletions
@@ -18,27 +18,7 @@ public @interface DocMapping {
String name();
/**
* Set this to true to indicate that this property should be un-analysed.
* Mapping options.
*/
boolean code() default false;
/**
* Set this to true to get an additional un-analysed 'raw' field to use for sorting etc.
*/
boolean sortable() default false;
/**
* Set to true to have the property additionally stored separately from _source.
*/
boolean store() default false;
/**
* Set a boost value specific to this property.
*/
float boost() default 1;
/**
* Set a value to use instead of null.
*/
String nullValue() default "";
DocProperty options();
}
@@ -36,4 +36,75 @@ public @interface DocProperty {
* Set a value to use instead of null.
*/
String nullValue() default "";
/**
* Set this to false to exclude this from the _all property.
*/
boolean includeInAll() default true;
/**
* The analyzer to use.
*/
String analyzer() default "";
/**
* The analyzer to use for searches.
*/
String searchAnalyzer() default "";
/**
* The index options for this property.
*/
Option options() default Option.DEFAULT;
/**
* Set this to false such that doc values are not stored separately for this property.
*/
boolean docValues() default true;
/**
* Set a copyTo field.
*/
String copyTo() default "";
/**
* Set to false to disable the field from indexing, it will only be get/set in _source.
*/
boolean enabled() default true;
/**
* Set to false such that norms are not stored.
*/
boolean norms() default true;
/**
* Index options for a property.
*/
enum Option {
/**
* Only index the doc number.
*/
DOCS,
/**
* Doc number and term frequencies are indexed.
*/
FREQS,
/**
* Doc number, term frequencies and term positions are indexed.
*/
POSITIONS,
/**
* Doc number, term frequencies, term positions and start/end offsets are indexed.
*/
OFFSETS,
/**
* Use the default which means analysed string properties use POSITIONS as the default and all other types use DOCS.
*/
DEFAULT
}
}
@@ -10,64 +10,71 @@ import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyOptions;
*/
public class DeployDocPropertyOptions {
private Boolean code;
private static DocPropertyOptions EMPTY = new DocPropertyOptions();
private Boolean sortable;
private Boolean store;
private Float boost;
private String nullValue;
private DocPropertyOptions mapping;
private void createOptions() {
if (mapping == null) {
mapping = new DocPropertyOptions();
}
}
/**
* Read the DocProperty deployment options.
*/
public void setDocProperty(DocProperty doc) {
code = checkDefault(doc.code());
sortable = checkDefault(doc.sortable());
store = checkDefault(doc.store());
boost = checkDefault(doc.boost());
nullValue = checkDefault(doc.nullValue());
createOptions();
mapping.apply(doc);
}
/**
* Read the DocSortable deployment options.
*/
public void setDocSortable(DocSortable doc) {
sortable = Boolean.TRUE;
store = checkDefault(doc.store());
boost = checkDefault(doc.boost());
nullValue = checkDefault(doc.nullValue());
createOptions();
mapping.setSortable(true);
setStore(doc.store());
setBoost(doc.boost());
setNullValue(doc.nullValue());
}
/**
* Read the DocCode deployment options.
*/
public void setDocCode(DocCode doc) {
code = Boolean.TRUE;
store = checkDefault(doc.store());
boost = checkDefault(doc.boost());
nullValue = checkDefault(doc.nullValue());
createOptions();
mapping.setCode(true);
setStore(doc.store());
setBoost(doc.boost());
setNullValue(doc.nullValue());
}
private String checkDefault(String s) {
return "".equals(s) ? null : s;
private void setNullValue(String value) {
if (!value.equals("")) {
mapping.setNullValue(value);
}
}
private Float checkDefault(float boost) {
return (boost == 1) ? null : boost;
private void setBoost(float boost) {
if (boost != 1) {
mapping.setBoost(boost);
}
}
private Boolean checkDefault(boolean store) {
return (store) ? Boolean.TRUE : null;
private void setStore(boolean store) {
if (store) {
mapping.setStore(true);
}
}
/**
* Return the DocPropertyOptions with the collected options.
*/
public DocPropertyOptions create() {
return new DocPropertyOptions(code, sortable, store, boost, nullValue);
return (mapping == null) ? EMPTY : mapping;
}
}
@@ -184,17 +184,17 @@ public class AnnotationFields extends AnnotationParser {
}
}
DocProperty docProperty = get(prop, DocProperty.class);
if (docProperty != null) {
prop.setDocProperty(docProperty);
DocCode docCode = get(prop, DocCode.class);
if (docCode != null) {
prop.setDocCode(docCode);
}
DocSortable docSortable = get(prop, DocSortable.class);
if (docSortable != null) {
prop.setDocSortable(docSortable);
}
DocCode docCode = get(prop, DocCode.class);
if (docCode != null) {
prop.setDocCode(docCode);
DocProperty docProperty = get(prop, DocProperty.class);
if (docProperty != null) {
prop.setDocProperty(docProperty);
}
if (get(prop, DbHstore.class) != null) {
@@ -1,6 +1,7 @@
package com.avaje.ebeanservice.docstore.api.mapping;
import com.avaje.ebean.annotation.DocMapping;
import com.avaje.ebean.annotation.DocProperty;
/**
* Options for mapping a property for document storage.
@@ -17,6 +18,22 @@ public class DocPropertyOptions {
private String nullValue;
private Boolean includeInAll;
private Boolean enabled;
private Boolean norms;
private Boolean docValues;
private String analyzer;
private String searchAnalyzer;
private String copyTo;
private DocProperty.Option options;
/**
* Construct with no values set.
*/
@@ -33,17 +50,14 @@ public class DocPropertyOptions {
this.store = source.store;
this.boost = source.boost;
this.nullValue = source.nullValue;
}
/**
* Construct with options set.
*/
public DocPropertyOptions(Boolean code, Boolean sortable, Boolean store, Float boost, String nullValue) {
this.code = code;
this.sortable = sortable;
this.store = store;
this.boost = boost;
this.nullValue = nullValue;
this.includeInAll = source.includeInAll;
this.analyzer = source.analyzer;
this.searchAnalyzer = source.searchAnalyzer;
this.options = source.options;
this.docValues = source.docValues;
this.norms = source.norms;
this.copyTo = source.copyTo;
this.enabled = source.enabled;
}
public String toString() {
@@ -106,6 +120,77 @@ public class DocPropertyOptions {
this.store = store;
}
public Boolean getIncludeInAll() {
return includeInAll;
}
public void setIncludeInAll(Boolean includeInAll) {
this.includeInAll = includeInAll;
}
public Boolean getDocValues() {
return docValues;
}
public void setDocValues(Boolean docValues) {
this.docValues = docValues;
}
public String getAnalyzer() {
return analyzer;
}
public void setAnalyzer(String analyzer) {
this.analyzer = analyzer;
}
public String getSearchAnalyzer() {
return searchAnalyzer;
}
public void setSearchAnalyzer(String searchAnalyzer) {
this.searchAnalyzer = searchAnalyzer;
}
public String getCopyTo() {
return copyTo;
}
public void setCopyTo(String copyTo) {
this.copyTo = copyTo;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Boolean getNorms() {
return norms;
}
public void setNorms(Boolean norms) {
this.norms = norms;
}
/**
* Return true if the index options is set to a non-default value.
*/
public boolean isOptionsSet() {
return options != null && options != DocProperty.Option.DEFAULT;
}
public DocProperty.Option getOptions() {
return options;
}
public void setOptions(DocProperty.Option options) {
this.options = options;
}
/**
* Create a copy of this such that it can be overridden on a per index basis.
*/
@@ -117,7 +202,15 @@ public class DocPropertyOptions {
* Apply override mapping from the document level or embedded property level.
*/
public void apply(DocMapping docMapping) {
apply(docMapping.options());
}
/**
* Apply the property level mapping options.
*/
public void apply(DocProperty docMapping) {
options = docMapping.options();
if (docMapping.code()) {
code = true;
}
@@ -133,6 +226,27 @@ public class DocPropertyOptions {
if (!"".equals(docMapping.nullValue())) {
nullValue = docMapping.nullValue();
}
if (!docMapping.includeInAll()) {
includeInAll = false;
}
if (!docMapping.docValues()) {
docValues = false;
}
if (!docMapping.enabled()) {
enabled = false;
}
if (!docMapping.norms()) {
norms = false;
}
if (!"".equals(docMapping.analyzer())) {
analyzer = docMapping.analyzer();
}
if (!"".equals(docMapping.searchAnalyzer())) {
searchAnalyzer = docMapping.searchAnalyzer();
}
if (!"".equals(docMapping.copyTo())) {
copyTo = docMapping.copyTo();
}
}
}