我阅读了关于在Elasticsearch中索引文档的教程。这里有一个批量索引的例子。我有一个问题,当在循环中为一个项目创建有两个键的数组时,它是正确的:
for($i = 0; $i < 100; $i++) {
$params['body'][] = array(
'index' => array(
'_id' => $i
)
);
$params['body'][] = array(
'my_field' => 'my_value',
'second_field' => 'some more values'
);
}
为什么在循环中有两个数组$params['body'][]
的初始化?必须使用与my_field
相同的键设置索引吗?
我指的是一种情况,当所有关于索引的信息都通过一个键(index)添加到数组中:
$params['body'][] = array(
'index' => array(
'_id' => $i
),
'my_field' => 'my_value',
'second_field' => 'some more values'
);
也在搜索查询后,我得到错误:
消息:非法字符串偏移'match'在线,其中:
$query['match']['name'] = $query;
其中$query
为字符串。
我想这个错误是在创建索引时出现的问题,所以我从这个开始。
在索引中添加文档的代码:
private function addDocument($data = array(), $type)
{
if (!empty($data)) {
foreach ($data as $key => $val) {
$params['body'][] = array(
'index' => array(
'_id' => $key,
'_type' => 'profiles',
'_index' => $this->_typeIndex($type)
)
);
$params['body'][] = (array)$val;
}
$this->client->bulk($params);
}
}
正确吗?因为在搜索中,我得到了这里描述的错误
为了使批量索引工作,有效负载必须包含每个文档的一个命令行(索引、类型、文档id)和一个内容行(文档的实际字段),如下所示:
{"index": {"_id": "1234"}} <--- command for doc1
{"field1": "value1", "field2": "value2"} <--- source for doc1
{"index": {"_id": "1234"}} <--- command for doc2
{"field1": "value1", "field2": "value2"} <--- source for doc2
...
您引用的PHP示例就是这样做的:
$params['body'][] = array(
'index' => array(
'_id' => $i
)
);
将创建读取{"index": {"_id": "0"}}
的第一个命令行和
$params['body'][] = array(
'my_field' => 'my_value',
'second_field' => 'some more values'
);
将创建第二行内容,阅读{"my_field": "my_value", "second_field": "some more values"}
for循环执行此操作100次,并将为100个文档创建包含200行的有效负载。
如果你像对
那样连接正文$params['body'][] = array(
'index' => array(
'_id' => $i
),
'my_field' => 'my_value',
'second_field' => 'some more values'
);
这是行不通的,因为它会在每个文档中生成一行,如下所示:
{"index":{"_id": "0"}, "my_field": "my_value", "second_field": "some more values"}
和批量操作将失败…
再试一次。
不工作,因为你添加了太多行。您应该删除foreach
,并简单地这样做。我只是不确定你的id
字段被称为。我还假设$data
数组包含要添加的文档的字段。
private function addDocument($data = array(), $type)
{
if (!empty($data)) {
$params['body'][] = array(
'index' => array(
'_id' => $data['id'], <--- make sure to use the right id field
'_type' => 'profiles',
'_index' => $this->_typeIndex($type)
)
);
$params['body'][] = $data;
$this->client->bulk($params);
}
}