RediSearch / JRediSearch

Java Client for RediSearch
https://redisearch.io
BSD 2-Clause "Simplified" License
141 stars 62 forks source link

Adding Prefix when creating an Index using Jredisearch #2249 #166

Closed gkorland closed 2 years ago

gkorland commented 2 years ago

ref https://github.com/RediSearch/RediSearch/issues/2249 (@chaitradalawai)

I am not sure if this is the right place to ask help regarding Jredisearch, please do guide me if I need to ask in different platform.

I use Jredisearch(com.redislabs:jredisearch:2.0.0) for querying using Redisearch. I want to add a prefix while creating the Index. I am able to add prefix using the below Redisearch command, FT.CREATE MyIndex ON HASH PREFIX 1 doc: SCHEMA name TEXT

But not able to find options for the same when writing in Java. I use the following code in Java, client.createIndex(schema, Client.IndexOptions.defaultOptions());

Could you help me on how do we add Prefix when using Jredisearch?

gkorland commented 2 years ago
// IndexDefinition requires RediSearch 2.0+
IndexDefinition def = new IndexDefinition()
                        .setPrefixes(new String[] {"item:", "product:"})
                        .setFilter("@price>100");

client.createIndex(sc, Client.IndexOptions.defaultOptions().setDefinition(def));

You can see the full example in the README https://github.com/RediSearch/JRediSearch/blob/master/README.md

chaitradalawai commented 2 years ago

Hi, Thanks for the help. I am able to set prefix in JRedisearch.

chaitradalawai commented 2 years ago

I need info on dropping Indexes in Redisearch using JRedisearch, please help.

I want to delete the existing content of my Indexes and add new data daily. I couldn't find anything for this as client.deleteDocument() methods need docids. So, I am dropping the index using client.dropIndex(true) and recreating it. I am getting the below exception when adding the documents after recreating the index Code: client.dropIndex(true); Schema schema = new Schema() .addTextField("param1", 5.0) .addTextField("param2", 5.0) .addTextField("param3", 5.0); IndexDefinition indexDefinition = new IndexDefinition().setPrefixes("myprefix:"); client.createIndex(schema, Client.IndexOptions.defaultOptions().setDefinition(indexDefinition)); client.addDocument("myprefix:"+docId, 1, fields);

Exception {"@timestamp":"2021-09-22T01:20:03.782+02:00","message":"Unexpected error occurred in scheduled task","level":"ERROR","thread":"scheduling-1","logClass":"o.s.s.s.TaskUtils$LoggingErrorHandler","logMethod":"handleError","stack_trace":"r.c.j.e.JedisDataException: Document already exists\n\tat redis.clients.jedis.Protocol.processError(Protocol.java:132)\n\tat redis.clients.jedis.Protocol.process(Protocol.java:166)\n\tat redis.clients.jedis.Protocol.read(Protocol.java:220)\n\tat redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:278)\n\tat redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196)\n\tat io.redisearch.client.Client.addDocument(Client.java:491)\n\tat io.redisearch.client.Client.doAddDocument(Client.java:467)\n\tat io.redisearch.client.Client.addDocument(Client.java:454)\n\tat io.redisearch.client.Client.addDocument(Client.java:637)\n\tat

I am guessing documents aren't deleted when dropping the index, I saw a command FT.DROPINDEX DD where DD is for deleting documents. Could you Please tell me the corresponding option for setting 'DD' in jredisearch?

sazzad16 commented 2 years ago

@chaitradalawai There is not direct support at this moment.

PS: Please put different question in different/new issues.

Note: Created https://github.com/RediSearch/JRediSearch/issues/169