Skip to content

Commit

Permalink
- App: Facet Range
Browse files Browse the repository at this point in the history
  • Loading branch information
alegauss committed Feb 27, 2024
1 parent df8bc3b commit cc1634d
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.viglet.turing.persistence.model.nlp.TurNLPVendor;
import com.viglet.turing.persistence.model.se.TurSEInstance;
import com.viglet.turing.persistence.model.sn.TurSNSite;
import com.viglet.turing.persistence.model.sn.TurSNSiteFacetRangeEnum;
import com.viglet.turing.persistence.model.sn.field.TurSNSiteField;
import com.viglet.turing.persistence.model.sn.field.TurSNSiteFieldExt;
import com.viglet.turing.persistence.model.sn.locale.TurSNSiteLocale;
Expand Down Expand Up @@ -63,6 +64,20 @@
@RequestMapping("/api/sn/{ignoredSnSiteId}/field/ext")
@Tag(name = "Semantic Navigation Field Ext", description = "Semantic Navigation Field Ext API")
public class TurSNSiteFieldExtAPI {
public static final String TEXT_GENERAL = "text_general";
public static final String MULTI_VALUED = "multiValued";
public static final String STORED = "stored";
public static final String INDEXED = "indexed";
public static final String TYPE = "type";
public static final String STRING = "string";
public static final String ADD_FIELD = "add-field";
public static final String TEXT_ = "_text_";
public static final String SOURCE = "source";
public static final String ADD_COPY_FIELD = "add-copy-field";
public static final String DEST = "dest";
public static final String PDATE = "pdate";
public static final String NAME = "name";
public static final String SOLR_SCHEMA_REQUEST = "http://%s:%d/solr/%s/schema";
private final TurSNSiteRepository turSNSiteRepository;
private final TurSNSiteFieldExtRepository turSNSiteFieldExtRepository;
private final TurSNSiteFieldExtFacetRepository turSNSiteFieldExtFacetRepository;
Expand Down Expand Up @@ -176,6 +191,7 @@ private TurSNSiteFieldExt saveSNSiteFieldExt(TurSNSite turSNSite, TurSNSiteField
.description(turSNSiteField.getDescription())
.facet(0)
.facetName(turSNSiteField.getName())
.facetRange(TurSNSiteFacetRangeEnum.DISABLED)
.hl(0)
.multiValued(turSNSiteField.getMultiValued())
.mlt(0)
Expand All @@ -186,13 +202,14 @@ private TurSNSiteFieldExt saveSNSiteFieldExt(TurSNSite turSNSite, TurSNSiteField
}

private void addTurSNSiteFieldExt(TurSNFieldType turSNFieldType, TurSNSite turSNSite,
List<TurSNSiteFieldExt> turSNSiteFieldExts, TurNLPEntity turNLPEntity) {
turSNSiteFieldExts.add(turSNSiteFieldExtRepository.save(TurSNSiteFieldExt.builder()
List<TurSNSiteFieldExt> turSNSiteFieldExtList, TurNLPEntity turNLPEntity) {
turSNSiteFieldExtList.add(turSNSiteFieldExtRepository.save(TurSNSiteFieldExt.builder()
.enabled(0)
.name(turNLPEntity.getInternalName())
.description(turNLPEntity.getDescription())
.facet(0)
.facetName(turNLPEntity.getName())
.facetRange(TurSNSiteFacetRangeEnum.DISABLED)
.hl(0)
.multiValued(1)
.mlt(0)
Expand Down Expand Up @@ -227,6 +244,7 @@ public TurSNSiteFieldExt turSNSiteFieldExtUpdate(@PathVariable String ignoredSnS
.peek(fieldExtFacet ->
fieldExtFacet.setTurSNSiteFieldExt(turSNSiteFieldExt))
.collect(Collectors.toSet()));
turSNSiteFieldExtEdit.setFacetRange(turSNSiteFieldExt.getFacetRange());
turSNSiteFieldExtEdit.setHl(turSNSiteFieldExt.getHl());
turSNSiteFieldExtEdit.setEnabled(turSNSiteFieldExt.getEnabled());
turSNSiteFieldExtEdit.setMlt(turSNSiteFieldExt.getMlt());
Expand Down Expand Up @@ -317,7 +335,7 @@ public void updateExternalField(TurSNSiteFieldExt turSNSiteFieldExt) {
}
}

@GetMapping("/create")
@GetMapping("/create/{localeRequest}")
public List<TurSNSite> turSNSiteFieldExtCreate(@PathVariable String ignoredSnSiteId, @PathVariable String localeRequest) {
Locale locale = LocaleUtils.toLocale(localeRequest);
return turSNSiteRepository.findById(ignoredSnSiteId).map(turSNSite -> {
Expand All @@ -328,64 +346,59 @@ public List<TurSNSite> turSNSiteFieldExtCreate(@PathVariable String ignoredSnSit
}).orElse(new ArrayList<>());
}

public void createField(TurSNSite turSNSite, Locale locale, TurSNSiteFieldExt turSNSiteFieldExtList) {
public void createField(TurSNSite turSNSite, Locale locale, TurSNSiteFieldExt turSNSiteFieldExt) {
TurSNSiteLocale turSNSiteLocale = turSNSiteLocaleRepository.findByTurSNSiteAndLanguage(turSNSite, locale);
JSONObject jsonAddField = new JSONObject();
String fieldName;

if (turSNSiteFieldExtList.getSnType() == TurSNFieldType.NER) {
fieldName = String.format("turing_entity_%s", turSNSiteFieldExtList.getName());
if (turSNSiteFieldExt.getSnType() == TurSNFieldType.NER) {
fieldName = String.format("turing_entity_%s", turSNSiteFieldExt.getName());
} else {
fieldName = turSNSiteFieldExtList.getName();
fieldName = turSNSiteFieldExt.getName();
}

jsonAddField.put("name", fieldName);
jsonAddField.put(NAME, fieldName);

jsonAddField.put("indexed", true);
jsonAddField.put("stored", true);
if (turSNSiteFieldExtList.getMultiValued() == 1) {
jsonAddField.put("type", "string");
jsonAddField.put("multiValued", true);
jsonAddField.put(INDEXED, true);
jsonAddField.put(STORED, true);
if (turSNSiteFieldExt.getMultiValued() == 1) {
jsonAddField.put(TYPE, STRING);
jsonAddField.put(MULTI_VALUED, true);
} else {
if (turSNSiteFieldExtList.getType().equals(TurSEFieldType.DATE)) {
jsonAddField.put("type", "pdate");
if (turSNSiteFieldExt.getType().equals(TurSEFieldType.DATE)) {
jsonAddField.put(TYPE, PDATE);
} else {
jsonAddField.put("type", "text_general");
jsonAddField.put(TYPE, TEXT_GENERAL);
}
jsonAddField.put("multiValued", false);
jsonAddField.put(MULTI_VALUED, false);
}
JSONObject json = new JSONObject();
json.put("add-field", jsonAddField);
json.put(ADD_FIELD, jsonAddField);
HttpPost httpPost = new HttpPost(
String.format("http://%s:%d/solr/%s/schema", turSNSite.getTurSEInstance().getHost(),
String.format(SOLR_SCHEMA_REQUEST, turSNSite.getTurSEInstance().getHost(),
turSNSite.getTurSEInstance().getPort(), turSNSiteLocale.getCore()));
executeHttpPost(json, httpPost);
this.copyField(turSNSiteLocale, fieldName, "_text_");
this.copyField(turSNSiteLocale, fieldName, TEXT_);
}

public void copyField(TurSNSiteLocale turSNSiteLocale, String field, String dest) {

JSONObject jsonAddField = new JSONObject();
jsonAddField.put("source", field);

jsonAddField.put("dest", dest);
jsonAddField.put(SOURCE, field);
jsonAddField.put(DEST, dest);
JSONObject json = new JSONObject();
json.put("add-copy-field", jsonAddField);
json.put(ADD_COPY_FIELD, jsonAddField);
TurSEInstance turSEInstance = turSNSiteLocale.getTurSNSite().getTurSEInstance();
HttpPost httpPost = new HttpPost(String.format("http://%s:%d/solr/%s/schema", turSEInstance.getHost(),
HttpPost httpPost = new HttpPost(String.format(SOLR_SCHEMA_REQUEST, turSEInstance.getHost(),
turSEInstance.getPort(), turSNSiteLocale.getCore()));
executeHttpPost(json, httpPost);
}

private void executeHttpPost(JSONObject json, HttpPost httpPost) {
try {
try (CloseableHttpClient client = HttpClients.createDefault()) {
StringEntity entity = new StringEntity(json.toString());
httpPost.setEntity(entity);
httpPost.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
try (CloseableHttpClient client = HttpClients.createDefault()) {
client.execute(httpPost);
}
client.execute(httpPost);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.viglet.turing.persistence.model.sn;

public enum TurSNSiteFacetRangeEnum {

DISABLED,
DAY,
MONTH,
YEAR
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.viglet.turing.commons.se.field.TurSEFieldType;
import com.viglet.turing.persistence.model.sn.TurSNSite;
import com.viglet.turing.persistence.model.sn.TurSNSiteFacetRangeEnum;
import com.viglet.turing.sn.TurSNFieldType;
import jakarta.persistence.*;
import lombok.Builder;
Expand Down Expand Up @@ -57,45 +58,39 @@ public class TurSNSiteFieldExt implements Serializable {
@UuidGenerator
@Column(name = "id", updatable = false, nullable = false)
private String id;

@Column(nullable = false)
private String externalId;

@Column(nullable = false, length = 50)
private String name;

@Column
private String description;

@Column(length = 50)
private String facetName;


@OneToMany(mappedBy = "turSNSiteFieldExt", orphanRemoval = true, fetch = FetchType.LAZY)
@Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<TurSNSiteFieldExtFacet> facetLocales = new HashSet<>();

@Column
private TurSNSiteFacetRangeEnum facetRange = TurSNSiteFacetRangeEnum.DISABLED;
@Column(nullable = false)
private TurSNFieldType snType;

@Column(nullable = false)
private TurSEFieldType type;

@Column
private int multiValued;

@Column
private int facet;

@Column
private int hl;

@Column
private int mlt;

@Column
private int enabled;

@Column
private int required;

@Column(length = 50)
private String defaultValue;

@Column
private int nlp;

// bi-directional many-to-one association to TurSNSite
Expand All @@ -108,6 +103,7 @@ public class TurSNSiteFieldExt implements Serializable {
public TurSNSiteFieldExt() {

}

public void setFacetLocales(Set<TurSNSiteFieldExtFacet> facetLocales) {
this.facetLocales.clear();
if (facetLocales != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

package com.viglet.turing.se.result;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Setter
@Getter
public class TurSEGenericResults {
private long numFound;
Expand All @@ -34,29 +37,12 @@ public class TurSEGenericResults {
private int currentPage;
private List<TurSEResult> results;

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public void setNumFound(long numFound) {
public TurSEGenericResults(long numFound, long start, int limit, int pageCount, int currentPage, List<TurSEResult> results) {
this.numFound = numFound;
}

public void setStart(long start) {
this.start = start;
}

public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}

public void setLimit(int limit) {
this.limit = limit;
}

public void setResults(List<TurSEResult> results) {
this.pageCount = pageCount;
this.currentPage = currentPage;
this.results = results;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
*/
package com.viglet.turing.se.result;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Setter
@Getter
public class TurSEGroup extends TurSEGenericResults {
private String name;

public void setName(String name) {
@Builder
TurSEGroup(long numFound, long start, int limit, int pageCount, int currentPage, List<TurSEResult> results, String name) {
super(numFound, start, limit, pageCount, currentPage, results);
this.name = name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import com.viglet.turing.commons.se.result.spellcheck.TurSESpellCheckResult;
import com.viglet.turing.commons.se.similar.TurSESimilarResult;
import com.viglet.turing.se.facet.TurSEFacetResult;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;


@Setter
@Getter
public class TurSEResults extends TurSEGenericResults {
private int qTime;
Expand All @@ -38,37 +42,19 @@ public class TurSEResults extends TurSEGenericResults {
private List<TurSESimilarResult> similarResults;
private List<TurSEFacetResult> facetResults;
private List<TurSEGroup> groups;

public void setqTime(int qTime) {
@Builder
public TurSEResults(long numFound, long start, int limit, int pageCount, int currentPage, List<TurSEResult> results,
int qTime, long elapsedTime, String queryString, String sort, TurSESpellCheckResult spellCheck,
List<TurSESimilarResult> similarResults, List<TurSEFacetResult> facetResults,
List<TurSEGroup> groups) {
super(numFound, start, limit, pageCount, currentPage, results);
this.qTime = qTime;
}

public void setElapsedTime(long elapsedTime) {
this.elapsedTime = elapsedTime;
}

public void setQueryString(String queryString) {
this.queryString = queryString;
}

public void setSort(String sort) {
this.sort = sort;
}

public void setSpellCheck(TurSESpellCheckResult spellCheck) {
this.spellCheck = spellCheck;
}

public void setSimilarResults(List<TurSESimilarResult> similarResults) {
this.similarResults = similarResults;
}

public void setFacetResults(List<TurSEFacetResult> facetResults) {
this.facetResults = facetResults;
}

public void setGroups(List<TurSEGroup> groups) {
this.groups = groups;
}

}
Loading

0 comments on commit cc1634d

Please sign in to comment.