How to highlight text in elastic search result - codeigniter - codeigniter

thanks for helping me before, so now i can explore more about elastic search.
so far, i can manipulate data using elasticsearch. i create new library in my php framework (codeigniter).
after make several data input.
since the best part of elastic is searching (yeah of course....), i wonder to make search feature.
but when i create it, im dissapointed with the result.
unlike kibana, my result didnt show the highlight text of keywords that i input before.
question is :
how to make highlight text in elastic search result
i read this article :
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-highlighting.html
but i dont know where to put this code :
{
"query" : {...},
"highlight" : {
"fields" : {
**"content" : {}**
}
}
}
since my code in php only like this :
controller :
$product = $this->elasticsearch->searchDocsWithParameter($type, $post);
$result = json_decode(json_encode($product),true);
echo "total took:";
echo $result['took'];
$x=0; $no=1;
while($x <= $result['_shards']['total']):
echo $no; echo" . ";
echo $result["hits"]["hits"][$x]['_source']['en_question'];
echo"<br>";
echo $result["hits"]["hits"][$x]['_source']['en_answer'];
echo"<br><br>";
$x++; $no++;
endwhile;
library :
public function searchDocsWithParameter($type, $query")
{
return $this->call($type . '/_search?' . http_build_query(array('q' => $q)));
}
i already googling about it, nobody post about how it.
thats why i ask to this forum. pls help me.
many thanks in return
thanks

Maybe this will help you understanding how it should work:
Different result when using GET/POST in elastic search
but for me better option for you should be download elasticsearch library from composer and use this in your project:
https://philsturgeon.uk/php/2012/05/07/composer-with-codeigniter/
then you will be have a more extensive library to usage:
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html

Related

rewriting the URLs dynamically laravel

I am trying to rewrite some of the pages URL in my web app, for example, I have a company details page and I want the URL of this page to contain the name of the company without space or special characters and to have the id too, and I have no idea on how to do that or where to start.
I tried adding the name of the company in the route as a parameter like this:
Route::get('/{RS}-{id}', 'App\Http\Controllers\SiteController#getDetails')->name('details');
It didn't work for some reason and even if it worked there's no way to replace the special characters with their normal form and replace the space with dashes.
This the URL I'm getting: http://mywebsite.come/entreprise_details-13109
this the URL I want: http://mywebsite.come/yoorika-managements-13109 ( with Yoorika Managements being the name of the company )
I searched a lot but I can't seem to find what I am looking for, I just need someone to help me find the right term. thank you!
In route :
Route::get('/{input}', 'App\Http\Controllers\SiteController#getDetails')->name('details');
In controller :
public function getDetails($input)
{
$input_array = explode("-", $input);
$name = $input_array[0];
$id = $input_array[1];
$data = MyModel::where('id', $id)->get();
// return data to view
}
In list view, you may display data like this (table cell ) :
<td> <a href="/{{name .'-'.{{id}}"> {{name .'-'.{{id}}<td/>

Laravel getting ids of a collection without foreach

My code currently looks like this:
foreach ($things as $thing) {
$ids[] = $thing->id;
}
dd(Other::whereIn('thing_id', $ids)->get());
Thing model has many Other
public function others()
{
return $this->hasMany(Other::class);
}
It's working, but can I achieve this functionality without using the foreach? It doesn't seems to be clean for me. I've tried to give whole collection to where in like this:
dd(Other::whereIn('thing_id', $things)->get());
but this only returned where id was 1.
I'm looking for help to clean up this code, any help appreciated.
There is a function called "pluck"
You can apply it on a collection as following
$collection->pluck('id');
More can be seen on docs
https://laravel.com/docs/7.x/collections#method-pluck
I've already found a way to clean it up a bit, instead of foreach I can simply use :
$ids=$things->pluck('id');
If there's a cleaner way pls show me :)

getClientOriginalName() on null

I have a little problem with one request file in my Store controller !
I get the orginal file name extension like this :
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
Sometimes the user don't need to put a file "cert_medical_surclassement" i would like to do a condition like : If the file was added ok but if there is no files in the field i pass to the other variable .
Someone know how i could do this ? thanks a lot in advance
Use the hasFile() method
if ($request->hasFile('cert_medical_surclassement')) {
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
} else {
// else code
}
More info: Documentation

Laravel Scout with TNTSearch driver filtering where clause

I try to get the result of search queries with a where clause in the query using TNTSearch, but it doesn't work. The query didn't get the where clause.
Controller
<?php
public function getLigues(Request $request)
{
if ($request->has('recherche')) {
$ligues = Structure::search($request->recherche)->where('type_structure_id', '2')->get();
} else {
$ligues = Structure::where('type_structure_id', 2)->paginate(1);
}
return view('structure/ligues', compact('ligues'));
}
Does anyone have any ideas on how to get the filter query?
Please go through this issue #59 on official TNT Search Repository.
See contributors comment on same issue.
Where() clause yet does not support!
There are other workarounds to achieve this testcase, for that check issues on repository.

Laravel 4 SQL log / console

Is there something similar in Laravel that allows you to see the actual SQL being executed?
In Rails, for example, you can see the SQL in console. In Django you have a toolbar.
Is there something like that in Laravel 4?
To clarify: My question is how to do it without code. Is there something that is built-in in Laravel that does not require me to write code in app?
UPDATE: Preferably I'd like to see CLI queries as well (for example php artisan migrate)
If you are using Laravel 4, use this:
$queries = DB::getQueryLog();
$last_query = end($queries);
I do this in Laravel 4.
Just set it once in app/start/global.php or anywhere but make sure it is loaded and then it will start logging all your SQL queries.
Event::listen("illuminate.query", function($query, $bindings, $time, $name){
\Log::sql($query."\n");
\Log::sql(json_encode($bindings)."\n");
});
Here is a quick Javascript snippet you can throw onto your master page template.
As long as it's included, all queries will be output to your browser's Javascript Console.
It prints them in an easily readable list, making it simple to browse around your site and see what queries are executing on each page.
When you're done debugging, just remove it from your template.
<script type="text/javascript">
var queries = {{ json_encode(DB::getQueryLog()) }};
console.log('/****************************** Database Queries ******************************/');
console.log(' ');
$.each(queries, function(id, query) {
console.log(' ' + query.time + ' | ' + query.query + ' | ' + query.bindings[0]);
});
console.log(' ');
console.log('/****************************** End Queries ***********************************/');
</script>
There is a Composer package for that: https://packagist.org/packages/loic-sharma/profiler
It will give you a toolbar at the bottom with SQL queries, log messages, etc. Make sure you set debug to true in your configuration.
Here's another nice debugging option for Laravel 4:
https://github.com/barryvdh/laravel-debugbar
I came up with a really simple way (if you are using php artisan serve and PHP 5.4) - add this to app/start/local.php:
DB::listen(function($sql, $bindings, $time)
{
file_put_contents('php://stderr', "[SQL] {$sql} in {$time} s\n" .
" bindinds: ".json_encode($bindings)."\n");
});
but hoping to find a more official solution.
This will print SQL statements like this:
[SQL] select 1 in 0.06s
This code is directly taken form other source but i wanted to make it easy for you as follow it worked for me on PHPStorm using my terminal window i was able to see a complete log but ,after login there was some Sentry thing.
1.add
'log'=>true
inside your config/database.php and below the place ur database name ex.mysql
then add below code toroutes.php above all no under any route configuration , since u can make that under a give route configuration but , u only see when that route is called.
to see this output /goto / app/storage/log/somelogfile.log
if (Config::get('database.log', false))
{
Event::listen('illuminate.query', function($query, $bindings, $time, $name)
{
$data = compact('bindings', 'time', 'name');
// Format binding data for sql insertion
foreach ($bindings as $i => $binding)
{
if ($binding instanceof \DateTime)
{
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
}
else if (is_string($binding))
{
$bindings[$i] = "'$binding'";
}
}
// Insert bindings into query
$query = str_replace(array('%', '?'), array('%%', '%s'), $query);
$query = vsprintf($query, $bindings);
Log::info($query, $data);
});
}
Dont forget to make break point .... or ping me :)
In QueryBuilder instance there is a method toSql().
echo DB::table('employees')->toSql()
would return:
select * from `employees`
This is the easiest method to shows the queries.

Resources