I am using ES php library. Here is what i have tried...
$params = [
'index' => 'tasks',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'all' => [
'_source' => [
'enabled' => true
],
'properties' => [
'task' => [
'type' => 'string',
'analyzer' => 'standard'
],
'to' => [
'type' => 'string',
'analyzer' => 'standard'
],
'category' => [
'type' => 'integer',
'analyzer' => 'keyword'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
It returns success.
Now when i try to index a document...
$params = [
'index' => 'tasks',
'type' => 'all',
'id' => 'some_id',
'body' => [ 'task' => 'some test', 'to' => 'name', 'category' => 1]
];
$response = $client->index($params);
This throws error and does not work, however it works If i try this without creating index and mapping first.
Please suggest. Thanks
It's wrong to define analyzer in a field of type 'integer'.
Trying to create this mapping through Elasticsearch-PHP gives me a bad request:
... "reason":"Mapping definition for [category] has unsupported parameters: [analyzer : keyword]"}},"status":400}
Trying to create this mapping directly via PUT to ES gives me same error
I'm using ES version 2.2.0 and Elasticsearch-PHP 2.0
Related
How to validate a request like for this example i want to create a custom validation for qualities based on a group type. I know how to create a custom validation for laravel but for the example below i want to create a validation for quality type based on its group type.
The payload below it just for demonstration.
$payload = [
'groups' => [
[
'type' => 'human',
'qualities' => [
[
'type' => 'hair',
'value' => 'blue'
],
[
'type' => 'height',
'value' => '188cm'
],
]
],
[
'type' => 'cat',
'qualities' => [
[
'type' => 'hair',
'value' => 'yellow'
]
]
]
]
];
You can use the wildcard, for example:
$request->validate([
'payload.*' => 'required|array',
'payload.*.type' => 'required',
'payload.*.qualities' => 'required|array',
'payload.*.qualities.*' => 'required'
]);
I am trying to implement the suggest function of elasticsearch-php client in my API to suggest people some already existing problems.
I have made index for my problems
'index' => 'newproblemindex',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'newproblems' => [ // type of index
'_source' => [
'enabled' => true
],
'properties' => [
'title' => [
'type' => 'text',
'analyzer' => 'standard'
],
'description' => [
'type' => 'text',
'analyzer' => 'standard'
], 'suggest' => [
'type' => 'completion'
]
]
]
]
]
But I am unable to find which param fields to use to implement suggest function
'index' => 'newproblemindex',
'body' => [
'try' => [
'text' => $request->search_key,
'completion' => [ 'text' => 'suggest' ]
]
],
I am using laravel and taking search_key as request param but I am getting "invalid_type_name_exception" and when I tried to give the type name, it is again giving me some error.
"suggest" => [
"song-suggest" => [
"prefix" => $request->search_key,
"completion" => ["field" => "suggest"]
]
]
I am getting error "suggest is not a valid param ". Please help
and Thanks in advance.
Right now I'm using the $project aggregation for filtering out unnecessary fields. Also I'm using the $lookup aggregation to link two collections togethe and I know how to use both of them in the main collection.
Now my question is; how can I put this $project aggregation inside of a lookup?
What I have now is looks like this:
[
'$lookup' => [
'from' => Media::collectionName(),
'localField' => '_id',
'foreignField' => 'project_id',
'as' => 'mediaList'
]
],
[
'$project' => [
'title' => 1,
'owner_id' => 1,
'owner_name' => 1,
'created_at' => 1,
'updated_at' => 1,
'status' => 1,
'discount' => 1,
'company' => 1,
'media' => [
'$filter' => [
'input' => '$mediaList',
'as' => 'media',
'cond' => $mediaFilter
]
]
]
],
So I can filtering out the unnecessary fields in the main collection. How can I do this in the sub-collection?
If I understood your question correctly, you want to apply a $filter operation on your $mediaList field.
To avoid a redundant $project stage (where you have to declare every single field you want to keep), use $addFields stage instead, like this :
[
'$lookup' => [
'from' => Media::collectionName(),
'localField' => '_id',
'foreignField' => 'project_id',
'as' => 'media' // Note that I use the same name for the field
]
],
[
'$addFields' => [
'media' => [
'$filter' => [
'input' => '$media',
'as' => 'media',
'cond' => $mediaFilter
]
]
]
],
I have a CollectionSet<UDT> where UDT contains a CollectionMap<int,boolean>. I have not been able to find any documentation or example of how to define this when creating a new Cassandra\Type\CollectionSet for inserting into the table. There is a great example with a CollectionList (found here) which is like this:
// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean,
// CollectionList<Text>, CollectionList<UDT>
new Cassandra\Type\CollectionSet([
[
'id' => 1,
'name' => 'string',
'active' => true,
'friends' => ['string1', 'string2', 'string3'],
'drinks' => [['qty' => 5, 'brand' => 'Pepsi'], ['qty' => 3, 'brand' => 'Coke']]
],[
'id' => 2,
'name' => 'string',
'active' => false,
'friends' => ['string4', 'string5', 'string6'],
'drinks' => []
]
], [
[
'type' => Cassandra\Type\Base::UDT,
'definition' => [
'id' => Cassandra\Type\Base::INT,
'name' => Cassandra\Type\Base::VARCHAR,
'active' => Cassandra\Type\Base::BOOLEAN,
'friends' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => Cassandra\Type\Base::VARCHAR
],
'drinks' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => [
'type' => Cassandra\Type\Base::UDT,
'typeMap' => [
'qty' => Cassandra\Type\Base::INT,
'brand' => Cassandra\Type\Base::VARCHAR
]
]
]
]
]
]);
I've tried using the above example with several variations to accommodate the CollectionMap but nothing is working. My last attempt was this
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'value' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])
which gives the error Caught exception: Since v0.7, collection types should have \"definition\" directive. I've also tried using 'definition' instead of 'value'. I'm running out of ideas, any help would be greatly appreciated.
Use "definition" instead of "value". I tried this before but apparently I was doing something else wrong because this worked.
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'definition' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])
I am trying to configure rbac with phpmanager in my project using the Yii2 advance app version. But \Yii::$app->user->can is not returning the expected.
I wrote the RbacController and executed sucessfully yii rbac/init
That updated common/components/items.php as shown
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => 'create a X',],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX => [ 'type' => 2, 'description' => 'update a X', ],
];
In SignupForm::signup, I added it:
$auth = Yii::$app->authManager;
$roleObj = $auth->getRole('user'); // this role is defined by the RBAC Controller's init action
$auth->assign($roleObj, $user->getId());
assignments.php
return [ 2 => [ 'user', ], ];
I think that 2 corresponds to the user id.
rules.php
return [];
common/main.php
...
'components' => [
...
'authManager' => [
'class' => 'yii\rbac\PhpManager',
'defaultRoles' => ['user','admin'],
'itemFile' => '#common/components/rbac/items.php',
'assignmentFile' => '#common/components/rbac/assignments.php',
'ruleFile' => '#common/components/rbac/rules.php'
],
],
...
When I got that role permissions, it prints:
Array ( [createX] => yii\rbac\Permission Object ( [type] => 2 [name] => createX [description] => create a X [ruleName] => [data] => [createdAt] => 1438601819 [updatedAt] => 1438601819 ) )
So I'm expecting that user doesn't have updating permission, but in the method XController::update
echo \Yii::$app->user->can('updateX');
// returns 1, just the same than \Yii::$app->user->can('createX') returning
Please some help
First items file should be:
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => [ 'create a X', ], ],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX' => [ 'type' => 2, 'description' => ['update a X', ], ],
];
You can verify if the code is fine with var_dump:
var_dump(\Yii::$app->authManager);