在我应该用逗号分隔的字段搜索逗号分隔字符串的情况下,因此我在映射中执行以下操作但显示MapperParsingException[Analyzer [comma] not found for field [conduct_days]]
错误。
$course = new Course();
$course->no = '1231321';
.......
.......
$course->save();
// Now index the new created course
$client = \Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'type' => 'my_resources',
'body' => [
'my_resources' => [
'_source' => [
'enabled' => true
],
'settings' => [
"analysis" => [
"tokenizer" => [
"comma" => [
"type" => "pattern",
"pattern" => ","
]
],
"analyzer" => [
"comma" => [
"type" => "custom",
"tokenizer" => "comma"
]
]
]
],
'properties' => [
'conduct_days' => array(
'type' => 'string',
'analyzer' => 'comma'
),
'no' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'created_at' => array(
'type' => 'date_time',
"format"=>"YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
),
'updated_at' => array(
'type' => 'date_time',
"format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
),
'deleted_at' => array(
'type' => 'date_time',
"format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
),
'created_by' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'updated_by' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'deleted_by' => array(
'type' => 'string',
'analyzer' => 'standard'
)
]
]
]
];
// Update the index mapping
$client->indices()->putMapping($params);
$params = [
'index' => 'promote_kmp',
'type' => 'courses',
'id' => uniqid(),
'body' => [
'id' => $course->id,
'conduct_days' => $course->conduct_days,
'no' => $course->no,
'created_at' => $course->created_at,
'created_by' => $loggedInUser,
]
];
$client->index($params);
假设我必须在行为天数字段中搜索1,3,5,7
,1,2
和1,2,3
以及1,3,5,6
等等。对于搜索,我认为我应该展开搜索字词,例如如果搜索字词为1,2
,我应该搜索两次,首先搜索1
,然后搜索2
。还有其他搜索解决方案吗?
答案 0 :(得分:1)
您无法在settings
电话中传递putMapping
,它们将被忽略。 settings
旨在传递给create
调用以创建索引
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
"analysis" => [
"tokenizer" => [
"comma" => [
"type" => "pattern",
"pattern" => ","
]
],
"analyzer" => [
"comma" => [
"type" => "custom",
"tokenizer" => "comma"
]
]
]
]
]
];
$response = $client->indices()->create($params);
然后您可以使用映射类型定义调用putMapping
但不使用settings
:
$params = [
'index' => 'my_index',
'type' => 'my_resources',
'body' => [
'my_resources' => [
'_source' => [
'enabled' => true
],
'properties' => [
'conduct_days' => array(
'type' => 'string',
'analyzer' => 'comma'
),
'no' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'created_at' => array(
'type' => 'date_time',
"format"=>"YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
),
'updated_at' => array(
'type' => 'date_time',
"format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
),
'deleted_at' => array(
'type' => 'date_time',
"format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
),
'created_by' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'updated_by' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'deleted_by' => array(
'type' => 'string',
'analyzer' => 'standard'
)
]
]
]
];
// Update the index mapping
$client->indices()->putMapping($params);
<强>更新强>
但是,在您的情况下,我认为最好的办法是创建一个index template,其中包含设置(即分析器)和映射。然后,您的所有应用程序都必须关注,只需致电index()
索引新的课程文档。 ES将负责在正确的时间创建索引和映射,即第一次索引第一个课程文档。
请注意,要执行此操作,您需要
indices->create()
和indices->putMapping()
来电index()