spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

RedisVectorStore NPE when using FilterExpression

Open bram-pramono opened this issue 2 years ago • 6 comments

Hi,

I got this NPE every time I started my application using spring-ai-redis.

java.lang.NullPointerException: Cannot invoke "org.springframework.ai.vectorstore.filter.converter.FilterExpressionConverter.convertExpression(org.springframework.ai.vectorstore.filter.Filter$Expression)" because "this.filterExpressionConverter" is null
	at org.springframework.ai.vectorstore.RedisVectorStore.nativeExpressionFilter(RedisVectorStore.java:394) ~[spring-ai-redis-0.8.0-20240114.220504-53.jar:0.8.0-SNAPSHOT]

It seems that the filterExpressionConverter is not initialized once the index has been created in Redis. The only place, where this field is set, is inside the method afterPropertiesSet() along with the check if index exists & its creation. This is not a necessity to have the index creation and intialization of the filterExpressionConverter field.

I purpose that the initialization to be done before the check of index existence. Thank you.

Cheers, Bram

bram-pramono avatar Jan 24 '24 14:01 bram-pramono

@bram-pramono the afterPropertiesSet() is automatically called if the RedisVectorStore is initialised as a @Bean. For example in RedisVectorStoreIT .

But if you decide to create it manually, then you must call the RedisVectorStore#afterPropertiesSet() after setting the properties and before using the client.

I also updated the spring-ai redis docs to note this detail.

tzolov avatar Jan 26 '24 17:01 tzolov

I guess, the filterExpressionConverter should be inited at first line of the afterPropertiesSet() not matter the index exist or not. or at least mark it as protected so sub-class can extend the logic.

apihug avatar Jan 30 '24 05:01 apihug

Ah, I see it now. Your observation makes perfect sense. Just pushed a patch: 0abf1528dd6d6bb3a6747c4bb435b30bb4c3dd46 that moves the filter configuration to the constructor. Let me know if it solves the issue for you?

@apihug thank you for catching this problem!

tzolov avatar Jan 31 '24 10:01 tzolov

my pleasure :-)

apihug avatar Jan 31 '24 12:01 apihug

Fixed with https://github.com/spring-projects/spring-ai/commit/0abf1528dd6d6bb3a6747c4bb435b30bb4c3dd46

tzolov avatar Feb 01 '24 11:02 tzolov

it looks much better now :) thx @apihug & @tzolov

bram-pramono avatar Feb 13 '24 12:02 bram-pramono