创建新的索引并在同一步骤中添加数据与seen in the docs相同.
但是,当我试图添加数据到现有的索引,我得到以下异常.
Ehann\RediSearch\Exception\FieldNotInSchemaException
The field is not a property in the index
以下是我的代码:
$adapter = new PredisAdapter();
$host = config('database.redis.default.host');
$port = config('database.redis.default.port');
$database = config('database.redis.default.database');
$password = config('database.redis.default.password');
$redis = $adapter->connect($host, $port, $database, $password);
$bookIndex = new Index($redis, 'books');
$bookIndex->add([
new TextField('title', 'Tale of Two Cities'),
new TextField('author', 'Charles Dickens'),
new NumericField('price', 9.99),
new NumericField('stock', 231),
]);
我研究了redisearch-php
的源代码,似乎如果不总是添加带有addTextField
或其他的字段,这是不可能实现的.但在我看来,这似乎不太现实.我想我必须在创建索引时添加一次字段.我说错了吗?
getFields
方法(用于某些方法,如makeDocument
)从Index
获取对象变量,但如果事先未手动添加,则字段相关对象变量不在实例中.
供应商/ethanhann/redisearch-php/src/index.php
/**
* @return array
*/
protected function getFields(): array
{
$fields = [];
foreach (get_object_vars($this) as $field) {
if ($field instanceof FieldInterface) {
$fields[$field->getName()] = clone $field;
}
}
return $fields;
}
/**
* @param string $name
* @param float $weight
* @param bool $sortable
* @param bool $noindex
* @return IndexInterface
*/
public function addTextField(string $name, float $weight = 1.0, bool $sortable = false, bool $noindex = false): IndexInterface
{
$this->$name = (new TextField($name))->setSortable($sortable)->setNoindex($noindex)->setWeight($weight);
return $this;
}
/**
* @param string $name
* @param bool $sortable
* @param bool $noindex
* @return IndexInterface
*/
public function addNumericField(string $name, bool $sortable = false, bool $noindex = false): IndexInterface
{
$this->$name = (new NumericField($name))->setSortable($sortable)->setNoindex($noindex);
return $this;
}
/**
* @param string $name
* @return IndexInterface
*/
public function addGeoField(string $name, bool $noindex = false): IndexInterface
{
$this->$name = (new GeoField($name))->setNoindex($noindex);
return $this;
}