Skip to content

Commit

Permalink
Merge pull request #117 from jacomago/upgrade-elastic
Browse files Browse the repository at this point in the history
Update elastic search version
  • Loading branch information
jacomago authored Mar 28, 2024
2 parents c06ae6b + c771291 commit dd5d15f
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 336 deletions.
2 changes: 1 addition & 1 deletion docker-compose-integrationtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ services:
retries: 5

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.2.0
image: docker.elastic.co/elasticsearch/elasticsearch:8.11.2
hostname: elasticsearch-cf
networks:
- channelfinder-net
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
java -jar /channelfinder/ChannelFinder-4.7.2.jar"
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.2.0
image: docker.elastic.co/elasticsearch/elasticsearch:8.11.2
hostname: elasticsearch-cf
networks:
- channelfinder-net
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot-version>2.7.3</spring.boot-version>
<elasticsearch.version>8.2.0</elasticsearch.version>
<elasticsearch.version>8.11.2</elasticsearch.version>
<junit-jupiter.version>5.10.0</junit-jupiter.version>
<skipITs>true</skipITs>
<skipITCoverage>true</skipITCoverage>
Expand Down Expand Up @@ -100,7 +100,7 @@
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.2.0</version>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/org/phoebus/channelfinder/ChannelRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch._types.FieldSort;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.Refresh;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.DisMaxQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.NestedQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.WildcardQuery;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.BulkResponse;
Expand Down Expand Up @@ -66,7 +68,7 @@ public class ChannelRepository implements CrudRepository<Channel, String> {
@Autowired
@Qualifier("indexClient")
ElasticsearchClient client;

final ObjectMapper objectMapper = new ObjectMapper()
.addMixIn(Tag.class, Tag.OnlyTag.class)
.addMixIn(Property.class, Property.OnlyProperty.class);
Expand Down Expand Up @@ -422,7 +424,7 @@ public SearchResult search(MultiValueMap<String, String> searchParameters) {
.size(finalSize)
.trackTotalHits(builder -> builder.enabled(builtQuery.trackTotalHits))
.sort(SortOptions.of(o -> o.field(FieldSort.of(f -> f.field("name")))));
builtQuery.searchAfter.ifPresent(searchBuilder::searchAfter);
builtQuery.searchAfter.ifPresent(s -> searchBuilder.searchAfter(FieldValue.of(s)));

SearchResponse<Channel> response = client.search(searchBuilder.build(),
Channel.class
Expand All @@ -448,7 +450,7 @@ private BuiltQuery getBuiltQuery(MultiValueMap<String, String> searchParameters)
int from = 0;
boolean trackTotalHits = false;
Optional<String> searchAfter = Optional.empty();
String valueSplitPattern = "[\\|,;]";
String valueSplitPattern = "[|,;]";
for (Map.Entry<String, List<String>> parameter : searchParameters.entrySet()) {
String key = parameter.getKey().trim();
boolean isNot = key.endsWith("!");
Expand All @@ -460,7 +462,7 @@ private BuiltQuery getBuiltQuery(MultiValueMap<String, String> searchParameters)
for (String value : parameter.getValue()) {
DisMaxQuery.Builder nameQuery = new DisMaxQuery.Builder();
for (String pattern : value.split(valueSplitPattern)) {
nameQuery.queries(WildcardQuery.of(w -> w.field("name").caseInsensitive(true).value(pattern.trim()))._toQuery());
nameQuery.queries(getSingleValueQuery("name", pattern.trim()));
}
boolQuery.must(nameQuery.build()._toQuery());
}
Expand All @@ -471,7 +473,7 @@ private BuiltQuery getBuiltQuery(MultiValueMap<String, String> searchParameters)
for (String pattern : value.split(valueSplitPattern)) {
tagQuery.queries(
NestedQuery.of(n -> n.path("tags").query(
WildcardQuery.of(w -> w.field("tags.name").caseInsensitive(true).value(pattern.trim()))._toQuery()))._toQuery());
getSingleValueQuery("tags.name", pattern.trim())))._toQuery());
}
if (isNot) {
boolQuery.mustNot(tagQuery.build()._toQuery());
Expand Down Expand Up @@ -510,11 +512,11 @@ private BuiltQuery getBuiltQuery(MultiValueMap<String, String> searchParameters)
String finalKey = key;
BoolQuery bq;
if (isNot) {
bq = BoolQuery.of(p -> p.must(WildcardQuery.of(name -> name.field("properties.name").caseInsensitive(true).value(finalKey))._toQuery())
.mustNot(WildcardQuery.of(val -> val.field("properties.value").caseInsensitive(true).value(pattern.trim()))._toQuery()));
bq = BoolQuery.of(p -> p.must(getSingleValueQuery("properties.name", finalKey))
.mustNot(getSingleValueQuery("properties.value", pattern.trim())));
} else {
bq = BoolQuery.of(p -> p.must(WildcardQuery.of(name -> name.field("properties.name").caseInsensitive(true).value(finalKey))._toQuery())
.must(WildcardQuery.of(val -> val.field("properties.value").caseInsensitive(true).value(pattern.trim()))._toQuery()));
bq = BoolQuery.of(p -> p.must(getSingleValueQuery("properties.name", finalKey))
.must(getSingleValueQuery("properties.value", pattern.trim())));
}
propertyQuery.queries(
NestedQuery.of(n -> n.path("properties").query(bq._toQuery()))._toQuery()
Expand All @@ -528,6 +530,10 @@ private BuiltQuery getBuiltQuery(MultiValueMap<String, String> searchParameters)
return new BuiltQuery(boolQuery, size, from, searchAfter, trackTotalHits);
}

private static Query getSingleValueQuery(String name, String pattern) {
return WildcardQuery.of(w -> w.field(name).caseInsensitive(true).value(pattern))._toQuery();
}

private static class BuiltQuery {
public final BoolQuery.Builder boolQuery;
public final Integer size;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/phoebus/channelfinder/ChannelScroll.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;

import co.elastic.clients.elasticsearch._types.FieldSort;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.query_dsl.*;
import co.elastic.clients.elasticsearch.core.SearchRequest;
Expand Down Expand Up @@ -83,14 +84,15 @@ public Scroll query(@PathVariable("scrollId") String scrollId, @RequestParam Mul
* The query result is sorted based on the channel name ~size - The number of
* channels to be returned ~from - The starting index of the channel list
*
* TODO combine with ChannelRepository code.
* @param scrollId scroll ID
* @param searchParameters - search parameters for scrolling searches
* @return search scroll
*/
public Scroll search(String scrollId, MultiValueMap<String, String> searchParameters) {
BoolQuery.Builder boolQuery = new BoolQuery.Builder();
Integer size = esService.getES_QUERY_SIZE();
Integer from = 0;
int size = esService.getES_QUERY_SIZE();
int from = 0;

for (Map.Entry<String, List<String>> parameter : searchParameters.entrySet()) {
String key = parameter.getKey().trim();
Expand Down Expand Up @@ -127,13 +129,13 @@ public Scroll search(String scrollId, MultiValueMap<String, String> searchParame
case "~size":
Optional<String> maxSize = parameter.getValue().stream().max(Comparator.comparing(Integer::valueOf));
if (maxSize.isPresent()) {
size = Integer.valueOf(maxSize.get());
size = Integer.parseInt(maxSize.get());
}
break;
case "~from":
Optional<String> maxFrom = parameter.getValue().stream().max(Comparator.comparing(Integer::valueOf));
if (maxFrom.isPresent()) {
from = Integer.valueOf(maxFrom.get());
from = Integer.parseInt(maxFrom.get());
}
break;

Expand Down Expand Up @@ -161,22 +163,20 @@ public Scroll search(String scrollId, MultiValueMap<String, String> searchParame
}

try {
Integer finalSize = size;
Integer finalFrom = from;
SearchRequest.Builder builder = new SearchRequest.Builder();
builder.index(esService.getES_CHANNEL_INDEX())
.query(boolQuery.build()._toQuery())
.from(finalFrom)
.size(finalSize)
.from(from)
.size(size)
.sort(SortOptions.of(o -> o.field(FieldSort.of(f -> f.field("name")))));
if(scrollId != null && !scrollId.isEmpty()) {
builder.searchAfter(scrollId);
builder.searchAfter(FieldValue.of(scrollId));
}
SearchResponse<Channel> response = client.search(builder.build(),
Channel.class
);
List<Hit<Channel>> hits = response.hits().hits();
return new Scroll(hits.size() > 0 ? hits.get(hits.size()-1).id() : null, hits.stream().map(Hit::source).collect(Collectors.toList()));
return new Scroll(!hits.isEmpty() ? hits.get(hits.size()-1).id() : null, hits.stream().map(Hit::source).collect(Collectors.toList()));
} catch (Exception e) {
String message = MessageFormat.format(TextUtil.SEARCH_FAILED_CAUSE, searchParameters, e.getMessage());
logger.log(Level.SEVERE, message, e);
Expand Down
Loading

0 comments on commit dd5d15f

Please sign in to comment.