Skip to content

Commit

Permalink
refactor getParameterVocabSuggestions method and its usages
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyengit committed Aug 23, 2024
1 parent 23aab33 commit bd8f670
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COPY ./server/target/ogcapi-java-server-*-exec.jar app.jar
ENTRYPOINT [\
"java",\
"-Delasticsearch.index.name=${INDEX_NAME}",\
"-Delasticsearch.search_as_you_type.vocabs_index.name=${VOCABS_INDEX_NAME}",\
"-Delasticsearch.vocabs_index.name=${VOCABS_INDEX_NAME}",\
"-Dapi.host=${HOST}:${PORT}",\
"-Dserver.port=${PORT}",\
"-Delasticsearch.serverUrl=${ELASTIC_URL}",\
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package au.org.aodn.ogcapi.server.core.service;

import au.org.aodn.ogcapi.server.core.model.ParameterVocabSuggestDTO;
import au.org.aodn.ogcapi.server.core.model.ParameterVocabModel;
import au.org.aodn.ogcapi.server.core.model.RecordSuggestDTO;
import au.org.aodn.ogcapi.server.core.model.enumeration.*;
import au.org.aodn.ogcapi.server.core.parser.CQLToElasticFilterFactory;
Expand All @@ -17,6 +17,7 @@
import co.elastic.clients.elasticsearch.core.search_mvt.GridType;
import co.elastic.clients.transport.endpoints.BinaryResponse;
import co.elastic.clients.util.ObjectBuilder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.geotools.filter.text.commons.CompilerUtil;
Expand Down Expand Up @@ -46,20 +47,14 @@ public class ElasticSearch extends ElasticSearchBase implements Search {
@Value("${elasticsearch.search_as_you_type.record_suggest.fields}")
protected String[] searchAsYouTypeEnabledFields;

/*
* this secondLevelParameterVocabSuggestFilters for accessing the search_as_you_type "label" field
* of the second level vocabs of the parameter_vocabs field from the vocabs_index index will never be changed unless the schema is changed,
* or the vocabs_index index is no longer be used
*/
protected Query secondLevelParameterVocabSuggestFilters = Query.of(q -> q.bool(b -> b.filter(f -> f.nested(n -> n.path("broader")
.query(qq -> qq.exists(e -> e.field("broader")))))));
@Value("${elasticsearch.vocabs_index.name}")
protected String vocabsIndexName;

// TODO: field is here
@Value("${elasticsearch.search_as_you_type.vocabs_index.name}")
protected String secondLevelParameterVocabSuggestField;
@Value("${elasticsearch.search_as_you_type.parameter_vocab.path}")
protected String parameterVocabsPath;

@Value("${elasticsearch.search_as_you_type.vocabs_index.name}")
protected String vocabsIndexName;
@Value("${elasticsearch.search_as_you_type.parameter_vocab.field}")
protected String parameterVocabsField;

public ElasticSearch(ElasticsearchClient client,
ObjectMapper mapper,
Expand Down Expand Up @@ -146,20 +141,28 @@ this query uses AND operator for the parameter vocabs (e.g "wave" AND "temperatu
return response.hits().hits();
}

protected List<Hit<ParameterVocabSuggestDTO>> getParameterVocabSuggestions(String input) throws IOException {
protected List<Hit<JsonNode>> getParameterVocabSuggestions(String input) throws IOException {
// create query
Query secondLevelParameterVocabSuggestQuery = this.generateSearchAsYouTypeQuery(input, secondLevelParameterVocabSuggestField);
Query secondLevelParameterVocabSuggestQuery = this.generateSearchAsYouTypeQuery(input, parameterVocabsPath + "." + parameterVocabsField);

/*
* this secondLevelParameterVocabSuggestFilters for accessing the search_as_you_type "label" field
* of the second level vocabs of the parameter_vocabs field from the vocabs_index index will never be changed unless the schema is changed,
* or the vocabs_index index is no longer be used
*/
Query secondLevelParameterVocabSuggestFilters = Query.of(q -> q.bool(b -> b.filter(f -> f.nested(n -> n.path(parameterVocabsPath + ".broader")
.query(qq -> qq.exists(e -> e.field(parameterVocabsPath + ".broader")))))));

// create request
SearchRequest searchRequest = this.buildSearchAsYouTypeRequest(
List.of("label"),
List.of(parameterVocabsPath + "." + parameterVocabsField),
vocabsIndexName,
List.of(secondLevelParameterVocabSuggestQuery),
List.of(secondLevelParameterVocabSuggestFilters));

// execute
log.info("getParameterVocabSuggestions | Elastic search payload {}", searchRequest.toString());
SearchResponse<ParameterVocabSuggestDTO> response = esClient.search(searchRequest, ParameterVocabSuggestDTO.class);
SearchResponse<JsonNode> response = esClient.search(searchRequest, JsonNode.class);
log.info("getParameterVocabSuggestions | Elastic search response {}", response);

// return
Expand All @@ -169,9 +172,10 @@ protected List<Hit<ParameterVocabSuggestDTO>> getParameterVocabSuggestions(Strin
public ResponseEntity<Map<String, ?>> getAutocompleteSuggestions(String input, String cql, CQLCrsType coor) throws IOException, CQLException {
// extract parameter vocab suggestions
Set<String> parameterVocabSuggestions = new HashSet<>();
for (Hit<ParameterVocabSuggestDTO> item : this.getParameterVocabSuggestions(input)) {
if (item.source() != null) {
parameterVocabSuggestions.add(item.source().getLabel());
for (Hit<JsonNode> item : this.getParameterVocabSuggestions(input)) {
if (item.source() != null && item.source().get(parameterVocabsPath) != null) {
ParameterVocabModel parameterVocab = mapper.readValue(item.source().get(parameterVocabsPath).toString(), ParameterVocabModel.class);
parameterVocabSuggestions.add(parameterVocab.getLabel());
}
}

Expand Down
7 changes: 5 additions & 2 deletions server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ server:
elasticsearch:
index:
name: dev_portal_records
vocabs_index:
name: vocabs_index
serverUrl: http://localhost:9200
apiKey: <sample-api-key>
search_as_you_type:
record_suggest:
path: record_suggest
fields: abstract_phrases # if you have multiple search_as_you_type fields, separate them with commas e.g title, description
vocabs_index:
name: vocabs_index
parameter_vocab:
path: parameter_vocab
field: label

api:
host: http://localhost:${server.port}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.org.aodn.ogcapi.server;

import au.org.aodn.ogcapi.server.core.model.ParameterVocabModel;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
Expand All @@ -23,8 +24,10 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -48,7 +51,7 @@ public class BaseTestClass {
@Value("${elasticsearch.index.name}")
protected String record_index_name;

@Value("${elasticsearch.search_as_you_type.vocabs_index.name}")
@Value("${elasticsearch.vocabs_index.name}")
protected String vocabs_index_name;

protected Logger logger = LoggerFactory.getLogger(BaseTestClass.class);
Expand Down
8 changes: 6 additions & 2 deletions server/src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ elasticsearch:
name: testing_index
pageSize: 4

vocabs_index:
name: test_vocabs_index

search_as_you_type:
record_suggest:
path: record_suggest
fields: abstract_phrases # if you have multiple search_as_you_type fields, separate them with commas e.g title, description
vocabs_index:
name: test_vocabs_index
parameter_vocab:
path: parameter_vocab
field: label
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"end": "2020-12-02T13:00:00Z"
}
],
"discovery_categories": [
"parameter_vocabs": [
"wave"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@
"end": "2023-11-27T22:11:44Z"
}
],
"discovery_categories": [
"parameter_vocabs": [
"chlorophyll",
"ocean biota",
"temperature"
Expand Down

0 comments on commit bd8f670

Please sign in to comment.