hello i am new to typesense and want to use it wit...
# community-help
v
hello i am new to typesense and want to use it with java, can anyone help me to design a project where i can convert mongodb data to typesense in java
k
👋 What problem are you running into? You have to just read from MongoDB and convert that into a "flat" (i.e. non-nested) documents that can be indexed into Typesense.
v
i have a mongoDB database, i can fetch data through API and convert it to JSON then using javascript i can index it to typesense. and i have done it successfully,
but what i want is without converting mongodb data into json is it possible to index it into typesense using java
k
Typesense has a Java client but the API uses JSON for transfer.
v
okay means whatever client i use ,first i have to convert mongodb to json then i can index it ?
k
Yes
v
can you provide me initial templates for java client
k
v
thanks
Copy code
SearchParameters searchParameters = new SearchParameters()
        .q("harry")
        .addQueryByItem("title")
        .addSortByItem("ratings_count:desc");
addQueryByItem is not defined
Copy code
SearchParameters searchParameters = new SearchParameters()
                                        .q("tokoyo")                                      .addQueryByItem("countryName").addQueryByItem("capital")                                      .addPrefixItem(true).addPrefixItem(false);
SearchResult searchResult = client.collections("countries").documents().search(searchParameters);
same with your documenation ,addQueryByItem is not defined
k
The examples in the README were written when the client was undergoing development so ignore that and refer to the examples in the test, like here: https://github.com/typesense/typesense-java/blob/master/src/test/java/org/typesense/api/DocumentsTest.java#L94
v
Copy code
package org.typesense.Client;

import org.typesense.api.*;
import org.typesense.model.*;
import org.typesense.resources.*;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;

public class Indexing {

    public void newClient() throws Exception {
        ArrayList<Node> nodes = new ArrayList<>();
        nodes.add(
                new Node(
                        "http",
                        "localhost",
                        "8108"
                )
        );

        Configuration configuration = new Configuration(nodes, Duration.ofSeconds(2),"MyAPI");
        Client client = new Client(configuration);

        CollectionSchema collectionSchema = new CollectionSchema();
        collectionSchema.name("books").defaultSortingField("ratings_count")
                .addFieldsItem(new Field().name("title").type("string"))
                .addFieldsItem(new Field().name("authors").type("string[]").facet(true))
                .addFieldsItem(new Field().name("publication_year").type("string").facet(true))
                .addFieldsItem(new Field().name("ratings_count").type("int32"))
                .addFieldsItem(new Field().name("average_rating").type("float"));

        CollectionResponse collectionResponse = client.collections().create(collectionSchema);

        String booksData = Files.readString(Path.of("org/typesense/Data/books.jsonl"));
        client.collections("books").documents().import_(booksData);


        System.out.println(booksData);
        SearchParameters searchParameters = new SearchParameters()
                .q("harry")
                .queryBy("title");

        SearchResult searchResult = client.collections("books").documents().search(searchParameters);
        System.out.println(searchResult);
    }


}
this client has been created with given typesense document
Now is there anything else i have to do or just create main class to run this ?
Copy code
package org.typesense;

import org.typesense.Client.Indexing;

public class Main {
    public static void main(String[] args) throws Exception {
        Indexing i=new Indexing();
        i.newClient();
    }
}
k
If you run it, it will index the docs and then search as well.
v
okay thanks
"message": "Forbidden - a valid
x-typesense-api-key
header must be sent." this error is shown while i have inserted my API key in node configuration
also sometimes i am gettng error like :- collection already exist so is there any method in java to delete collection before reading the json data like i have done in javascript:-
try { await typesense.collections('books').delete(); console.log('Deleting existing collection: books'); } catch (error) { // Do nothing }
v
okay got it
do typesense also have something like mongoDB compass so we can check our data if it has been indexed properly or not
k
The API response will indicate if there are any errors.
v
everything is working fine
thank you
i just want to know, i can use typesense in springboot ?
k
I'm not familiar with Sprintboot -- it seems to be a framework.. Any place you can use a Java client, it should work. Any reason for thinking it might not work?
v
i also think it should work, just asking for initial templates if you have any for springboot with typesense.
k
No we don't have any prior experience there.
v
okay
class SearchResult { facetCounts: [] found: 0 searchTimeMs: 0 outOf: 0 searchCutoff: false page: 1 groupedHits: null hits: [] requestParams: class SearchResultRequestParams { collectionName: books q: harry perPage: 10 } }
instead of getting a content about searched query this is i all i got on get request
k
Please post collection schema, a sample document and client code that reproduces the issue.
v
sample document of book collection
sorry to bother you, actually there was problem with a data instead of string it was integer
now its working
k
Ok
v
https://typesense.org/docs/guide/mongodb-full-text-search.html#step-4-create-a-typesense-collection i have gone through whole documentation for mongoDB to TypeSense, want to know that https://github.com/HarisaranG/typesense-mongodb this link will convert directly or it is just a template to start?
k
That might be an older fork. The actual project is: https://github.com/typesense/typesense-mongodb It will help sync data.
v
Okay