RediSearch / JRediSearch

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

No Result returned from client.search() #133

Closed kapil-signity closed 3 years ago

kapil-signity commented 4 years ago

I used the same code in Readme. SearchResult res = client.search(q); print(res.totalResults) <===Returning 0 always even item inserted in redis. see below snapshotshot image

gkorland commented 4 years ago

@kapil-signity can you please share the index schema definition?

kapil-signity commented 4 years ago

@gkorland

Client client = new Client("myIndexNew", "localhost", 6379);
Schema sc = new Schema()
                .addTextField("title", 5.0)
                .addTextField("body", 1.0)
                .addNumericField("price");

// 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));

Map<String, Object> fields = new HashMap<>();
fields.put("title", "hello world");
fields.put("state", "NY");
fields.put("body", "lorem ipsum");
fields.put("price", 1337);

// RediSearch 2.0+ supports working with Redis Hash commands
try(Jedis conn = client.connection()){
    conn.hset("item", fields);
}

Query q = new Query("hello world")
                    .addFilter(new Query.NumericFilter("price", 0, 1000))
                    .limit(0,5);

// actual search
SearchResult res = client.search(q);
System.out.println(res.totalResults);  **<==== printing 0 as result**
gkorland commented 4 years ago

I see two issues in your code example:

  1. Prefix is set as item: while the hash key is item --> this prefix can match keys like item:, item:1, items:2....
  2. Price is set as 1337 while the query range is 0-1000