I am currently working on a project and as the title suggests, what I want to do is to be able to search from an HTML a cluster already uploaded on elasticsearch and preview the results back in the HTML.
I thought of using Logstash to send the search input from HTML to elasticsearch but I can't figure a way of viewing those results back in the HTML. In general what I want to do, is to be able to work with elasticsearch the way kibana does, but from a website.
Appreciate any possible help :)
use php-elastic official library(https://github.com/elastic/elasticsearch-php).
You can use the following code to get the search result:
$this->client = ClientBuilder::create()->setHosts(["ELASTICSEARCH_HOST:ELASTICSEARCH_PORT"])->build();
$queryBody = ["match_all" => new \stdClass()];
if($search) {
$queryBody = [
"match" => [
"_all" => $search
]
];
}
$params = [
"from" => $page * $this->pageSize, // if you want data for pagination
"size" => $this->pageSize, // if you want data for pagination
"index" => $index,
"type" => $type,
"_source_include" => $fields, // Your required field array
"body" => [
"query" => $queryBody
]
];
//Get the search result
$response = $this->client->search($params);
Related
This is my data table image
my blade file
#table([
'id' => 'Persons.index',
'xhr' => route('api.manage-dup-api'),
'ns' => 'PersonsIndex',
'columns' => $columns ?? [],
'filters' => $filterTable ?? [],
'params' => [
'filters_live' => false,
'selectable' => true,
'toolbar_style' => 'fixed'
]
])
this is a query which passes data to a data table [API]
$q->with('user')
->with('user.roles')
->select(
'persons.*',
'birth->date as birthdate'
)->`enter code here`whereIn('id', $id)->orWhereIn('old_id_CONINET', $coninet_ids);
return $this->outputList($q, $request);
as shown in the picture I want to remove ["] from the CONINET_ID table
you are storing an array of strings in the DB.
you can convert the array values to int:
array_map('intval', $array);
you can also create an accessor on your eloquent model
public function getOldIdConinetAttribute($value)
{
return array_map('intval', $value);
}
It would better if you give some detailed info. As of now details mentioned above can not explain your code. As of my understanding, I suggest you to check Yajra datatable plugin which will help you solving your issue.
or you can cast coninet_id to array by adding below code in your model.
protected $casts = [
'coninet_id' => 'array'
];
I am trying to return the JSON data from a third party API with Laravel-Guzzle. Everything is working but the value is different it's like some kind missing characters specially on images.
Here's Laravel-Guzzle code :
$client = new \GuzzleHttp\Client();
$request = $client->request('GET',
'https://targetweb.com/api/v2/search_items/?', [
'query' => [
'by' => 'relevancy',
'keyword' => 'productx',
'limit' => 50,
'newest' => 0,
'order' => 'desc',
'page_type' => 'search',
'version' => 2
]
]);
$array = json_decode($request->getBody()->getContents(), true);
$nArrItems = count($array['items']);
for ($x=0;$x<$nArrItems;$x++){
echo $array['items'][$x]['name'].'<br/>';
$nImages = count($array['items'][$x]['images']);
for($z=0;$z<$nImages;$z++){
echo $array['items'][$x]['images'][$z]."<br/>";
}
}
Result on my browser running Laravel
It has same result as with API tester
Result API tester
but if I compare it(the above) with chrome inspect
Chrome inspect
in my browser : "309adc074bfeef9ad6d13561ef2","70eb....
in API tester : "images":["309adc074bfeef9ad6d13561ef2","70eb....
in Chrome inspect : "images":["309adc074bfeef9ad6d13561ef2e3ad1","70eb....
It has missing characters that does not show in my browser & API Tester.
Any help as why this is happening would be greatly appreciated.
I was trying to use the QueryBuilder object to generate a properly escaped INSERT statement. The database table name is generated using an uploaded file's name and there'd be multiple tables, so using a model here is not really an option.
The code I tried to use was this:
$params = [
"index" => $row["A"],
"description" => $row["B"],
];
$conn->createCommand(
$qb->insert($tableName, [
"Index" => ":index",
"Description" => ":description",
], $params),
$params
)->execute();
The SQL error message I got was this that the number of parameters did not match the number of tokens.
My primary problem was that the documentation does not properly explain what the $params variable should be. I found out that it should be an empty, but initialised array, so basically $params = [];.
Also, since the function uses $params as a reference, they are already processed by the QueryBuilder object and I don't need to escape my values two times.
The final code that worked was this:
$params = [];
$conn->createCommand(
$qb->insert($tableName, [
"Index" => $row["A"],
"Description" => $row["B"],
], $params),
$params
)->execute();
I hope this helps anyone out there sometime.
It is simple as that:
$conn->createCommand()->insert($tableName, [
"Index" => $row["A"],
"Description" => $row["B"],
])->execute();
The yii\db\Command::insert() does the escaping for you. E.g.:
$a = "a'b\"";
echo \Yii::$app->db->createCommand()->insert('t', ['a' => $a])->getRawSql() . "\n";
returns
INSERT INTO `t` (`a`) VALUES ('a\'b\"')
I have a dataprovider to search a world but my listview does not display any record? How can i send this query object to listview?
every thing in my webpage is worked very good but at the output my list view show "not found any result" this mean my list view code is no have problem . problem is in my dataprovider and this query beacuse i customize that
my controller:
$query = new Query();
$dataProvidersearch=new ActiveDataProvider([
'query'=>$query->from('tbl_post')->Where(['like', 'title', $search])-
>andWhere(['like', 'en_title', $search])->andWhere(['like', 'content', $search])->andWhere(['like', 'en_content', $search]),
]);
this is my list view in my view:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
]);
I'm not sure you have enough code here for someone to help. Even something simple like a listview could consist of a view, a controller, and two model files and your code could be failing at any of these points. You may have simply forgot to include the listview library at the top of your view, but we can't see that in your current example.
What I would recommend is using Gii to generate a listview. It is simple to do and once you have it created, you can study the code to see where you went wrong. You can see how to get started generating code with Gii here: http://www.yiiframework.com/doc-2.0/guide-start-gii.html
ANSWER FROM COMMENTS: Replace andWhere with orWhere, no results are found because no record can match 'title' and 'en_title' and 'content' and 'en_content'.
You are submitting $posts as 'dataProvider' while it should be dataProvidersearch
Instead of:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
Should be:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$dataProvidersearch,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
There is a textarea where user can edit some templates and can use variables like:
{{model.user.name}}
Application need to replace this variables with data and display HTML output.
We can write a small function that will replace variables from template with data but we need to use a template engine like Twig or Smarty.
https://github.com/yiisoft/yii2-twig
https://github.com/yiisoft/yii2-smarty
Now we can use ViewRenderer from Smarty or Twig.
$render = new ViewRenderer();
$content = $render->render($this->view,'template.twig',[
'model' => $model,
]);
But I see this error:
Unable to find template "template.twig" (looked into: .).
How can I use Smarty or Twig to render a template with the content from database in Yii2 ?
I found Mustache ( https://github.com/bobthecow/mustache.php ) and I'm using like this:
$m = new \Mustache_Engine();
echo $m->render("Hello {{model.client.firma}}",['model' => $model]);
You don't create a renderer manually, you configure your application components, like this:
[
'components' => [
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '#runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],
]
You are telling Yii that there is a renderer to handle templates with the .twig extension.
Then, all you need to do is add the .twigextension when calling render in your controller actions:
public function actionIndex()
{
return $this->render('index.twig');
Then put your twig templates in the view folders where the views normally go.
Read the documentation for the twig extension: Twig Extension for Yii 2
If you want to only use twig templates, you can avoid specifying the extension (.twig) if you set the default extension:
'components' => [
'view' => [
'defaultExtension' => 'twig',
You should create a twig component which will wrap around Twig_Environment. You can add yii\twig\Extension extension if you need. Then you should use it like this:
$renderedTemplate = Yii::$app->twig->render($template, $context);
If you really need to render a template from a string variable you may implement your own Twig_LoaderInterface. There is a Twig_Loader_String, but it's deprecated and you should not use it.