use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.
the class TermsController method get.
@Get
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8", MediaType.TEXT_PLAIN + ";charset=utf-8" })
public HttpResponse<?> get(@QueryValue(ZuliaConstants.INDEX) final String indexName, @QueryValue(ZuliaConstants.FIELDS) final String field, @Nullable @QueryValue(ZuliaConstants.AMOUNT) final Integer amount, @Nullable @QueryValue(ZuliaConstants.MIN_DOC_FREQ) final Integer minDocFreq, @Nullable @QueryValue(ZuliaConstants.MIN_TERM_FREQ) final Integer minTermFreq, @Nullable @QueryValue(ZuliaConstants.START_TERM) final String startTerm, @Nullable @QueryValue(ZuliaConstants.END_TERM) final String endTerm, @Nullable @QueryValue(ZuliaConstants.TERM_FILTER) final String termFilter, @Nullable @QueryValue(ZuliaConstants.TERM_MATCH) final String termMatch, @Nullable @QueryValue(ZuliaConstants.INCLUDE_TERM) final List<String> includeTerm, @Nullable @QueryValue(ZuliaConstants.FUZZY_TERM_JSON) final String fuzzyTermJson, @QueryValue(value = ZuliaConstants.PRETTY, defaultValue = "true") Boolean pretty, @QueryValue(value = ZuliaConstants.FORMAT, defaultValue = "json") final String format) {
ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
GetTermsRequest.Builder termsBuilder = GetTermsRequest.newBuilder();
termsBuilder.setIndexName(indexName);
termsBuilder.setFieldName(field);
if (amount != null) {
termsBuilder.setAmount(amount);
}
if (minDocFreq != null) {
termsBuilder.setMinDocFreq(minDocFreq);
}
if (minTermFreq != null) {
termsBuilder.setMinTermFreq(minTermFreq);
}
if (startTerm != null) {
termsBuilder.setStartTerm(startTerm);
}
if (endTerm != null) {
termsBuilder.setEndTerm(endTerm);
}
if (termFilter != null) {
termsBuilder.setTermFilter(termFilter);
}
if (termMatch != null) {
termsBuilder.setTermMatch(termMatch);
}
if (includeTerm != null) {
termsBuilder.addAllIncludeTerm(includeTerm);
}
if (fuzzyTermJson != null) {
try {
FuzzyTerm.Builder fuzzyTermBuilder = FuzzyTerm.newBuilder();
JsonFormat.parser().merge(fuzzyTermJson, fuzzyTermBuilder);
termsBuilder.setFuzzyTerm(fuzzyTermBuilder);
} catch (InvalidProtocolBufferException e) {
return HttpResponse.ok("Failed to parse analyzer json: " + e.getClass().getSimpleName() + ":" + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
}
}
try {
GetTermsResponse terms = indexManager.getTerms(termsBuilder.build());
if (format.equalsIgnoreCase("json")) {
Document document = new Document();
document.put("index", indexName);
document.put("field", field);
List<Document> termsDocs = new ArrayList<>();
for (Term term : terms.getTermList()) {
Document termDoc = new Document();
termDoc.put("term", term.getValue());
termDoc.put("docFreq", term.getDocFreq());
termDoc.put("termFreq", term.getTermFreq());
termDoc.put("score", term.getScore());
termsDocs.add(termDoc);
}
document.put("terms", termsDocs);
String docString = document.toJson();
if (pretty) {
docString = JsonWriter.formatJson(docString);
}
return HttpResponse.ok(docString).status(ZuliaConstants.SUCCESS).contentType(MediaType.APPLICATION_JSON_TYPE + ";charset=utf-8");
} else {
StringBuilder csvString = new StringBuilder();
csvString.append("term");
csvString.append(",");
csvString.append("termFreq");
csvString.append(",");
csvString.append("docFreq");
if (termsBuilder.hasFuzzyTerm()) {
csvString.append(",");
csvString.append("score");
}
csvString.append("\n");
for (Term term : terms.getTermList()) {
String value = term.getValue();
if (value.contains(",") || value.contains(" ") || value.contains("\"") || value.contains("\n")) {
csvString.append("\"");
csvString.append(value.replace("\"", "\"\""));
csvString.append("\"");
} else {
csvString.append(value);
}
csvString.append(",");
csvString.append(term.getTermFreq());
csvString.append(",");
csvString.append(term.getDocFreq());
csvString.append(",");
csvString.append(term.getScore());
csvString.append("\n");
}
return HttpResponse.ok(csvString).status(ZuliaConstants.SUCCESS).contentType(MediaType.TEXT_PLAIN + ";charset=utf-8");
}
} catch (Exception e) {
return HttpResponse.serverError("Failed to fetch fields for index <" + indexName + ">: " + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
}
}
use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.
the class AssociatedController method get.
@Get("/allForId")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public HttpResponse<?> get(@QueryValue(ZuliaConstants.ID) final String uniqueId, @QueryValue(ZuliaConstants.INDEX) final String indexName) {
ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
try {
if (uniqueId != null && indexName != null) {
List<String> associatedDocuments = indexManager.getAssociatedFilenames(indexName, uniqueId);
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
for (String filename : associatedDocuments) {
jsonArray.add(filename);
}
jsonObject.add("filenames", jsonArray);
return HttpResponse.ok(jsonObject);
} else {
return HttpResponse.serverError("Provide uniqueId and index.");
}
} catch (Exception e) {
return HttpResponse.serverError(e.getMessage());
}
}
use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.
the class AssociatedController method getMetadata.
@Get("/metadata")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public HttpResponse<?> getMetadata(@QueryValue(ZuliaConstants.ID) final String uniqueId, @QueryValue(ZuliaConstants.FILE_NAME) final String fileName, @QueryValue(ZuliaConstants.INDEX) final String indexName) {
ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
try {
if (uniqueId != null && fileName != null && indexName != null) {
ZuliaBase.AssociatedDocument associatedDocument = indexManager.getAssociatedDocument(indexName, uniqueId, fileName);
StreamedFile attach = new StreamedFile(new ByteArrayInputStream(associatedDocument.getMetadata().toByteArray()), MediaType.of(MediaType.APPLICATION_JSON)).attach(fileName);
MutableHttpResponse<StreamedFile> ok = HttpResponse.ok(attach);
attach.process(ok);
return ok;
} else {
return HttpResponse.serverError(ZuliaConstants.ID + " and " + ZuliaConstants.FILE_NAME + " are required");
}
} catch (Exception e) {
return HttpResponse.serverError(e.getMessage());
}
}
use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.
the class FetchController method get.
@Get
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public HttpResponse<?> get(@QueryValue(ZuliaConstants.ID) final String uniqueId, @QueryValue(ZuliaConstants.INDEX) final String indexName, @QueryValue(value = ZuliaConstants.PRETTY, defaultValue = "true") Boolean pretty) {
ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
FetchRequest.Builder fetchRequest = FetchRequest.newBuilder();
fetchRequest.setIndexName(indexName);
fetchRequest.setUniqueId(uniqueId);
FetchResponse fetchResponse;
try {
fetchResponse = indexManager.fetch(fetchRequest.build());
if (fetchResponse.hasResultDocument()) {
Document document = ResultHelper.getDocumentFromResultDocument(fetchResponse.getResultDocument());
if (document != null) {
String docString;
if (pretty) {
docString = document.toJson(JsonWriterSettings.builder().indent(true).build());
} else {
docString = document.toJson();
}
if (pretty) {
docString = JsonWriter.formatJson(docString);
}
return HttpResponse.ok(docString).status(ZuliaConstants.SUCCESS);
}
return HttpResponse.ok("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">").status(ZuliaConstants.NOT_FOUND);
} else {
return HttpResponse.ok("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">").status(ZuliaConstants.NOT_FOUND);
}
} catch (Exception e) {
return HttpResponse.serverError("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">: " + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
}
}
use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.
the class FieldsController method get.
@Get
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public HttpResponse<?> get(@QueryValue(ZuliaConstants.INDEX) final String indexName, @QueryValue(value = ZuliaConstants.PRETTY, defaultValue = "true") Boolean pretty) {
ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
GetFieldNamesRequest fieldNamesRequest = GetFieldNamesRequest.newBuilder().setIndexName(indexName).build();
GetFieldNamesResponse fieldNamesResponse;
try {
fieldNamesResponse = indexManager.getFieldNames(fieldNamesRequest);
Document mongoDocument = new Document();
mongoDocument.put("index", indexName);
mongoDocument.put("fields", fieldNamesResponse.getFieldNameList());
String docString = mongoDocument.toJson();
if (pretty) {
docString = JsonWriter.formatJson(docString);
}
return HttpResponse.ok(docString).status(ZuliaConstants.SUCCESS);
} catch (Exception e) {
return HttpResponse.ok("Failed to fetch fields for index <" + indexName + ">: " + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
}
}
Aggregations