Skip to content

Commit

Permalink
Arbeid pågår II
Browse files Browse the repository at this point in the history
  • Loading branch information
krharum committed Nov 2, 2023
1 parent bf6027c commit d754618
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/dolly-backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.retry:spring-retry:2.0.0'

implementation 'org.springframework.data:spring-data-elasticsearch'
implementation 'org.opensearch.client:spring-data-opensearch:1.2.0'

implementation 'org.apache.commons:commons-lang3:3.12.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
import no.nav.dolly.elastic.ElasticBestilling;
import no.nav.dolly.elastic.ElasticTyper;
import no.nav.testnav.libs.dto.personsearchservice.v1.search.PersonSearch;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
Expand All @@ -29,6 +34,7 @@ public class OpenSearchService {

private final BestillingElasticRepository bestillingElasticRepository;
private final ElasticsearchOperations searchOperations;
private final RestHighLevelClient restHighLevelClient;
private Random random = new SecureRandom();

public List<ElasticBestilling> getAll() {
Expand All @@ -46,16 +52,43 @@ public List<String> getTyper(ElasticTyper[] typer) {
.map(this::getCriteria)
.forEach(criteria::and);

// var query = new CriteriaQuery(criteria);
var functionScore = new StringQuery("{\n" +
" \"query\": {\n" +
" \"function_score\": {\n" +
" \"random_score\": {\n" +
" \"seed\": 10,\n" +
" \"field\": \"_seq_no\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}");

var query = new CriteriaQuery(criteria)
.setPageable(Pageable.ofSize(10))
.getRe

// var query = new NativeQuery(new NativeQueryBuilder()
// .withQuery()
// .withMaxResults(10)
// .build());

var functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(new RandomScoreFunctionBuilder().seed(random.nextLong()));

var query = QueryBuilder.getSearchRequest(QueryBuilder.buildPersonSearchQuery(), 1, 10, 10)
// var query = QueryBuilder.getSearchRequest(QueryBuilder.buildPersonSearchQuery(), 1, 10, 10);

// var hits = searchOperations.search(query,
// ElasticBestilling.class, IndexCoordinates.of("bestilling"));
var hits = searchOperations.search(query,
ElasticBestilling.class, IndexCoordinates.of("bestilling"));

var hits = searchOperations.search(query)
// SearchResponse hits = null;
// try {
// hits = restHighLevelClient.search(query, RequestOptions.DEFAULT);
//
// } catch (IOException e) {
// throw new RuntimeException(e);
// }

log.info("hits. {}", hits);
// return null;

return hits.getSearchHits().stream()
.map(SearchHit::getContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static BoolQueryBuilder buildPersonSearchQuery() {
var queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.functionScoreQuery(new RandomScoreFunctionBuilder()
.seed(random.nextLong())));
queryBuilder.must(QueryBuilders.existsQuery("aareg"));

return queryBuilder;
}
Expand Down

0 comments on commit d754618

Please sign in to comment.