This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects

Resolve "Die Statistiken sollten auch in ElasticSearch hinterlegt werden."

Compare and Show latest version
14 files
+ 225
41
Compare changes
  • Side-by-side
  • Inline
Files
14
package at.ac.uibk.gitsearch.repository.search;
import at.ac.uibk.gitsearch.repository.jpa.StatisticsRepository;
import at.ac.uibk.gitsearch.service.dto.StatisticsDTO;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch.cat.AliasesResponse;
import co.elastic.clients.elasticsearch.cat.IndicesResponse;
import co.elastic.clients.elasticsearch.cat.aliases.AliasesRecord;
import co.elastic.clients.elasticsearch.cat.indices.IndicesRecord;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.indices.DeleteAliasRequest;
import co.elastic.clients.elasticsearch.indices.DeleteAliasResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
@@ -18,6 +22,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codeability.sharing.plugins.api.search.SearchResultDTO;
import org.codeability.sharing.plugins.api.search.SearchStatisticsDTO;
import org.springframework.stereotype.Repository;
@Repository
@@ -25,17 +30,31 @@ public class ElasticSearchRepository {
private final ElasticsearchClient elasticsearchAPIClient;
@SuppressWarnings("unused")
private final StatisticsRepository statisticsRepository;
private static final Logger LOGGER = LogManager.getLogger(ElasticSearchRepository.class);
public ElasticSearchRepository(ElasticsearchClient elasticsearchAPIClient) {
protected ElasticSearchRepository(ElasticsearchClient elasticsearchAPIClient, StatisticsRepository statisticsRepository) {
super();
this.elasticsearchAPIClient = elasticsearchAPIClient;
this.statisticsRepository = statisticsRepository;
}
/**
* indexes a SearchResult
*
* @param document
* @throws IOException
*/
public void index(final SearchResultDTO document) throws IOException {
index(document, SearchRepositoryConstants.INDEX_METADATA);
}
/**
* indexes a SearchResult
*
* @param document
* @param index the index
* @throws IOException
*/
public void index(final SearchResultDTO document, String index) throws IOException {
@@ -44,6 +63,19 @@ public class ElasticSearchRepository {
});
}
/**
* returns a document from index metadata
*
* @param document
* @throws ElasticsearchException
* @throws IOException
*/
public SearchResultDTO get(final String docId) throws ElasticsearchException, IOException {
final GetResponse<SearchResultDTO> searchResultDTOResponse =
this.elasticsearchAPIClient.get(r -> r.index(SearchRepositoryConstants.INDEX_METADATA).id(docId), SearchResultDTO.class);
return searchResultDTOResponse.source();
}
/**
* return all indices
*
@@ -77,6 +109,28 @@ public class ElasticSearchRepository {
this.elasticsearchAPIClient.indices().create(createIndexRequest -> createIndexRequest.index(indexName).mappings(mapping));
}
/**
* adds/updates searchstatistics to the current doc, if it is existing
*
* @param doc
*/
public void updateSearchStatistics(SearchResultDTO doc) {
final Optional<StatisticsDTO> statistics = statisticsRepository
.findByExerciseID(doc.getExerciseId())
.map(StatisticsDTO::fromEntity);
statistics.ifPresent(s -> {
updateSearchStatistics(doc, s);
});
}
public void updateSearchStatistics(SearchResultDTO doc, StatisticsDTO statistics) {
var ss = new SearchStatisticsDTO();
ss.setBadgeRewarded(statistics.getBadgeRewarded());
ss.setDownloads(statistics.getDownloads());
ss.setViews(statistics.getViews());
doc.setSearchStatistics(ss);
}
/**
* delete an Index
*