Skip to content

Commit

Permalink
Merge pull request #4 from schlusslicht/feat/generic-index-name
Browse files Browse the repository at this point in the history
Use configured index name, if available
  • Loading branch information
bkis authored Sep 30, 2024
2 parents beffa1f + 16735d5 commit e0f4e6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ public abstract class CommonSearchData {
@Schema(description = "List of meta properties to filter for")
private Map<String, String[]> meta;


/*
* This will have to be refactored for a generic
* version of the platform!
*/
private String indexName = "vedaweb";


public CommonSearchData(){
sortBy = "_score";
sortOrder = "descend";
Expand Down Expand Up @@ -131,7 +123,7 @@ public void setMeta(Map<String, String[]> meta) {


public String getIndexName() {
return indexName;
return IndexService.indexName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
@Service
public class IndexService {

public static String indexName = "vedaweb";

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Autowired
Expand All @@ -79,12 +81,15 @@ public class IndexService {
@Autowired
private JsonUtilService json;

@Value("${es.index.name}")
private String indexName;

@Value("classpath:es-index.json")
private Resource indexDef;

@Autowired
public IndexService(@Value("${es.index.name}") String indexName) {
if (indexName != null && !indexName.isEmpty()) {
IndexService.indexName = indexName;
}
}

public ObjectNode rebuildIndex(){
ObjectNode response = json.newObjectNode();
Expand Down Expand Up @@ -138,7 +143,7 @@ public ObjectNode indexDbDocuments(){
indexDoc.set("metricalPositions", mPosArray);

// create index request
IndexRequest request = new IndexRequest("vedaweb");
IndexRequest request = new IndexRequest(indexName);

// add request source
request.source(indexDoc.toString(), XContentType.JSON);
Expand Down Expand Up @@ -261,8 +266,8 @@ public ArrayNode getUIGrammarData() {

ObjectNode response = urlRequest(
"get",
"vedaweb/_mapping/field/tokens.grammar.*",
"/vedaweb/mappings");
String.format("%s/_mapping/field/tokens.grammar.*", indexName),
String.format("/%s/mappings", indexName));

if (response == null) {
log.error("Couldn't find index grammar mapping");
Expand Down Expand Up @@ -293,7 +298,7 @@ public ArrayNode getUIBooksData() {
SearchSourceBuilder searchSourceBuilder =
new SearchSourceBuilder().query(match).aggregation(agg);
//create request
SearchRequest req = new SearchRequest("vedaweb").source(searchSourceBuilder);
SearchRequest req = new SearchRequest(indexName).source(searchSourceBuilder);
try {
SearchResponse response = elastic.client().search(req, RequestOptions.DEFAULT);
books.add(((Cardinality)response.getAggregations().get("hymns")).getValue());
Expand Down Expand Up @@ -321,7 +326,7 @@ public ArrayNode getStanzasMetaData(String field) {
SearchSourceBuilder searchSourceBuilder =
new SearchSourceBuilder().aggregation(agg);
//create request
SearchRequest req = new SearchRequest("vedaweb").source(searchSourceBuilder);
SearchRequest req = new SearchRequest(indexName).source(searchSourceBuilder);
try {
SearchResponse response = elastic.client().search(req, RequestOptions.DEFAULT);
for (Bucket b : ((Terms)response.getAggregations().get("metaAgg")).getBuckets()) {
Expand Down Expand Up @@ -373,7 +378,7 @@ public int countStanzas(int book, int hymn) {
SearchSourceBuilder searchSourceBuilder =
new SearchSourceBuilder().query(bool).size(0);
//create request
SearchRequest req = new SearchRequest("vedaweb").source(searchSourceBuilder);
SearchRequest req = new SearchRequest(indexName).source(searchSourceBuilder);
try {
SearchResponse response = elastic.client().search(req, RequestOptions.DEFAULT);
return (int) response.getHits().getTotalHits().value;
Expand Down Expand Up @@ -415,7 +420,7 @@ private long distinct(String field) {
CardinalityAggregationBuilder agg = AggregationBuilders.cardinality("agg");
agg.field(field);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().aggregation(agg);
SearchRequest req = new SearchRequest("vedaweb").source(searchSourceBuilder);
SearchRequest req = new SearchRequest(indexName).source(searchSourceBuilder);
try {
SearchResponse response = elastic.client().search(req, RequestOptions.DEFAULT);
count = ((Cardinality)response.getAggregations().get("agg")).getValue();
Expand All @@ -429,7 +434,7 @@ private long distinct(String field) {
private Map<String, List<String>> collectGrammarFieldAggregations(List<String> grammarFields) {
Map<String, List<String>> grammarAggregations = new HashMap<>();

SearchRequest req = new SearchRequest("vedaweb");
SearchRequest req = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

NestedAggregationBuilder tokens = AggregationBuilders.nested("tokens", "tokens");
Expand Down

0 comments on commit e0f4e6a

Please sign in to comment.