RediSearch / JRediSearch

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

examples in README do not work #110

Closed NikkyAI closed 4 years ago

NikkyAI commented 4 years ago

first of all.. i am a total noob in redis and i am doing this using kotlin, so i might be making some really stupid errors here

using version 1.8.0 java 8 and writing my code in kotlin

i am running redis in docker and i am using https://hub.docker.com/r/redislabs/redismod/ latest

the first block of code

// Creating a complex query
Query q = new Query("hello world")
                    .addFilter(new Query.NumericFilter("price", 0, 1000))
                    .limit(0,5);

this seems to break because the argument type Query.Filter of addFilter is a private inner class so this is being thrown:

Exception in thread "main" java.lang.IllegalAccessError: tried to access class io.redisearch.Query$Filter from class moe.nikky.redistest.RedisTestKt
    at moe.nikky.redistest.RedisTestKt.main(RedisTest.kt:65)

i tried with java 11 too but there the error just gets more confusing

the other error is more of a runtime error

after adding all the extra fields to the index (looks like it should work) the aggregate qury fails with a classcast exception, seems like jedis returns a JedisDataException that is blindly casted to byte[]

code

    val client = Client("testing", "localhost", 6379)

    // reset index on each run ?
    client.dropIndex(true)

    val sc: Schema = Schema()
        .addTextField("title", 5.0)
        .addTextField("body", 1.0)
        .addTextField("state", 1.0)
        .addNumericField("price")

    client.createIndex(sc, Client.IndexOptions.defaultOptions())

    // insert document(s)
    client.addDocument("doc1",
        mapOf(
            "title" to "hello world",
            "state" to "NY",
            "body" to "lorem ipsum",
            "price" to 1337
        )
    )

// aggregation query
    val r = AggregationBuilder("hello")
        .apply("@price/1000", "k")
        .groupBy("@state", Reducers.avg("@k").`as`("avgprice"))
        .filter("@avgprice>=2")
        .sortBy(10, SortedField.asc("@state"))

    println("executing:")
    println(r.argsString)
    val res2 = client.aggregate(r)

    println("result 2: $res2")
    res2.results.forEach { doc ->
        println("doc: $doc")
    }

the exception is

Exception in thread "main" java.lang.ClassCastException: redis.clients.jedis.exceptions.JedisDataException cannot be cast to [B
    at io.redisearch.AggregationResult.<init>(AggregationResult.java:33)
    at io.redisearch.client.Client.aggregate(Client.java:413)
    at moe.nikky.redistest.RedisTestKt.main(RedisTest.kt:85)

and points to here https://github.com/RediSearch/JRediSearch/blob/f48f3f1cf54302eb6c2134634f0aef0af28a4a9e/src/main/java/io/redisearch/AggregationResult.java#L33

when i run this query in RedisInsight i get a error as well, so i guess there is something wrong with the logic as well.. https://i.imgur.com/lmI3C0b.png

gkorland commented 4 years ago
Query.Filter

Is indeed private but it shouldn't impose an issue in pure Java, but I don't see a reason not to make it public.

gkorland commented 4 years ago

As for the ClassCastException thanks for reporting, I'll push a fix soon.

NikkyAI commented 4 years ago

i tested a bit more and indeed it works fine in java, no idea why kotlin is having issues there seems like some kind of edgecase anyways

gkorland commented 4 years ago

@NikkyAI I hope 1.8.1 https://github.com/RediSearch/JRediSearch/releases/tag/v1.8.1 should fix the kotlin issues.