弹性搜索示例中的索引?
Indexing in elasticsearch example?
我阅读了 tutorial 关于在 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
?
相同的键设置索引
我的意思是一种情况,当所有关于索引的信息通过一个键(索引)添加到数组中时:
$params['body'][] = array(
'index' => array(
'_id' => $i
),
'my_field' => 'my_value',
'second_field' => 'some more values'
);
同样在搜索查询后我得到错误:
Message: Illegal string offset 'match' on line where is:
$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);
}
}
我阅读了 tutorial 关于在 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
?
我的意思是一种情况,当所有关于索引的信息通过一个键(索引)添加到数组中时:
$params['body'][] = array(
'index' => array(
'_id' => $i
),
'my_field' => 'my_value',
'second_field' => 'some more values'
);
同样在搜索查询后我得到错误:
Message: Illegal string offset 'match' on line where is:
$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);
}
}