#589 - ElasticSearch - refactor to DocQueryContext

This commit is contained in:
Robin Bygrave committed 2016-03-08 00:00:02 +13:00
1 parent da8766533f
commit 37b86f2191
32 files changed
+155 -788

No files matched your search

@@ -10,8 +10,6 @@ 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;
@@ -27,15 +25,11 @@ 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.ElasticExpressionContext;
import com.avaje.ebeaninternal.server.expression.DefaultExpressionList;
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;
@@ -290,64 +284,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
}
}
public String asElasticQuery() {
JsonContext json = server.json();
ElasticExpressionContext context = new ElasticExpressionContext(json, beanDescriptor);
try {
writeElastic(context);
generatedSql = context.flush();
return generatedSql;
} 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;
@@ -6,12 +6,9 @@ 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;
@@ -440,7 +437,7 @@ public class OrmQueryDetail implements Serializable {
}
public boolean hasSelectClause() {
return (baseProps.hasSelectClause());
return baseProps.hasSelectClause();
}
/**
@@ -503,65 +500,9 @@ public class OrmQueryDetail implements Serializable {
}
/**
* 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>
* Return the underlying fetch path entries.
*/
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();
}
public Set<Map.Entry<String, OrmQueryProperties>> entries() {
return fetchPaths.entrySet();
}
}
@@ -373,7 +373,7 @@ public class OrmQueryProperties implements Serializable {
/**
* Return the property set.
*/
protected Set<String> getIncluded() {
public Set<String> getIncluded() {
return included;
}