#community-help

Understanding Typesense with Java and MongoDB

TLDR Vasudev needed help implementing a Java project with Typesense and MongoDB. Kishore Nallan guided them in designing the project, explaining different methods, providing templates, and responding to error issues. At last, Vasudev successfully implemented the project and expressed gratitude.

Powered by Struct AI
45
15mo
Solved
Join the chat
Jul 21, 2022 (15 months ago)
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
07:05 AM
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
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:16 AM
👋 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.
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
07:19 AM
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,
07:20
Vasudev
07:20 AM
but what i want is without converting mongodb data into json
is it possible to index it into typesense using java
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:21 AM
Typesense has a Java client but the API uses JSON for transfer.
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
07:23 AM
okay means whatever client i use ,first i have to convert mongodb to json then i can index it ?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:27 AM
Yes
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
08:29 AM
can you provide me initial templates for java client
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
08:54 AM
thanks
09:31
Vasudev
09:31 AM
SearchParameters searchParameters = new SearchParameters()
        .q("harry")
        .addQueryByItem("title")
        .addSortByItem("ratings_count:desc");

addQueryByItem is not defined
09:32
Vasudev
09:32 AM
SearchParameters searchParameters = new SearchParameters()
                                        .q("tokoyo")                                      .addQueryByItem("countryName").addQueryByItem("capital")                                      .addPrefixItem(true).addPrefixItem(false);
SearchResult searchResult = client.collections("countries").documents().search(searchParameters);
09:33
Vasudev
09:33 AM
same with your documenation ,addQueryByItem is not defined
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:29 AM
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
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
10:44 AM
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);
    }


}
10:45
Vasudev
10:45 AM
this client has been created with given typesense document
10:46
Vasudev
10:46 AM
Now is there anything else i have to do or just create main class to run this ?
10:47
Vasudev
10:47 AM
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();
    }
}

Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:47 AM
If you run it, it will index the docs and then search as well.
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
11:16 AM
okay thanks
11:33
Vasudev
11:33 AM
"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
12:15
Vasudev
12:15 PM
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:-
12:15
Vasudev
12:15 PM
try {
await typesense.collections('books').delete();
console.log('Deleting existing collection: books');
} catch (error) {
// Do nothing
}
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
01:27 PM
okay got it
01:27
Vasudev
01:27 PM
do typesense also have something like mongoDB compass so we can check our data if it has been indexed properly or not
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
02:12 PM
The API response will indicate if there are any errors.
Jul 22, 2022 (15 months ago)
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
06:38 AM
everything is working fine
06:38
Vasudev
06:38 AM
thank you
06:39
Vasudev
06:39 AM
i just want to know,
i can use typesense in springboot ?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:41 AM
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?
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
06:45 AM
i also think it should work, just asking for initial templates if you have any for springboot with typesense.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:50 AM
No we don't have any prior experience there.
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
06:57 AM
okay
Jul 24, 2022 (15 months ago)
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
09:51 AM
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
}
}
09:52
Vasudev
09:52 AM
instead of getting a content about searched query this is i all i got on get request
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:00 AM
Please post collection schema, a sample document and client code that reproduces the issue.
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
10:03 AM
sample document of book collection
10:03
Vasudev
10:03 AM
client code
10:08
Vasudev
10:08 AM
sorry to bother you, actually there was problem with a data instead of string it was integer
10:08
Vasudev
10:08 AM
now its working
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:10 AM
Ok
Jul 25, 2022 (15 months ago)
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
12:18 PM
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?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:23 PM
That might be an older fork. The actual project is: https://github.com/typesense/typesense-mongodb

It will help sync data.
Vasudev
Photo of md5-591a0ac3f5f7ef9eb6e6fc35534beeae
Vasudev
12:25 PM
Okay