Combine mongoDB $lookup with $project - php

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
]
]
]
],

Related

How to validate an array inside an array payload in laravel

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'
]);

MongoDB filter query results by $lookup array

MongoDB 3.4
I have a project and project_permission collections. The project_permission collection contains permissions to the projects for some users. A single user can have multiple different permissions to the project.
[
'$lookup' => [
'from' => ProjectPermission::collectionName(),
'localField' => '_id',
'foreignField' => 'project_id',
'as' => 'project_permissions'
]
],
[
'$project' => [
// ... irrelevant fields here
'permissions' => '$project_permissions'
]
],
this is how the project query results looks like without filtering:
// other project results
// ... other fields
'permissions' => [
0 => [
'_id' => '5d2873aafa873b2b7c000fad'
'project_id' => '56a9e5c5d18cacc72a485839'
'user_id' => '562f6bfc05dfe9570fb6e427'
'permission' => 'read'
'created_at' => 1562932138
'updated_at' => 1562932139
]
1 => [
'_id' => '5d2879fdfa873b2b7c000fbd'
'project_id' => '56a9e5c5d18cacc72a485839'
'user_id' => '562f6bfc05dfe9570fb6e427'
'permission' => 'write'
'created_at' => 1562932139
'updated_at' => 1562932140
]
2 => [
'_id' => '5db960b5fa873b1604005e8e'
'project_id' => '56a9e5c5d18cacc72a485839'
'user_id' => '582b30dd1e634e6362e1b504'
'permission' => 'write'
'created_at' => 1572430005
'updated_at' => 1572430005
]
]
What I would like to achieve is to return with only those projects where the client - who requested the query - has a specific permission to the project, for example write.
The way I tried it:
pipeline: [
0 => [
'$match' => [
// not related to the problem
]
]
1 => [
'$match' => [
'$and' => [
0 => [
'shared_permissions' => [
'$eq' => true
]
]
1 => [
'$or' => [
0 => [
'project_permissions' => [
'$exists' => true
'$ne' => []
]
]
1 => [
'owner_id' => [
'$ne' => MongoDB\BSON\ObjectId#1
(
[oid] => '582b30dd1e634e6362e1b504'
)
]
]
]
]
]
]
]
2 => [
'$lookup' => [
'from' => 'project_permission'
'localField' => '_id'
'foreignField' => 'project_id'
'as' => 'project_permissions'
]
]
3 => [
'$project' => [
// more not important fields here
'shared_permissions' => 1
'permissions' => [
'$map' => [
'input' => [
'$filter' => [
'input' => '$project_permissions'
'as' => 'project_permission'
'cond' => [
'$and' => [
0 => [
'$eq' => ['$$project_permission.user_id', MongoDB\BSON\ObjectId#1
(
[oid] => '582b30dd1e634e6362e1b504'
)
]
1 => [
'$eq' => ['$$project_permission.permission', 'write']
]
]
]
]
]
'as' => 'project_permission'
'in' => [
'user_id' => '$$project_permission.user_id'
'permission' => '$$project_permission.permission'
]
]
]
]
]
]
For this I almost get the correct response:
[
0 => [
'_id' => '56a9e5c5d18cacc72a485839'
'short_id' => 3
'title' => 'Modified title'
'owner_id' => '562f692a05dfe9560fb6e428'
'updated_at' => 1572435428
'owner_name' => 'Borat Sagdiyev'
'shared_permissions' => true
'permissions' => [
0 => [
'user_id' => '582b30dd1e634e6362e1b504'
'permission' => 'write'
]
]
]
1 => []
]
The problem with this is that empty array, where the result was filtered out - and it wouldn't be a problem if the empty array wouldn't be in the result, because if I use the pagination, then it says two results, instead of one. And we know that in the worst case we would get back an array of empty arrays only.
So what I would like to achieve is this last example results without empty arrays in a way where the pagination will be fine with it too.
ps.: unwind is not an option, because of some structural conventions.
Any ideas?
To perform an equality match between a field from the input documents with a field from the documents of the “joined” collection, the $lookup stage has the following syntax:
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}

How to optimize elastic search query

I have been reading through elastic search docs over the last few months and have continued to optimize my query, but I can't seem to get a search query below 500-600ms. Locally with less data I can get responses in ~80-200ms.
To outline what I am trying to accomplish:
I have 12 different models in Laravel that are searchable from a single search bar. As someone types it is searched and returned in a list of results.
Currently, I have this for my search query. Are there any references for how I can improve this? I looked into multi_match, but I was having issues with partial matches and specifying all fields.
$results = $this->elastic->search([
'index' => config('scout.elasticsearch.index'),
'type' => $type ?? implode(',', array_keys($this->permissions, true, true)),
'body' => [
'query' => [
'bool' => [
'must' => [
[
'query_string' => [
'query' => "$searchQuery*",
],
],
],
'filter' => [
[
'term' => [
'account_id' => $accountId,
],
],
],
'should' => [
[
'term' => [
'_type' => [
'value' => 'customers',
'boost' => 1.3,
],
],
],
[
'term' => [
'_type' => [
'value' => 'contacts',
'boost' => 1.3,
],
],
],
[
'term' => [
'_type' => [
'value' => 'users',
'boost' => 1.3,
],
],
],
[
'term' => [
'_type' => [
'value' => 'chart_accounts',
'boost' => 1.2,
],
],
],
],
],
],
'from' => $from,
'size' => $size,
],
]);

ElasticSearch Index a Document fails on existing index

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

How do you define a Cassandra CollectionMap nested in a UDT with duoshuo's PHP client library?

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
]
]
]
]])

Categories