Skip to content

Commit

Permalink
Replace abstract methods in SearchApi #267
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Mar 24, 2022
1 parent 17cd54d commit bfb7d7b
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/icatproject/core/entity/Datafile.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void getDoc(JsonGenerator gen, SearchApi searchApi) {
} else {
searchApi.encodeStringField(gen, "date", modTime);
}
searchApi.encodeStoredId(gen, id);
searchApi.encodeStringField(gen, "id", id, true);
searchApi.encodeStringField(gen, "dataset", dataset.id);
searchApi.encodeStringField(gen, "investigation", dataset.getInvestigation().id);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/icatproject/core/entity/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void getDoc(JsonGenerator gen, SearchApi searchApi) {
} else {
searchApi.encodeStringField(gen, "endDate", modTime);
}
searchApi.encodeStoredId(gen, id);
searchApi.encodeStringField(gen, "id", id, true);

searchApi.encodeSortedDocValuesField(gen, "id", id);

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/icatproject/core/entity/Investigation.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,19 @@ public void getDoc(JsonGenerator gen, SearchApi searchApi) {


samples.forEach((sample) -> {
searchApi.encodeSortedSetDocValuesFacetField(gen, "sampleName", sample.getName());
// searchApi.encodeSortedSetDocValuesFacetField(gen, "sampleName", sample.getName());
searchApi.encodeTextField(gen, "sampleText", sample.getDocText());
});

for (InvestigationParameter parameter : parameters) {
ParameterType type = parameter.type;
String parameterName = type.getName();
String parameterUnits = type.getUnits();
searchApi.encodeSortedSetDocValuesFacetField(gen, "parameterName", parameterName);
// searchApi.encodeSortedSetDocValuesFacetField(gen, "parameterName", parameterName);
searchApi.encodeStringField(gen, "parameterUnits", parameterUnits);
// TODO make all value types facetable...
if (type.getValueType() == ParameterValueType.STRING) {
searchApi.encodeSortedSetDocValuesFacetField(gen, "parameterStringValue", parameter.getStringValue());
// searchApi.encodeSortedSetDocValuesFacetField(gen, "parameterStringValue", parameter.getStringValue());
} else if (type.getValueType() == ParameterValueType.DATE_AND_TIME) {
searchApi.encodeStringField(gen, "parameterDateValue", parameter.getDateTimeValue());
} else if (type.getValueType() == ParameterValueType.NUMERIC) {
Expand All @@ -312,6 +312,6 @@ public void getDoc(JsonGenerator gen, SearchApi searchApi) {

searchApi.encodeSortedDocValuesField(gen, "id", id);

searchApi.encodeStoredId(gen, id);
searchApi.encodeStringField(gen, "id", id, true);
}
}
56 changes: 0 additions & 56 deletions src/main/java/org/icatproject/core/manager/ElasticsearchApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.icatproject.core.manager;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
Expand All @@ -11,7 +10,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import javax.json.Json;
import javax.json.JsonArray;
Expand All @@ -20,13 +18,11 @@
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.json.stream.JsonGenerator;
import javax.persistence.EntityManager;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.icatproject.core.IcatException;
import org.icatproject.core.IcatException.IcatExceptionType;
import org.icatproject.core.entity.EntityBaseBean;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
Expand Down Expand Up @@ -158,35 +154,6 @@ private void initMappings() throws IcatException {
}
}

@Override
public void addNow(String entityName, List<Long> ids, EntityManager manager,
Class<? extends EntityBaseBean> klass, ExecutorService getBeanDocExecutor)
throws IcatException, IOException {
// getBeanDocExecutor is not used for the Elasticsearch implementation, but is
// required for the @Override

// TODO Change this string building fake JSON by hand
StringBuilder sb = new StringBuilder();
sb.append("[");
for (Long id : ids) {
EntityBaseBean bean = (EntityBaseBean) manager.find(klass, id);
if (bean != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (JsonGenerator gen = Json.createGenerator(baos)) {
gen.writeStartArray(); // Document fields are wrapped in an array
bean.getDoc(gen, this); // Fields
gen.writeEnd();
}
if (sb.length() != 1) {
sb.append(',');
}
sb.append("[\"").append(entityName).append("\",null,").append(baos.toString()).append(']');
}
}
sb.append("]");
modify(sb.toString());
}

@Override
public void clear() throws IcatException {
try {
Expand Down Expand Up @@ -216,29 +183,6 @@ public void encodeStringField(JsonGenerator gen, String name, Date value) {
gen.writeStartObject().write(name, timeString).writeEnd();
}

public void encodeDoublePoint(JsonGenerator gen, String name, Double value) {
gen.writeStartObject().write(name, value).writeEnd();
}

public void encodeSortedSetDocValuesFacetField(JsonGenerator gen, String name, String value) {
gen.writeStartObject().write(name, value).writeEnd();

}

public void encodeStringField(JsonGenerator gen, String name, Long value) {
gen.writeStartObject().write(name, value).writeEnd();
}

public void encodeStringField(JsonGenerator gen, String name, String value) {
gen.writeStartObject().write(name, value).writeEnd();
}

public void encodeTextField(JsonGenerator gen, String name, String value) {
if (value != null) {
gen.writeStartObject().write(name, value).writeEnd();
}
}

@Override
public void freeSearcher(String uid)
throws IcatException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public ElasticsearchDocument(JsonArray jsonArray) throws IcatException {
} else if (fieldEntry.getKey().equals("text")) {
text = fieldObject.getString("text");
} else if (fieldEntry.getKey().equals("date")) {
date = SearchApi.dec(fieldObject.getString("date"));
date = SearchApi.decodeDate(fieldObject.getString("date"));
} else if (fieldEntry.getKey().equals("startDate")) {
startDate = SearchApi.dec(fieldObject.getString("startDate"));
startDate = SearchApi.decodeDate(fieldObject.getString("startDate"));
} else if (fieldEntry.getKey().equals("endDate")) {
endDate = SearchApi.dec(fieldObject.getString("endDate"));
endDate = SearchApi.decodeDate(fieldObject.getString("endDate"));
} else if (fieldEntry.getKey().equals("user.name")) {
userName.add(fieldObject.getString("user.name"));
} else if (fieldEntry.getKey().equals("user.fullName")) {
Expand All @@ -100,7 +100,7 @@ public ElasticsearchDocument(JsonArray jsonArray) throws IcatException {
} else if (fieldEntry.getKey().equals("parameter.stringValue")) {
parameterStringValue.add(fieldObject.getString("parameter.stringValue"));
} else if (fieldEntry.getKey().equals("parameter.dateValue")) {
parameterDateValue.add(SearchApi.dec(fieldObject.getString("parameter.dateValue")));
parameterDateValue.add(SearchApi.decodeDate(fieldObject.getString("parameter.dateValue")));
} else if (fieldEntry.getKey().equals("parameter.numericValue")) {
parameterNumericValue
.add(fieldObject.getJsonNumber("parameter.numericValue").doubleValue());
Expand Down Expand Up @@ -138,7 +138,7 @@ public ElasticsearchDocument(JsonArray jsonArray, String index, String parentInd
} else if (fieldEntry.getKey().equals("parameterStringValue")) {
parameterStringValue.add(fieldObject.getString("parameterStringValue"));
} else if (fieldEntry.getKey().equals("parameterDateValue")) {
parameterDateValue.add(SearchApi.dec(fieldObject.getString("parameterDateValue")));
parameterDateValue.add(SearchApi.decodeDate(fieldObject.getString("parameterDateValue")));
} else if (fieldEntry.getKey().equals("parameterNumericValue")) {
parameterNumericValue
.add(fieldObject.getJsonNumber("parameterNumericValue").doubleValue());
Expand Down
38 changes: 13 additions & 25 deletions src/main/java/org/icatproject/core/manager/LuceneApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -67,45 +66,35 @@ private String getTargetPath(JsonObject query) throws IcatException {
// TODO this method of encoding an entity as an array of 3 key objects that represent single field each
// is something that should be streamlined, but would require changes to icat.lucene

@Override
public void encodeSortedDocValuesField(JsonGenerator gen, String name, Long value) {
gen.writeStartObject().write("type", "SortedDocValuesField").write("name", name).write("value", value)
.writeEnd();
}

public void encodeStoredId(JsonGenerator gen, Long id) {
gen.writeStartObject().write("type", "StringField").write("name", "id").write("value", Long.toString(id))
.write("store", true).writeEnd();
}

public void encodeStringField(JsonGenerator gen, String name, Date value) {
String timeString;
synchronized (df) {
timeString = df.format(value);
}
gen.writeStartObject().write("type", "StringField").write("name", name).write("value", timeString).writeEnd();
}

@Override
public void encodeDoublePoint(JsonGenerator gen, String name, Double value) {
gen.writeStartObject().write("type", "DoublePoint").write("name", name).write("value", value)
.write("store", true).writeEnd();
}

public void encodeSortedSetDocValuesFacetField(JsonGenerator gen, String name, String value) {
gen.writeStartObject().write("type", "SortedSetDocValuesFacetField").write("name", name).write("value", value)
.writeEnd();

}
// public void encodeSortedSetDocValuesFacetField(JsonGenerator gen, String name, String value) {
// gen.writeStartObject().write("type", "SortedSetDocValuesFacetField").write("name", name).write("value", value)
// .writeEnd();

public void encodeStringField(JsonGenerator gen, String name, Long value) {
gen.writeStartObject().write("type", "StringField").write("name", name).write("value", Long.toString(value))
.writeEnd();
}
// }

@Override
public void encodeStringField(JsonGenerator gen, String name, String value) {
gen.writeStartObject().write("type", "StringField").write("name", name).write("value", value).writeEnd();
}

@Override
public void encodeStringField(JsonGenerator gen, String name, Long value, Boolean store) {
gen.writeStartObject().write("type", "StringField").write("name", name).write("value", Long.toString(value)).write("store", store).writeEnd();
}

@Override
public void encodeTextField(JsonGenerator gen, String name, String value) {
if (value != null) {
gen.writeStartObject().write("type", "TextField").write("name", name).write("value", value).writeEnd();
Expand All @@ -118,9 +107,8 @@ public LuceneApi(URI server) {
this.server = server;
}

@Override
public void addNow(String entityName, List<Long> ids, EntityManager manager,
Class<? extends EntityBaseBean> klass, ExecutorService getBeanDocExecutor) throws Exception {
Class<? extends EntityBaseBean> klass, ExecutorService getBeanDocExecutor) throws IcatException, IOException, URISyntaxException {
URI uri = new URIBuilder(server).setPath(basePath + "/addNow/" + entityName)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public class ScoredEntityBaseBean {
private long entityBaseBeanId;
private float score;

public ScoredEntityBaseBean(String id, double score) {
this.entityBaseBeanId = Long.parseLong(id);
this.score = (float) score;
}

public ScoredEntityBaseBean(long id, float score) {
this.entityBaseBeanId = id;
this.score = score;
Expand Down
Loading

0 comments on commit bfb7d7b

Please sign in to comment.