How to Combine Aggregate + Lookup and Search in Laravel - laravel

I Have the following Aggregate + Lookup Query
Aggregate + lookup Query
$collection = Dapil_Kotakab::raw(function ($collection) use ($page, $perPage) {
return $collection->aggregate([
[
'$lookup' => [
'as' => 'KecDetails',
'from' => 'src_kecamatan',
'foreignField' => 'id',
'localField' => 'idKecamatan'
]
],
[
'$lookup' => [
'as' => 'KotDetails',
'from' => 'src_kota_kabupaten',
'foreignField' => 'code',
'localField' => 'idKota'
]
],
[
'$lookup' => [
'as' => 'ProvDetails',
'from' => 'src_provinsi',
'foreignField' => 'idProv',
'localField' => 'idProvinsi'
]
],
['$skip' => ($page - 1) * $perPage],
['$limit' => $perPage],
]);
});
And I have the following $Search code.
$collection = Dapil_Kotakab::whereRaw(array('$text'=>array('$search'=> "\"".$word."\"" )))->skip(($page - 1)*$perPage)->limit($perPage)->get();
I have tried several times, But I keep getting error.
Any suggestions in this regard will be appreciated.

Related

Tweaking Lingo parameters with carrot2 (with PHP)

I'm trying to tweak the call to the Carrot2 REST API :
$client = new Client();
try {
$params = [
'multipart'=> [
['name'=> 'dcs.c2stream', 'contents' => $xml],
['name' => 'dcs.algorithm', 'contents' => 'lingo'],
['name' => 'dcs.output.format', 'contents' => 'JSON'],
['name' => 'dcs.clusters.only', 'contents' => 'true'],
['name' => 'MultilingualClustering.defaultLanguage', 'contents' => 'FRENCH'],
['name' => 'preprocessing.labelFilters.minLengthLabelFilter.minLength', 'contents' => 5],
['name' => 'preprocessing.documentAssigner.minClusterSize', 'contents' => 4]
],
'debug' => false
];
$response = $client->request('POST', 'http://devbox:8080/dcs/rest', $params);
The lingo parameters 'preprocessing.labelFilters.minLengthLabelFilter.minLength' and 'preprocessing.documentAssigner.minClusterSize' have no impact in the request.
I've found them in the documentation of the lingo algorithm.
Thanks for help !
With the good docker image, everything is fine (docker pull touane/carrot2) :
$c2Payload = [
'algorithm' => 'Lingo',
'language' => 'French',
'parameters' => [
'preprocessing' => [
'documentAssigner' => [
'minClusterSize' => 4
],
'labelFilters' => [
'minLengthLabelFilter' => [
'minLength' => 8
],
'completeLabelFilter' => [
'labelOverrideThreshold' => 0.35
]
]
],
'scoreWeight' => 1, // Tri par score
'clusterBuilder' => [
'phraseLabelBoost' => 2.5
],
'dictionaries' => [
'wordFilters' => [
['exact' => $this->getParameter('carrot2')['stop_words']]
]
],
'matrixBuilder' => [
'termWeighting' => [
'#type' => 'LinearTfIdfTermWeighting'
],
'boostFields' => ['title']
]
],
'documents' => []
];
$client = new Client();
$params = [
'body' => json_encode($c2Payload ),
'debug' => false
];
$response = $client->request('POST', $this->getParameter('carrot2')['url'], $params);

Remove duplicates in Laravel collection only if previous item is identical

I have a Laravel Collection of items:
[
[
'id' => 1,
'path' => '/',
'created_at' => '2020-05-20 20:12:00'
],
[
'id' => 2,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:01:00'
],
[
'id' => 3,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:21:00'
],
[
'id' => 4,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:09:00'
],
[
'id' => 5,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:10:00'
],
]
This is the raw data that would be found in the collection if I cast it to an array, but I need it to remain in collection format.
I need to remove any duplicates where the current item has the same path and is less than 5 minutes old compared to the previous item.
So in this case, item #4 would be removed leaving only #5, but items #2 and #3 would be kept even tho their paths are the same.
How would I do this?
Use the following namespace in your controller file:
use DateTime;
Use the following code in your controller file:
$collection = [
[
'id' => 1,
'path' => '/',
'created_at' => '2020-05-20 20:12:00'
],
[
'id' => 2,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:01:00'
],
[
'id' => 3,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:21:00'
],
[
'id' => 4,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:09:00'
],
[
'id' => 5,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:10:00'
],
];
$paths = [];
$time_count = [];
foreach ($collection as $key => $data) {
if (in_array($data['path'], $paths)) {
$arr_pos = array_search($data['path'], array_values($paths));
$datetime1 = new DateTime($data['created_at']);
$datetime2 = new DateTime($collection[$arr_pos]['created_at']);
$interval = $datetime1->diff($datetime2);
$mins = $interval->format('%i');
if ($mins < 5) {
unset($collection[$arr_pos]);
}
}
$paths[] = $data['path'];
}
This will return result:
array:4 [▼
0 => array:3 [▼
"id" => 1
"path" => "/"
"created_at" => "2020-05-20 20:12:00"
]
1 => array:3 [▼
"id" => 2
"path" => "/somewhere"
"created_at" => "2020-05-20 21:01:00"
]
2 => array:3 [▼
"id" => 3
"path" => "/somewhere"
"created_at" => "2020-05-20 21:21:00"
]
4 => array:3 [▼
"id" => 5
"path" => "/somewhere/else"
"created_at" => "2020-05-20 21:10:00"
]
]
I hope this works for you.

Proper formatting of a JSON and multiplepart Guzzle post request

I have two different Guzzle post requests that I am trying to merge (solely because they basically do a united job and should be performed together).
Initially I have my donation data:
'donation' => [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
],
And then I have my files that go with it, which are basically two different PDFs that are enabled or disabled for donors, sometimes they have both. I know the multipart would look something like below, but I'm not sure.
foreach ($uploadDocs as $doc) {
'multipart' => [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id',
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
],
}
Since I've usually only handled one file at a time and I'm not sure how to merge the first block of code with the second for an appropriate Guzzle post request.
You can try this:
$donationData = [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
];
$multipart = [];
foreach ($uploadDocs as $doc) {
$multipart[] = [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id,
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
];
}
Than perform your request:
$r = $client->request('POST', 'http://example.com', [
'body' => $donationData,
'multipart' => $multipart,
]);

Search in Laravel Datatables

In my datatables, the search function works only on the name column. The name column ist different that the others. It means, the name column gives the value of the database back, while the others gives a value back with the function add column
In the column array there are the following values:
´´´
protected function getColumns()
{
return [
'status',
'name' => [
'title' => 'Name',
'orderable' => true,
'searchable' => true
],
'location' => [
'title' => 'Standort',
],
'department' => [
'title' => 'Abteilung',
'orderable' => true,
'searchable' => true
],
'division' => [
'title' => 'Bereich',
'orderable' => true,
'searchable' => true
],
'leader' => [
'title' => 'Verantwortlicher',
'orderable' => true,
'searchable' => true
],
'start_date' => [
'title' => 'Startdatum',
'searchable'=> true,
],
'end_date' => [
'title' => 'Enddatum',
'searchable'=> true
]
];
}
```
Why it doesn't search on all columns? What i have to do?
Try to search by using this code.
return Datatables::of($tasks)
->filter(function ($query) use ($request) {
$title = $request->title;
if (isset($title) && !empty($title)) {
$query->where('title', 'like', '%'.$title.'%');
}
})->make(true);
OR
return Datatables::of($tasks)
->filterColumn('title', function($query, $value) {
$query->whereRaw("title like ?", ["%{$value}%"]);
})->make(true);

Elastic Search: ES-PHP Client how to create GEOPOINT Index

I am trying to create an index that should contain GEOPoint by using the Elastic Search PHP client -> https://www.elastic.co/guide/en/elasticsearch/client/php-api/5.0/index.html
My Code is as follow
$params = [
'index' => 'sweden_codes',
'type' => 'sweden_c',
'body' => [
'mappings' => [
'location' => [
'properties' => [
'pin' => [
'properties' => [
'location' => [
'type' => 'geo_point'
]
]
]
]
],
'text' => $code->City,
'pin' => [
'location' => [
'lat' => $code->Latitude,
'lon' => $code->Longitude
]
]
]
]
];
$client = ClientBuilder::create()
->setSSLVerification(false)
->setHosts(["elasticsearch:9200"])->build();
The Problem is when i go into kibana it say " No Compatible Fields: The "sweden_codes" index pattern does not contain any of the following field types: geo_point"
can anyone please have a look into the issue and let me know whats wrong with my mapping and index creation
Here is the Code for Mappings that works for me
$params = [
'index' => 'sweden_postal_codes',
'body' => [
'mappings' => [
'codes' => [
'properties' => [
'location' => [
'type' => 'geo_point'
],
'city' => [
'type' => 'string'
]
]
]
]
]
];
$client = ClientBuilder::create()
->setSSLVerification(false)
->setHosts(["elasticsearch:9200"])->build();
// Adding the index into the ES Cluster
$response = $client->indices()->create($params);
Here is the code for document indexing that worked for me
$params = [
'index' => 'sweden_postal_codes',
'type' => 'codes',
'id' => 2,
'body' => [
'location' => [
'lat' => 30.5268956,
'lon' => 79.2289643
],
'city' => 'Stockholm'
]
];
$client = ClientBuilder::create()
->setSSLVerification(false)
->setHosts(["elasticsearch:9200"])->build();
// Adding the index into the ES Cluster
$response = $client->index($params);

Resources