Skip to content

Commit

Permalink
Add support for Opensearch requests #267
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Apr 16, 2022
1 parent 5e93b3d commit 7eeb840
Show file tree
Hide file tree
Showing 22 changed files with 2,076 additions and 240 deletions.
23 changes: 19 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<dependency>
<groupId>org.icatproject</groupId>
<artifactId>icat.client</artifactId>
<version>4.11.1</version>
<version>4.11.2-SNAPSHOT</version>
<scope>test</scope>
</dependency>

Expand All @@ -137,12 +137,24 @@
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.1.0</version>
</dependency>
<dependency>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
</dependency>

<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api</artifactId>
<version>2.1.3</version>
</dependency>

<dependency>
<groupId>tech.units</groupId>
<artifactId>indriya</artifactId>
<version>2.1.3</version>
</dependency>

</dependencies>

Expand Down Expand Up @@ -237,6 +249,7 @@
<javax.net.ssl.trustStore>${javax.net.ssl.trustStore}</javax.net.ssl.trustStore>
<luceneUrl>${luceneUrl}</luceneUrl>
<elasticsearchUrl>${elasticsearchUrl}</elasticsearchUrl>
<opensearchUrl>${opensearchUrl}</opensearchUrl>
</systemPropertyVariables>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
Expand All @@ -257,6 +270,7 @@
<searchEngine>${searchEngine}</searchEngine>
<luceneUrl>${luceneUrl}</luceneUrl>
<elasticsearchUrl>${elasticsearchUrl}</elasticsearchUrl>
<opensearchUrl>${opensearchUrl}</opensearchUrl>
</systemPropertyVariables>
</configuration>
<executions>
Expand Down Expand Up @@ -338,6 +352,7 @@
<argument>${searchEngine}</argument>
<argument>${luceneUrl}</argument>
<argument>${elasticsearchUrl}</argument>
<argument>${opensearchUrl}</argument>
</arguments>
</configuration>
<goals>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/icatproject/core/entity/DatafileFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -54,6 +55,8 @@ public void setFacility(Facility facility) {
@Column(name = "VERSION", nullable = false)
private String version;

public static List<String> docFields = Arrays.asList("datafileFormat.name", "datafileFormat.id");

/* Needed for JPA */
public DatafileFormat() {
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/icatproject/core/entity/DatasetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -38,6 +39,8 @@ public class DatasetType extends EntityBaseBean implements Serializable {
@Column(name = "NAME", nullable = false)
private String name;

public static List<String> docFields = Arrays.asList("type.name", "type.id");

/* Needed for JPA */
public DatasetType() {
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/icatproject/core/entity/Facility.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -64,6 +65,8 @@ public class Facility extends EntityBaseBean implements Serializable {
@Comment("A URL associated with this facility")
private String url;

public static List<String> docFields = Arrays.asList("facility.name", "facility.id");

/* Needed for JPA */
public Facility() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -54,6 +55,8 @@ public void setInvestigations(List<Investigation> investigations) {
this.investigations = investigations;
}

public static List<String> docFields = Arrays.asList("type.name", "type.id");

/* Needed for JPA */
public InvestigationType() {
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/icatproject/core/entity/ParameterType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -94,6 +95,8 @@ public class ParameterType extends EntityBaseBean implements Serializable {
@Comment("If ordinary users are allowed to create their own parameter types this indicates that this one has been approved")
private boolean verified;

public static List<String> docFields = Arrays.asList("type.name", "type.units", "type.unitsSI", "numericValueSI", "type.id");

/* Needed for JPA */
public ParameterType() {
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/icatproject/core/entity/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void setType(SampleType type) {

@Override
public void getDoc(JsonGenerator gen) {
SearchApi.encodeString(gen, "sample.name", name);
SearchApi.encodeString(gen, "name", name);
SearchApi.encodeString(gen, "id", id);
SearchApi.encodeString(gen, "investigation.id", investigation.id);
if (type != null) {
Expand All @@ -115,7 +115,7 @@ public void getDoc(JsonGenerator gen) {
* @param prefix String to precede all ambiguous field names.
*/
public void getDoc(JsonGenerator gen, String prefix) {
SearchApi.encodeString(gen, "sample.name", name);
SearchApi.encodeString(gen, prefix + "name", name);
SearchApi.encodeString(gen, prefix + "id", id);
SearchApi.encodeString(gen, prefix + "investigation.id", investigation.id);
if (type != null) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/icatproject/core/entity/SampleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class SampleType extends EntityBaseBean implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "type")
private List<Sample> samples = new ArrayList<>();

public static List<String> docFields = Arrays.asList("sample.type.name", "type.id");

/* Needed for JPA */
public SampleType() {
}
Expand Down Expand Up @@ -89,7 +92,7 @@ public void setSamples(List<Sample> samples) {

@Override
public void getDoc(JsonGenerator gen) {
SearchApi.encodeString(gen, "sample.type.name", name);
SearchApi.encodeString(gen, "type.name", name);
SearchApi.encodeString(gen, "type.id", id);
}

Expand All @@ -104,7 +107,7 @@ public void getDoc(JsonGenerator gen) {
* @param prefix String to precede all ambiguous field names.
*/
public void getDoc(JsonGenerator gen, String prefix) {
SearchApi.encodeString(gen, "sample.type.name", name);
SearchApi.encodeString(gen, prefix + "type.name", name);
SearchApi.encodeString(gen, prefix + "type.id", id);
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/icatproject/core/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.stream.JsonGenerator;
Expand Down Expand Up @@ -54,6 +55,8 @@ public class User extends EntityBaseBean implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Study> studies = new ArrayList<Study>();

public static List<String> docFields = Arrays.asList("user.name", "user.fullName", "user.id");

public User() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public class ElasticsearchApi extends SearchApi {
private final Map<String, Integer> pitMap = new HashMap<>();

public ElasticsearchApi(List<URL> servers) throws IcatException {
super(null);
List<HttpHost> hosts = new ArrayList<HttpHost>();
for (URL server : servers) {
hosts.add(new HttpHost(server.getHost(), server.getPort(), server.getProtocol()));
Expand All @@ -117,7 +118,7 @@ public ElasticsearchApi(List<URL> servers) throws IcatException {
initMappings();
}

private void initMappings() throws IcatException {
public void initMappings() throws IcatException {
try {
client.cluster().putSettings(s -> s.persistent("action.auto_create_index", JsonData.of(false)));
client.putScript(p -> p.id("update_user").script(s -> s
Expand Down Expand Up @@ -189,7 +190,7 @@ public void commit() throws IcatException {
}

@Override
public List<FacetDimension> facetSearch(String target, JsonObject facetQuery, int maxResults, int maxLabels) throws IcatException {
public List<FacetDimension> facetSearch(String target, JsonObject facetQuery, Integer maxResults, int maxLabels) throws IcatException {
// TODO this should be generalised
return null;
// try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,8 +1394,8 @@ public List<ScoredEntityBaseBean> freeTextSearch(String userName, JsonObject jo,
EntityManager manager, String ip, Class<? extends EntityBaseBean> klass) throws IcatException {
long startMillis = log ? System.currentTimeMillis() : 0;
List<ScoredEntityBaseBean> results = new ArrayList<>();
String searchAfter = null;
String lastSearchAfter = null;
JsonValue searchAfter = null;
JsonValue lastSearchAfter = null;
if (searchActive) {
SearchResult lastSearchResult = null;
List<ScoredEntityBaseBean> allResults = Collections.emptyList();
Expand All @@ -1415,7 +1415,7 @@ public List<ScoredEntityBaseBean> freeTextSearch(String userName, JsonObject jo,
if (lastSearchAfter == null) {
break; // If searchAfter is null, we ran out of results so stop here
}
searchAfter = lastSearchAfter.toString();
searchAfter = lastSearchAfter;
} else {
// Have stopped early by reaching the limit, so build a searchAfter document
lastSearchAfter = searchManager.buildSearchAfter(lastBean, sort);
Expand All @@ -1439,12 +1439,12 @@ public List<ScoredEntityBaseBean> freeTextSearch(String userName, JsonObject jo,
return results;
}

public SearchResult freeTextSearchDocs(String userName, JsonObject jo, String searchAfter, int limit, String sort,
public SearchResult freeTextSearchDocs(String userName, JsonObject jo, JsonValue searchAfter, int limit, String sort,
String facets, EntityManager manager, String ip, Class<? extends EntityBaseBean> klass)
throws IcatException {
long startMillis = log ? System.currentTimeMillis() : 0;
List<ScoredEntityBaseBean> results = new ArrayList<>();
String lastSearchAfter = null;
JsonValue lastSearchAfter = null;
List<FacetDimension> dimensions = null;
if (searchActive) {
SearchResult lastSearchResult = null;
Expand All @@ -1466,7 +1466,7 @@ public SearchResult freeTextSearchDocs(String userName, JsonObject jo, String se
if (lastSearchAfter == null) {
break; // If searchAfter is null, we ran out of results so stop here
}
searchAfter = lastSearchAfter.toString();
searchAfter = lastSearchAfter;
} else {
// Have stopped early by reaching the limit, so build a searchAfter document
lastSearchAfter = searchManager.buildSearchAfter(lastBean, sort);
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/org/icatproject/core/manager/LuceneApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.ExecutorService;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
Expand Down Expand Up @@ -66,10 +67,8 @@ private String getTargetPath(JsonObject query) throws IcatException {
return path;
}

URI server;

public LuceneApi(URI server) {
this.server = server;
super(server);
}

public void addNow(String entityName, List<Long> ids, EntityManager manager,
Expand Down Expand Up @@ -110,7 +109,7 @@ public void addNow(String entityName, List<Long> ids, EntityManager manager,
}

@Override
public String buildSearchAfter(ScoredEntityBaseBean lastBean, String sort) throws IcatException {
public JsonObject buildSearchAfter(ScoredEntityBaseBean lastBean, String sort) throws IcatException {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("doc", lastBean.getEngineDocId());
builder.add("shardIndex", -1);
Expand All @@ -132,7 +131,7 @@ public String buildSearchAfter(ScoredEntityBaseBean lastBean, String sort) throw
builder.add("fields", arrayBuilder);
}
}
return builder.build().toString();
return builder.build();
}

@Override
Expand Down Expand Up @@ -164,7 +163,7 @@ public void commit() throws IcatException {
}

@Override
public List<FacetDimension> facetSearch(String target, JsonObject facetQuery, int maxResults, int maxLabels) throws IcatException {
public List<FacetDimension> facetSearch(String target, JsonObject facetQuery, Integer maxResults, int maxLabels) throws IcatException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
URI uri = new URIBuilder(server).setPath(basePath + "/" + target + "/facet")
.setParameter("maxResults", Integer.toString(maxResults))
Expand Down Expand Up @@ -227,14 +226,18 @@ private List<FacetDimension> getFacets(URI uri, CloseableHttpClient httpclient,
}

@Override
public SearchResult getResults(JsonObject query, String searchAfter, int blockSize, String sort,
public SearchResult getResults(JsonObject query, JsonValue searchAfter, Integer blockSize, String sort,
List<String> fields) throws IcatException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
String indexPath = getTargetPath(query);
URI uri = new URIBuilder(server).setPath(basePath + "/" + indexPath)
.setParameter("search_after", searchAfter)
.setParameter("maxResults", Integer.toString(blockSize))
.setParameter("sort", sort).build();
URIBuilder uriBuilder = new URIBuilder(server).setPath(basePath + "/" + indexPath)
.setParameter("maxResults", Integer.toString(blockSize));
if (searchAfter != null) {
uriBuilder.setParameter("search_after", searchAfter.toString());
}
if (sort != null) {
uriBuilder.setParameter("sort", sort);
}
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
objectBuilder.add("query", query);
if (fields != null && fields.size() > 0) {
Expand All @@ -243,6 +246,7 @@ public SearchResult getResults(JsonObject query, String searchAfter, int blockSi
objectBuilder.add("fields", arrayBuilder.build());
}
String queryString = objectBuilder.build().toString();
URI uri = uriBuilder.build();
logger.trace("Making call {} with queryString {}", uri, queryString);
return getResults(uri, httpclient, queryString);

Expand Down Expand Up @@ -278,7 +282,7 @@ private SearchResult getResults(URI uri, CloseableHttpClient httpclient, String
results.add(new ScoredEntityBaseBean(luceneDocId, score, source));
}
if (responseObject.containsKey("search_after")) {
lsr.setSearchAfter(responseObject.getJsonObject("search_after").toString());
lsr.setSearchAfter(responseObject.getJsonObject("search_after"));
}
}
}
Expand Down
Loading

0 comments on commit 7eeb840

Please sign in to comment.