imin
imin

Reputation: 4578

redis ft.search searched across two different indexes

Here's how I created my indexes

await redis.call('FT.CREATE', 'idx:business', 'SCHEMA',
    'description', 'TEXT',
    'location', 'GEO', 'SORTABLE',
    'rating', 'NUMERIC', 'SORTABLE',
    'sold', 'NUMERIC', 'SORTABLE'
);

await redis.call('FT.CREATE', 'idx:product', 'SCHEMA',
    'description', 'TEXT',
    'location', 'GEO', 'SORTABLE',
    'rating', 'NUMERIC', 'SORTABLE',
    'sold', 'NUMERIC', 'SORTABLE',
);

And here's how I did my search:

const searchResults = await redis.call(
  'FT.SEARCH',
  'idx:product', 
  '@description:' + searchText + ' @location:[' + longitude + ' ' + latitude + ' ' + radius + ' km]', 
  'SORTBY', 'location', 'ASC',
  'RETURN', 4, 'description', 'location', 'rating', 'sold'
);

The problem is, the search result returned data from both product & business

[
  4,
  'business:USER#[email protected]',
  [
    'location',
    '101.80196018074508,2.93521259165689',
    'description',
    'Ayam Gunting Pakcik'
  ],
  'product:USER#[email protected]##product#kepakayammadu123',
  [
    'location',
    '101.80229411576097,2.9349018635560005',
    'description',
    'kepak ayam maduss',
    'sold',
    '5'
  ],
  'business:USER#[email protected]',
  [
    'location',
    '101.80229411576097,2.9349018635560005',
    'description',
    'Kepak Ayam Rambo',
    'rating',
    '3.45',
    'sold',
    '252'
  ],
  'product:USER#[email protected]##product#bolayam11',
  [
    'location',
    '101.80229411576097,2.9349018635560005',
    'description',
    'bebola ayam',
    'rating',
    '3.45',
    'sold',
    '247'
  ]
]

Upvotes: 0

Views: 24

Answers (1)

Guy Royse
Guy Royse

Reputation: 4332

You'll want to take a look at using the PREFIX option with your index creation. By default, Redis will index all fields containing hashes. You need to tell it which hashes. That's what PREFIX is for.

It works like this:

redis.cloud> FT.CREATE idx:business PREFIX 1 business: SCHEMA ...schema elided...

Now, any hash that starts with business: will be added to the index named idx:business.

Likewise, for the product:

redis.cloud> FT.CREATE idx:product PREFIX 1 product: SCHEMA ...schema elided...

This assumes, of course, that your hashes that contain businesses and products are in keys that start with business: and product:. Adjust as needed.

Upvotes: 2

Related Questions