Search in sources :

Example 1 with ZuliaIndexManager

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);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ArrayList(java.util.ArrayList) Term(io.zulia.message.ZuliaBase.Term) FuzzyTerm(io.zulia.message.ZuliaBase.FuzzyTerm) Document(org.bson.Document) FuzzyTerm(io.zulia.message.ZuliaBase.FuzzyTerm) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GetTermsResponse(io.zulia.message.ZuliaServiceOuterClass.GetTermsResponse) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) GetTermsRequest(io.zulia.message.ZuliaServiceOuterClass.GetTermsRequest) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 2 with ZuliaIndexManager

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());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) JsonObject(com.google.gson.JsonObject) MultipartException(io.micronaut.http.multipart.MultipartException) IOException(java.io.IOException) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 3 with ZuliaIndexManager

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ZuliaBase(io.zulia.message.ZuliaBase) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) MultipartException(io.micronaut.http.multipart.MultipartException) IOException(java.io.IOException) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 4 with ZuliaIndexManager

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);
    }
}
Also used : ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) FetchRequest(io.zulia.message.ZuliaServiceOuterClass.FetchRequest) FetchResponse(io.zulia.message.ZuliaServiceOuterClass.FetchResponse) Document(org.bson.Document) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 5 with ZuliaIndexManager

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);
    }
}
Also used : GetFieldNamesRequest(io.zulia.message.ZuliaServiceOuterClass.GetFieldNamesRequest) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) GetFieldNamesResponse(io.zulia.message.ZuliaServiceOuterClass.GetFieldNamesResponse) Document(org.bson.Document) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Aggregations

Produces (io.micronaut.http.annotation.Produces)11 ZuliaIndexManager (io.zulia.server.index.ZuliaIndexManager)11 Get (io.micronaut.http.annotation.Get)10 Document (org.bson.Document)8 MultipartException (io.micronaut.http.multipart.MultipartException)4 IOException (java.io.IOException)4 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 JsonFormat (com.google.protobuf.util.JsonFormat)2 Nullable (io.micronaut.core.annotation.Nullable)2 Writable (io.micronaut.core.io.Writable)2 HttpResponse (io.micronaut.http.HttpResponse)2 MediaType (io.micronaut.http.MediaType)2 Controller (io.micronaut.http.annotation.Controller)2 Post (io.micronaut.http.annotation.Post)2 QueryValue (io.micronaut.http.annotation.QueryValue)2 StreamedFile (io.micronaut.http.server.types.files.StreamedFile)2 ZuliaConstants (io.zulia.ZuliaConstants)2 ZuliaBase (io.zulia.message.ZuliaBase)2