Skip to content

Commit

Permalink
add fields xml generation
Browse files Browse the repository at this point in the history
  • Loading branch information
micnori committed Feb 19, 2025
1 parent c817f92 commit 0d75a5c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package it.smartcommunitylabdhub.core.components.solr;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import it.smartcommunitylabdhub.core.models.indexers.SolrEntityIndexer;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@Profile("generate-solr")
public class SolrFieldsExportRunner implements CommandLineRunner {
@Autowired
ApplicationContext context;

@Autowired
private TemplateEngine templateEngine;

private List<SolrEntityIndexer<?>> services;

@Autowired(required = false)
public void setServices(List<SolrEntityIndexer<?>> services) {
this.services = services;
}

@Override
public void run(String... args) throws Exception {
log.info("Running solr fields export...");
int returnCode = 0;
if(services != null) {
try {
Map<String, IndexField> fieldsMap = new HashMap<>();
services.forEach(service -> {
for(IndexField field : service.fields()) {
if(!fieldsMap.containsKey(field.getName()))
fieldsMap.put(field.getName(), field);
}
});
Map<String, Object> variables = new HashMap<>();
variables.put("fields", fieldsMap.values());
final Context ctx = new Context();
ctx.setVariables(variables);
String content = templateEngine.process("solr_fields.xml", ctx);
String out = "solr/solr_fields.xml";
Path fp = Paths.get(out);
Files.createDirectories(fp.getParent());
File file = new File(out);
log.info("writing solr fields to {}...",file.getAbsolutePath());
FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8);
} catch (Exception e) {
log.error("Error with export: {}", e.getMessage());
returnCode = 1;
}
}
int exitCode = returnCode == 0
? SpringApplication.exit(context, () -> 0)
: SpringApplication.exit(context, () -> 1);
System.exit(exitCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected SolrInputDocument parse(D item, String type) {
public List<IndexField> fields() {
List<IndexField> fields = new LinkedList<>();

fields.add(new IndexField("id", "string", true, false, true, true));
fields.add(new IndexField("keyGroup", "string", true, false, true, true));
fields.add(new IndexField("type", "string", true, false, true, true));

Expand Down
4 changes: 2 additions & 2 deletions application/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ solr:
url: ${SOLR_URL:false}
user: ${SOLR_USER:}
password: ${SOLR_PASSWORD:}
admin-user: ${SOLR_ADMIN_USER:}
admin-password: ${SOLR_ADMIN_PASSWORD:}
admin-user: ${SOLR_ADMIN_USER:${SOLR_USER}}
admin-password: ${SOLR_ADMIN_PASSWORD:${SOLR_PASSWORD}}
collection: ${SOLR_COLLECTION:dhcore}
timeout: ${SOLR_TIMEOUT:5000}
shards: ${SOLR_COLLECTION_SHARDS_NUM:1}
Expand Down
3 changes: 3 additions & 0 deletions application/src/main/resources/templates/solr_fields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<root>
<field th:each="item : ${fields}" th:name="${item.name}" th:type="${item.type}" th:indexed="${item.indexed}" th:multiValued="${item.multiValued}" th:stored="${item.stored}" th:uninvertible="${item.uninvertible}"/>
</root>

0 comments on commit 0d75a5c

Please sign in to comment.