#304 - ElasticSearch integration part 1 / doc store integration

This commit is contained in:
Robin Bygrave committed 2016-03-01 15:36:44 +13:00
1 parent cb1fe297d6
commit 8d3b4a0407
209 files changed
+6736 -573

No files matched your search

@@ -10,6 +10,8 @@ import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebean.event.readaudit.ReadEvent;
import com.avaje.ebean.plugin.BeanType;
import com.avaje.ebean.FetchPath;
import com.avaje.ebean.text.json.JsonContext;
import com.avaje.ebeaninternal.api.BindParams;
import com.avaje.ebeaninternal.api.CQueryPlanKey;
import com.avaje.ebeaninternal.api.HashQuery;
@@ -25,11 +27,15 @@ import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DRawSqlSelect;
import com.avaje.ebeaninternal.server.deploy.DeployNamedQuery;
import com.avaje.ebeaninternal.server.deploy.TableJoin;
import com.avaje.ebeaninternal.server.expression.DefaultExpressionList;
import com.avaje.ebeaninternal.server.expression.ElasticExpressionContext;
import com.avaje.ebeaninternal.server.expression.SimpleExpression;
import com.avaje.ebeaninternal.server.query.CancelableQuery;
import com.avaje.ebeaninternal.server.expression.DefaultExpressionList;
import com.fasterxml.jackson.core.JsonGenerator;
import javax.persistence.PersistenceException;
import java.io.IOException;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
@@ -42,29 +48,27 @@ import java.util.Set;
*/
public class DefaultOrmQuery<T> implements SpiQuery<T> {
private static final long serialVersionUID = 6838006264714672460L;
private final Class<T> beanType;
private transient final EbeanServer server;
private final BeanDescriptor<T> beanDescriptor;
private transient BeanCollectionTouched beanCollectionTouched;
private final EbeanServer server;
private transient final ExpressionFactory expressionFactory;
private BeanCollectionTouched beanCollectionTouched;
private final ExpressionFactory expressionFactory;
/**
* For lazy loading of ManyToMany we need to add a join to the intersection table. This is that
* join to the intersection table.
*/
private transient TableJoin includeTableJoin;
private TableJoin includeTableJoin;
private transient ProfilingListener profilingListener;
private transient BeanDescriptor<?> beanDescriptor;
private ProfilingListener profilingListener;
private boolean cancelled;
private transient CancelableQuery cancelableQuery;
private CancelableQuery cancelableQuery;
/**
* The name of the query.
@@ -235,14 +239,17 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
*/
private CQueryPlanKey queryPlanKey;
private transient PersistenceContext persistenceContext;
private PersistenceContext persistenceContext;
private ManyWhereJoins manyWhereJoins;
private RawSql rawSql;
public DefaultOrmQuery(Class<T> beanType, EbeanServer server, ExpressionFactory expressionFactory, String query) {
this.beanType = beanType;
private boolean useDocStore;
public DefaultOrmQuery(BeanDescriptor<T> desc, EbeanServer server, ExpressionFactory expressionFactory, String query) {
this.beanDescriptor = desc;
this.beanType = desc.getBeanType();
this.server = server;
this.expressionFactory = expressionFactory;
this.detail = new OrmQueryDetail();
@@ -255,10 +262,11 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
/**
* Additional supply a query which is parsed.
*/
public DefaultOrmQuery(Class<T> beanType, EbeanServer server, ExpressionFactory expressionFactory,
public DefaultOrmQuery(BeanDescriptor<T> desc, EbeanServer server, ExpressionFactory expressionFactory,
DeployNamedQuery namedQuery) throws PersistenceException {
this.beanType = beanType;
this.beanDescriptor = desc;
this.beanType = desc.getBeanType();
this.server = server;
this.expressionFactory = expressionFactory;
this.detail = new OrmQueryDetail();
@@ -282,6 +290,88 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
}
public String asElasticQuery() {
StringWriter sw = new StringWriter(200);
JsonContext json = server.json();
JsonGenerator generator = json.createGenerator(sw);
BeanType<T> beanType = server.getPluginApi().getBeanType(this.beanType);
ElasticExpressionContext context = new ElasticExpressionContext(generator, beanType);
try {
writeElastic(context);
context.flush();
return sw.toString();
} catch (IOException e) {
throw new PersistenceIOException(e);
}
}
public void writeElastic(ElasticExpressionContext context) throws IOException {
JsonGenerator json = context.json();
json.writeStartObject();
if (firstRow > 0) {
json.writeNumberField("from", firstRow);
}
if (maxRows > 0) {
json.writeNumberField("size", maxRows);
}
detail.writeElastic(context);
context.writeOrderBy(orderBy);
json.writeFieldName("query");
json.writeStartObject();
SpiExpression idEquals = null;
if (id != null) {
idEquals = (SpiExpression)expressionFactory.idEq(id);
}
boolean hasWhere = (whereExpressions != null && !whereExpressions.isEmpty());
if (idEquals != null || hasWhere) {
json.writeFieldName("filtered");
json.writeStartObject();
json.writeFieldName("filter");
if (hasWhere) {
whereExpressions.writeElastic(context, idEquals);
} else {
idEquals.writeElastic(context);
}
json.writeEndObject();
} else {
json.writeObjectFieldStart("match_all");
json.writeEndObject();
}
json.writeEndObject();
json.writeEndObject();
}
@Override
public BeanDescriptor<T> getBeanDescriptor() {
return beanDescriptor;
}
@Override
public boolean isAutoTunable() {
return beanDescriptor.isAutoTunable() && !isSqlSelect();
}
@Override
public Query<T> setUseDocStore(boolean useDocStore) {
this.useDocStore = useDocStore;
return this;
}
@Override
public boolean isUseDocStore() {
return useDocStore;
}
@Override
public Query<T> apply(FetchPath fetchPath) {
fetchPath.apply(this);
@@ -337,13 +427,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return this;
}
/**
* Set the BeanDescriptor for the root type of this query.
*/
public void setBeanDescriptor(BeanDescriptor<?> beanDescriptor) {
this.beanDescriptor = beanDescriptor;
}
public RawSql getRawSql() {
return rawSql;
}
@@ -559,7 +642,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
public DefaultOrmQuery<T> copy(EbeanServer server) {
DefaultOrmQuery<T> copy = new DefaultOrmQuery<T>(beanType, server, expressionFactory, (String) null);
DefaultOrmQuery<T> copy = new DefaultOrmQuery<T>(beanDescriptor, server, expressionFactory, (String) null);
copy.name = name;
copy.includeTableJoin = includeTableJoin;
copy.profilingListener = profilingListener;
@@ -669,7 +752,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
@Override
public BeanPropertyAssocMany<?> getLazyLoadForParentsProperty() {
public BeanPropertyAssocMany<?> getLazyLoadMany() {
return lazyLoadForParentsProperty;
}
@@ -6,9 +6,12 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import com.avaje.ebeaninternal.server.expression.ElasticExpressionContext;
import com.avaje.ebeaninternal.server.query.SplitName;
import com.fasterxml.jackson.core.JsonGenerator;
import javax.persistence.PersistenceException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
@@ -498,4 +501,67 @@ public class OrmQueryDetail implements Serializable {
public Set<String> getFetchPaths() {
return fetchPaths.keySet();
}
/**
* Write the Elastic search source include and fields if necessary.
* <p>
* Fetch all property is put into includes.
* Fetch on 'many' path is put into includes.
* Fetch on 'one' paths and root path are put into fields.
* </p>
*/
public void writeElastic(ElasticExpressionContext context) throws IOException {
Set<String> includes = new LinkedHashSet<String>();
Set<String> fields = new LinkedHashSet<String>();
for (Map.Entry<String, OrmQueryProperties> entry : fetchPaths.entrySet()) {
String path = entry.getKey();
OrmQueryProperties value = entry.getValue();
if (value.allProperties()) {
includes.add(path + ".*");
} else if (context.containsMany(path)) {
for (String propName : value.getIncluded()) {
includes.add(path + "." + propName);
}
} else {
for (String propName : value.getIncluded()) {
fields.add(path + "." + propName);
}
}
}
if (hasSelectClause()) {
Set<String> included = baseProps.getIncluded();
if (included != null) {
for (String propName : included) {
fields.add(propName);
}
}
}
if (!includes.isEmpty()) {
JsonGenerator json = context.json();
json.writeFieldName("_source");
json.writeStartObject();
json.writeFieldName("include");
json.writeStartArray();
for (String propName : includes) {
json.writeString(propName);
}
json.writeEndArray();
json.writeEndObject();
}
if (!fields.isEmpty()) {
JsonGenerator json = context.json();
json.writeFieldName("fields");
json.writeStartArray();
for (String propName : fields) {
json.writeString(propName);
}
json.writeEndArray();
}
}
}