Redisearch-php - How to find data in Index?

Hi,

I use redisearch-php and am new to redisearch and am not sure how to search. I am having the following problem expecting help from everyone.

I created 2 indexes named HQ_Table_1_Index and HQ_Table_2_Index but I searched on HQ_Table_1_Index the data included in HQ_Table_2_Index.

Here is my code:

function createData1(Request $request){
$redis = (new PredisAdapter())->connect(‘redisearch’, 6379, ‘0’, null);
$bookIndex = new Index($redis, “HQ_Table_1_Index”);
$bookIndex->addTextField(‘title’)
->addTextField(‘author’)
->addNumericField(‘price’)
->addNumericField(‘stock’)
->addTextField(‘created_at’)
->create();
$bookIndex->add([
new TextField(‘title’, ‘Title’),
new TextField(‘author’, ‘XXYY’),
new NumericField(‘price’, 9.99),
new NumericField(‘stock’, 231),
new TextField(‘created_at’, ‘2020-09-15’),
]);
return response()->json($bookIndex->getIndexName(), 200);
}

function createData2(Request $request)
{
    $redis = (new PredisAdapter())->connect('redisearch', 6379, '0', null);
    $bookIndex = new Index($redis,"HQ_Table_2_Index");
    $bookIndex->addTextField('name')
            ->addTextField('body')
            ->addNumericField('status')
            ->addTextField('created_at')
            ->create();
    $bookIndex->add([
        new TextField('name', 'HaiQuan'),
        new TextField('body', 'body'),
        new NumericField('status', 1),
        new TextField('created_at', '2020-09-15'),
    ]);
    return response()->json($bookIndex->getIndexName(), 200);
}

function search(Request $request){
    $redis = (new PredisAdapter())->connect('redisearch', 6379, '0', null);
    $bookIndex = new Index($redis, "HQ_Table_1_Index");
    $result =  $bookIndex->search('2020');
    return response()->json($result->getDocuments());
}

Thank you!

Hello,

Do you mind explaining what you want to achieve in text in addition to the code. This will help the community to provide a detailed answer.

Also I am inviting you to look the RediSearch 2.0 Getting Started available here, it may help you to understand the commands and the flow.

Note that the way you create indexes and index document is a little bit different, you just have to create a hash, no need to use the FT.ADD command anymore.

@quannh

you may find more example on redisearch-php github page

Simple Text Search

Text fields can be filtered with the index’s search method.

$result = $bookIndex->search('two cities'); $result->count(); // Number of documents. $result->documents(); // Array of stdObjects.

Documents can also be returned as arrays instead of objects by passing true as the second parameter to the search method.

$result = $bookIndex->search('two cities', true); $result->documents(); // Array of arrays.

Filtering

Tag Fields

Tag fields can be filtered with the index’s tagFilter method.

Specifying multiple tags creates a union of documents.

$result = $bookIndex ->tagFilter('color', ['blue', 'red']) ->search('two cities');

Use multiple separate tagFilter calls to create an intersection of documents.

$result = $bookIndex ->tagFilter('color', ['blue']) ->tagFilter('color', ['red']) ->search('two cities');

Numeric Fields

Numeric fields can be filtered with the index’s numericFilter method.

$result = $bookIndex ->numericFilter('price', 4.99, 19.99) ->search('two cities');

Geo Fields

Numeric fields can be filtered with the index’s geoFilter method.

$result = $bookIndex ->geoFilter('place', -77.0366, 38.897, 100) ->search('two cities');

Sorting Results

Search results can be sorted with the index’s sort method.

$result = $bookIndex ->sortBy('price') ->search('two cities');

Number of Results

The number of documents can be retrieved after performing a search.

$result = $bookIndex->search('two cities'); $result->count(); // Number of documents.

Refer: Searching - RediSearch-PHP

Hi,

Thank you everyone for the support. I created 2 Index(my_index_1, my_index_2) with the command and looked for the following:

redis-cli
FT.CREATE my_index_1 SCHEMA title TEXT WEIGHT 5 description TEXT created_at TEXT PRICE numeric
FT.ADD my_index_1 doc1 1.0 FIELDS title “Acme 42 inch LCD TV” description “42 inch created_at new Full-HD tv with smart tv capabilities” created_at “2020-09-15” price 300

FT.CREATE my_index_2 SCHEMA name TEXT WEIGHT 5 body TEXT created_at TEXT PRICE numeric
FT.ADD my_index_2 doc2 1.0 FIELDS name “HaiQuan” body “Test” created_at “2020-09-15” price 300

FT.SEARCH my_index_1 “2020”

I try to search for data in my_index_1 but have data from my_index_2.

Thank you!

Hello,

I quickly tested on my instance of RediSearch 1.6.14 and cannot reproduce what you are seeing.
The 2 searches are returning (from the CLI for now) the correct result.

Can you do a MODULE LIST command to see which version of the module you are using?

Mine: 10614 == 1.6.14

MODULE LIST
1) 1) "name"
   2) "ft"
   3) "ver"
   4) (integer) 10614

Commands I ran:

FT.CREATE my_index_1 SCHEMA title TEXT WEIGHT 5 description TEXT created_at TEXT PRICE numeric


FT.ADD my_index_1 doc1 1.0 FIELDS title "Acme 42 inch LCD TV" description "42 inch created_at new Full-HD tv with smart tv capabilities" created_at "2020-09-15" price 300

FT.CREATE my_index_2 SCHEMA name TEXT WEIGHT 5 body TEXT created_at TEXT PRICE numeric

FT.ADD my_index_2 doc2 1.0 FIELDS name "HaiQuan" body "Test" created_at "2020-09-15" price 300

FT.SEARCH my_index_1 2020

1) (integer) 1
2) "doc1"
3) 1) "title"
   2) "Acme 42 inch LCD TV"
   3) "description"
   4) "42 inch created_at new Full-HD tv with smart tv capabilities"
   5) "created_at"
   6) "2020-09-15"
   7) "price"
   8) "300"

FT.SEARCH my_index_2 2020
 FT.SEARCH my_index_2 2020
1) (integer) 1
2) "doc2"
3) 1) "name"
   2) "HaiQuan"
   3) "body"
   4) "Test"
   5) "created_at"
   6) "2020-09-15"
   7) "price"
   8) "300"

If you are using RediSearch 2.0, do not use FT.ADD but create the index using a prefix or a filter and to index the HASHES.

I invite you to look at this document:

  • Upgrade to 2.0
  • but if you start on a new project follow, create the index from CLI and you should be able to run the PHP Seartch command without any issues.
MODULE LIST
1) 1) "name"
   2) "search"
   3) "ver"
   4) (integer) 20001

The flow for RediSearch 2 will be:

FT.CREATE my_index_1 ON HASH prefix 1 type:1: SCHEMA title TEXT WEIGHT 5 description TEXT created_at TEXT PRICE numeric

HSET  type:1:001 title "Acme 42 inch LCD TV" description "42 inch created_at new Full-HD tv with smart tv capabilities" created_at "2020-09-15" price 300


FT.CREATE my_index_2 ON HASH prefix 1 type:2: SCHEMA name TEXT WEIGHT 5 body TEXT created_at TEXT PRICE numeric

HSET  type:2:002  name "HaiQuan" body "Test" created_at "2020-09-15" price 300

FT.SEARCH my_index_1 2020

FT.SEARCH my_index_2 2020

Let me know if you need more help.

Hi,

Thank you very much. I reinstalled package 1.6.14 and the search was correct.