How to also get custom fields of documents from Elasticsearch using Laravel Scout Elasticsearch Driver? - json

Below is the link to the Scout Elasticsearch Drive which I am using in my Laravel project.
https://github.com/babenkoivan/scout-elasticsearch-driver
I have database table users with the following columns:
id Integer
name String
created_at DateTime
Following the documentation on GitHub, my mapping in the User model looks like followings:
use Searchable; // using Searchable trait.
protected $mapping = [
'properties' => [
'name' => [
'type' => 'text',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
'created_at' => [
'type' => 'date',
'format' => 'dd-MM-yyyy HH:mm',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
// Custom field...
'created_date' => [
'type' => 'date',
'format' => 'dd-MM-yyyy',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
// Custom field...
'created_time' => [
'type' => 'date',
'format' => 'HH:mm',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
]
]
Below is the implementation of the toSearchableArray() function.
public function toSearchableArray()
{
$array = $this->toArray();
// Pre-format the custom fields before inserting them into Elasticsearch.
$array['created_date'] = $this->created_at->format('d-m-Y');
$array['created_time'] = $this->created_at->format('h:i');
return $array;
}
When using curl -X GET command I get the following results and of course with the custom fields.
"hits" : [
{
"_source" : {
"id" : 3,
"name": "John Doe",
"created_at": "31-12-2018 23:59",
"created_date": "31-12-2018", // My custom field.
"created_time": "23:59" // My custom field.
}
]
In my index() action in my controller, I query the data using the following code.
public function index()
{
return User::search("*")->get();
}
I get the records with only the original attributes, matching those columns in the database table as following.
[
{
"id" : 3,
"name": "John Doe",
"created_at": "31-12-2018 23:59",
}
]
Note that this is a JSON response provided by default by Laravel, responding to API calls. My custom attributes created_date and created_time do exist in Elasticsearch as well. How can I get them too as result? The reason I created these custom fields is to format the date and time on the server side beforehands so that my client-side does not need to worry about formating the date and time in the for-loop.
When I use User::search("*")->explain(); I do get my custom fields as well in the hits.hits._source{} object.

This should give you results directly from Elastic Search without matching up with your related models. I think thats the reason why your custom fields get lost.
public function index()
{
return User::search("*")->raw();
}
Documentation

Related

MediaWiki, how to add filters to the SpecialPages:Categories page?

To better understand the code of MediaWiki, I'm trying to modify directly the source code without creating an extension.
In the page Special:Categories I'd like to show only the categories which are not created by a Template. How can I modify the query?
https://doc.wikimedia.org/mediawiki-core/1.28.0/php/classSpecialCategories.html
https://doc.wikimedia.org/mediawiki-core/1.28.0/php/classCategoryPager.html
I've found the solution
The source code for the Special Pages is inside the folder includes/specials. There are many scripts here about categories and by comparing some of those files I could find out the syntax used to manipulate queries in mediawiki which is defined through the method getQueryInfo() inside each file.
I'm posting the queries defined each in a different Special Page related to categories
public function getQueryInfo() {
return [
'tables' => [ 'category' ],
'fields' => [ 'title' => 'cat_title',
'namespace' => NS_CATEGORY,
'value' => 'cat_pages' ],
'conds' => [ 'cat_pages > 0' ],
];
}
public function getQueryInfo() {
return [
'tables' => [ 'categorylinks', 'page' ],
'fields' => [
'namespace' => 'page_namespace',
'title' => 'page_title',
'value' => 'COUNT(*)'
],
'conds' => [
'page_namespace' => $this->namespaceInfo->getContentNamespaces()
],
'options' => [
'HAVING' => 'COUNT(*) > 1',
'GROUP BY' => [ 'page_namespace', 'page_title' ]
],
'join_conds' => [
'page' => [
'LEFT JOIN',
'page_id = cl_from'
]
]
];
}

Use of condition in query builder in case of multiple association in cakephp 3.0?

I am facing trouble in multilevel association using query builder when i used conditions inside multiple contain in cakephp 3.0. Lets suppose i have to get a data of single store so i am trying to add the storeId in condition but its not working else rest of data is fetching correct from all store below are the query builder that i am using:-
// generate query
$bestSellingReportData = $this->ArticleMaster->find('all')->contain([
'Category' => [
'fields' => ['Cat_Id', 'Cat_Code']
],
'size_category' => [
'fields' => ['sizeCat_Id', 'sizeCat_Code']
],
'ItemMaster' => [
'Invoicedetaile' => [
'Invoice' => [
'Store' => [
'fields' => ['Store_Id', 'Store_Code']
],
'conditions' => ['Invoice.StoreId ='.$this->request->data['storeId']],
],
],
],
]);
$bestSellingReportData->select(['totalSoldItems' => $bestSellingReportData->func()->sum('Invoicedetaile.Qty')])
->matching('ItemMaster.Invoicedetaile', function ($q) {
return $q->where([
'AND' => [
'Invoicedetaile.ItemId = ItemMaster.Item_ID',
]
]);
})
->group(['ArticleMaster.Article_Code'])
->order(['totalSoldItems' => 'DESC'])
->autoFields(true);
I try to add the condition in both way using add condition and in where clause. But data is not filtering based on conditions i.e storeId
Work for me after doing R&D of many hours. Below is the query builder that work for me.
$bestSellingReportData = $this->ArticleMaster->find('all')->contain([
'Category' => [
'fields' => ['Cat_Id', 'Cat_Code']
],
'size_category' => [
'fields' => ['sizeCat_Id', 'sizeCat_Code']
],
'ItemMaster' => [
'Invoicedetaile' => [
'Invoice',
],
],
]);
$storeId = $this->request->data['storeId'];
$bestSellingReportData->select(['totalSoldItems' => $bestSellingReportData->func()->sum('Invoicedetaile.Qty')])
->matching('ItemMaster.Invoicedetaile.Invoice', function ($q) use ($storeId) {
return $q->where([
'AND' => [
'Invoicedetaile.ItemId = ItemMaster.Item_ID',
'Invoice.StoreId ='.$storeId,
]
]);
})
->group(['ArticleMaster.Article_Code'])
->order(['totalSoldItems' => 'DESC'])
->autoFields(true);

How access array inside of JSON?

I'm use Guzzle for Laravel to send POST for Webservice, but I can't access array to send picture. That is the structed to I need follow:
{
"title": "xxxxxxxxxxxxxxxxx",
"category_id": "XXXX",
"official_store_id": null,
"pictures": [
{
"id": "XXXXX",
"url": "http://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"secure_url": "https://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"size": "500x500",
"max_size": "500x500",
"quality": ""
}
],
}
I try to send it like this:
$r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token='.$token->erp_access_token, [
'json' => [
'title' => $name,
'category_id' => $cat2,
'price' => $price,
'currency_id' => 'BRL',
'available_quantity' => $quantity,
'buying_mode' => 'buy_it_now',
'listing_type_id' => 'gold_special',
'condition' => 'new',
'description' => $description,
'pictures' => [
'url' => $image
]
]
]);
Returns the error:
{"message":"body.invalid_field_types","error":"[invalid property type: [pictures] expected List but was JSONObject value (truncated...)
I read the Guzzle docs, but I not found any example for that case.
Any suggestion?
Change your pictures value from this
'pictures' => [
'url' => $image
]
to this
'pictures' => [
['url' => $image]
]

CakePHP 3 Passing Alphanumeric Parameter To Resource Routes

I'm trying to pass parameter in resource route. My routes.php has:
$routes->resources('Products', [
'map' => [
'sku/:sku' => [
'action' => 'viewBySku',
'method' => 'GET',
]
]
]);
});
And my control action is as follows:
public function viewBySku($sku) {
die($sku);
}
After I execute the route the controller is generating a warning:
Missing argument 1 for viewBySku()
What am I missing?
Edit:
I want to use alphanumeric value for the parameter. Most answers I've found are explaining how to pass integers only.
So here is what I've ended up doing:
routes.php
$routes->resources('Products', [
'map' => [
'sku/:sku' => [
'action' => 'viewBySku',
'method' => 'GET',
]
]
]);
controller
public function viewBySku() {
if (!$sku = $this->request->param('sku')) {
throw new BadRequestException("Sku is missing");
}
die($sku);
}
Apparently, my variable is being passed to router but not to the controller's action.
Other Solution
So after looking into resource routes I've found out that you can pass connect options to it:
$routes->resources('Products', [
'map' => [
'sku/:sku' => [
'action' => 'viewBySku',
'method' => 'GET',
]
],
'connectOptions' => [
'sku' => '[a-zA-Z0-9\-]{2,10}',
'pass' => ['sku', 'id']
]
]);
You can also modify the id's regex if you want to pass alphanumeric values to id parameter:
$routes->resources('Products', [
'connectOptions' => [
'id' => '[a-zA-Z0-9\-]{2,10}'
]
]);

How to set a Yii2 module to return JSON response using config/main.php

I am currently using the following lines of code on every controller in the API module in order to return JSON response/data.
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
return $behaviors;
}
It works well. But how can i achieve the same using main configuration file?
I tried the following on my frontend/config/main.php
'api' => [
'class' => 'app\modules\api\Module',
'components' => [
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
'enableSession' => false,
'loginUrl' => null,
],
'response' => [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => \yii\web\Response::FORMAT_JSON,
],
]
],// Module component
],
above configuration still returns XML response only. What is the correct configuration to set all the controllers in the API module to return JSON data.Thanks
Configure your response component as follows:
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
// ...
]
formats is an array containing the available formats. format is the actual output format.
Add this is your config/main-local.php
use yii\web\Response;
$config['bootstrap'][]=
[
'class' => '\yii\filters\ContentNegotiator',
'formats' => [
'text/html' => Response::FORMAT_JSON,
]
];