davy_yg's avatar
Level 27

Redis ft.aggregate 1) (integer) 0

127.0.0.1:6380> hgetall user:2

  1. "first_name"
  2. "Deane"
  3. "last_name"
  4. "Dewhurst"
  5. "email"
  6. "[email protected]"
  7. "gender"
  8. "Female"
  9. "ip_address"
  10. "189.88.77.190"
  11. "country"
  12. "Serbia"
  13. "country_code"
  14. "RS"
  15. "city"
  16. "Senta"
  17. "location"
  18. "20.0780937,45.9260128"
  19. "last_login"
  20. "1586483341"

127.0.0.1:6380> ft.aggregate idx:user "@gender:{female}" groupby 1 @country

  1. (integer) 0

Any idea why I get integer 0 ?

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! The (integer) 0 result from your ft.aggregate command means that no documents matched your query. Let's break down possible reasons and how to fix it.

1. Field Names and Case Sensitivity

Redisearch (the module that provides ft.aggregate) is case-sensitive for both field names and values. In your hash, the field is "gender" with value "Female" (capital "F"):

"gender" => "Female"

But your query is:

ft.aggregate idx:user "@gender:{female}" groupby 1 @country

Here, you're searching for female (all lowercase), but the value in Redis is "Female" (capital "F"). This will not match.

Solution: Match the Case

Try:

ft.aggregate idx:user "@gender:{Female}" groupby 1 @country

2. Index Schema

Make sure your index (idx:user) is set up to index the gender and country fields. You can check your index schema with:

ft.info idx:user

If gender or country are missing, recreate the index or add those fields.

Example index creation:

ft.create idx:user on hash prefix 1 user: schema first_name text last_name text email text gender text country text

3. Data Committed to Index

If you added the hash after creating the index, make sure the key matches the index prefix (user:), and that the index is up to date.

4. Query Syntax

Your query syntax is correct for filtering and grouping, assuming the above are addressed.


Summary

  • Check case sensitivity: Use "Female" not "female".
  • Ensure fields are indexed: gender and country must be in the index schema.
  • Check data and index prefix: The hash key should start with user:.

Example Working Query

ft.aggregate idx:user "@gender:{Female}" groupby 1 @country

If you want to match case-insensitively, consider storing all values in lowercase and querying with lowercase.


Let me know if you need help checking your index schema or recreating it!

davy_yg's avatar
Level 27

@LaryAI

127.0.0.1:6380> ft.info idx:user

index_name
idx:user
index_options
(empty array)
index_definition
    key_type
    HASH
    prefixes
        user:
    default_score
    "1"
attributes
        identifier
        first_name
        attribute
        first_name
        type
        TEXT
        WEIGHT
        "1"
        SORTABLE 
		...
        identifier
        gender
        attribute
        gender
        type
        TEXT
        WEIGHT
        "1"
        SORTABLE

127.0.0.1:6380> ft.create idx:user on hash prefix 1 user: schema first_name text last_name text email text gender text ip_address text country text country_code text city text location text last_login numeric (error) Index already exists

127.0.0.1:6380> ft.aggregate idx:user "@gender:{Female}" groupby 1 @country

(integer) 0

Any idea why integer still 0 ?

Please or to participate in this conversation.