Yii2 how to add multiple link in params.php - php

I want to add multiple link in params.php .
Is it possible to do so ?
Current view of my params in yii2 .
Params.php
<?php
return [
"api_link" => "http://example.com" ,
];
I want to add multiple link in it
Ex:
<?php
return[
"api_link" => "http://example.com" & "http://www.example.com"
];
Is it possible to do so ? I tried but unable to succeed on this attempt .
Any lead over this will really be helpful.

You can set array for each params :
<?php
return [
"api_link" => [
"http://example.com",
"http://example.com",
]
];

You can store in an array
<?php
return [
"api_link" => [
"http://example.com",
"http://example.com",
]
];
....
and for accessing you must iterate over it
$myaArray = [
"api_link" => [
"http://example.com",
"http://www.example.com",
]
];
foreach( $myArray['api_link'] as $key =>$value){
echo $value;
}
or
echo $myArray['api_link'][0];

i don't understand what you want. But you can try by multidimensional array.
$arrayname = array(
'url1' => 'nokibrokes.com',
'url2' => 'example.com'
);
return $arrayname;

Related

How to add "runtime_mappings" to the query for FOS/Elastica in PHP?

Need to add the next json query to php code with using FOSElasticaBundle:
"runtime_mappings": {
"Agreement": {
"type": "keyword",
"script": {
"source": "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n doc['seller'].value+\":\"+\r\n doc['trading.id'].value+\":\"+\r\n doc['winningBidder.edrpou'].value+\":\"\r\n )\r\n}"
}
}
}
If I'm set this in simple method (\Elastica\Query)->addParam():
->addParam('runtime_mappings', [
'Agreement' => [
'type' => 'keyword',
'script' => [
'source' => "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n doc['seller'].value+\":\"+\r\n doc['trading.id'].value+\":\"+\r\n doc['winningBidder.edrpou'].value+\":\"\r\n )\r\n}"
]
]
])
Then I get an error when I try to collect the query:
Unknown key for a START_ARRAY in [runtime_mappings].
Alternatively, you can create a query by starting with the "runtime_mappings" array and adding all other parameters after:
$query = \Elastica\Query::create(['runtime_mappings' => [
'Agreement' => [
'type' => 'keyword',
'script' => [
'source' => "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n doc['seller'].value+\":\"+\r\n doc['trading.id'].value+\":\"+\r\n doc['winningBidder.edrpou'].value+\":\"\r\n )\r\n}"
]
],
]]);
// add other params to query...
$query->addParam();

How to cancel update in elasticsearch painless script

While working on trying to switch out PHP code with pure elasticsearch-painless, I noticed that the document doesn't return "noop" even if the document is identical before and after update.
I'm not sure if there is any consequences of having a version update for every time the code is executed? How does it scale?
I'm simply trying to update the views of a post during visit if the identity was not found in views_log, and was wondering either if there is a way to fix the "noop" return, or somehow have it cancel the update?
The code I have right now looks like this:
$script = 'if (!ctx._source.views_log.contains(params.identity)) {
ctx._source.views_log.add(params.identity);
ctx._source.views += 1;
}';
$params = [
'index' => 'post',
'id' => 4861,
'body' => [
'script' => [
'source' => $script,
'lang' => "painless",
'params' => [
'identity' => $identifier
]
]
]
];
$response = $client->update($params);
Following elasticsearch's documentation:
ctx['op']:
Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.
I tried setting ctx.op to none if the condition is not met, but that didn't seem to work.
During writing of this question I figured it out, and might as well share with others.
none is an accepted keyword for ctx.op, it accepts a string. Change none to "none".
So the full script should look like this:
$script = 'if (!ctx._source.views_log.contains(params.identity)) {
ctx._source.views_log.add(params.identity);
ctx._source.views += 1;
} else {
ctx.op = "none";
}';
$params = [
'index' => 'post',
'id' => 4861,
'body' => [
'script' => [
'source' => $script,
'lang' => "painless",
'params' => [
'identity' => $identifier
]
]
]
];
$response = $client->update($params);
This will give the desired "result": "noop"

Search Filter not working in Elastic Search PHP

Stored a new index by the following code.
$data = array(
array("Name"=>"Norman","Email"=>"in.dolor#vulputatemauris.ca","Created"=>"2019-12-29 10:28:03","Modified"=>"2020-11-07 01:45:23"),
array("Name"=>"Drake","Email"=>"posuere#sedorcilobortis.co.uk","Created"=>"2020-11-08 14:37:00","Modified"=>"2019-08-10 06:42:07"),
array("Name"=>"Wynne","Email"=>"ligula.Donec#adipiscingenim.net","Created"=>"2019-05-19 23:30:42","Modified"=>"2019-06-09 08:13:58"),
array("Name"=>"Kirsten","Email"=>"lobortis#Suspendisseeleifend.net","Created"=>"2020-01-09 23:34:19","Modified"=>"2020-04-16 10:23:07"),
array("Name"=>"Ainsley","Email"=>"elit.dictum.eu#Quisquelibero.org","Created"=>"2019-01-22 18:14:39","Modified"=>"2019-09-02 18:44:30"),
array("Name"=>"Walker","Email"=>"ullamcorper#luctussitamet.org","Created"=>"2020-11-05 23:04:46","Modified"=>"2020-01-03 09:29:36"),
array("Name"=>"Evelyn","Email"=>"amet.metus.Aliquam#dui.org","Created"=>"2020-06-28 13:23:09","Modified"=>"2019-04-02 05:41:33"),
array("Name"=>"James","Email"=>"amet.risus#nullaCras.net","Created"=>"2020-04-20 10:15:54","Modified"=>"2020-07-22 12:04:49"),
array("Name"=>"Melvin","Email"=>"nec.eleifend.non#elit.edu","Created"=>"2020-03-07 05:19:53","Modified"=>"2018-12-30 19:33:29"),
);
$hosts = ['http://localhost:9200'];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
'index' => 'dummy_data',
'id' => 'my_id',
'body' => ['data' => $data]
];
$response = $client->index($params);
Trying to search the first record by the following:
$params = [
'index' => 'dummy_data',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'data.Name' => 'Norman' ] ],
],
]
]
]
];
$results = $client->search($params);
The issue is it's returning all the records where the query is matching with only the first one.
Please help with this.
Im guessing you are using the official Elasticsearch-PHP library. In that case it looks like you are indexing all provided data as exactly 1 document with the id being 'my_id'. That is why you get the the entire dataset back when your search matches.
If you want to index multiple documents at the same time, you should look at the bulk_index endpoint.
Here is the official example for reference:
for($i = 0; $i < 100; $i++) {
$params['body'][] = [
'index' => [
'_index' => 'my_index',
]
];
$params['body'][] = [
'my_field' => 'my_value',
'second_field' => 'some more values'
];
}
$responses = $client->bulk($params);

Get specific value from json response not working

enter image description here
I have the following json response as in the image. I would want to access specific value, like: "ROUMANIE ROVA AROMANIA" but I can't seem to get to it. I tried the following:
$response = json_decode($r->getBody(),true);
foreach($response['ParsedResults'] as $key)
{
foreach($key['TextOverlay']['Lines'] as $bla)
{
echo $bla['LineText'];
echo $bla[0]['LineText'];
}
}
If I only echo one depth, it's working. I searched for a solution but none did the trick. Thanks.
0 is the current index of the first item, $bla contains already the data you're looking for, so doing this directly should work:
echo $bla['LineText'];
Here is how the full code should look like:
$response = [
'ParsedResults' => [
[
'TextOverlay' => [
'Lines' => [
[
'LineText' => 'ROUMANIE ROVA AROMANIA',
'Words' => [
[
'WordText' => 'ROUMANIE',
'OtherData' => 'whatever'
],
[
'WordText' => 'ROVA',
'OtherData' => 'whatever'
],
[
'WordText' => 'AROMANIA',
'OtherData' => 'whatever'
],
]
]
]
]
]
]
];
foreach($response['ParsedResults'] as $key)
{
foreach($key['TextOverlay']['Lines'] as $bla)
{
echo $bla['LineText'];
}
}
Tested here: http://sandbox.onlinephpfunctions.com/code/0577e854eed73dfb33193c391acc37dd81baf982

multifield searching in elasticsearch did not work?

I tried to show all the document which matches either title or body section of article in elastic search and i wrote following code in php
if(!empty($_GET))
{
$bodysearch=$_GET['bodysearch'];
$titlesearch=$_GET['titlesearch'];
$params = [
'index' => 'pages',
'type' => 'page',
'body' => [
'query' => [
'bool'=>[
'should'=>[
'match'=>['title'=>$titlesearch],
'match'=>['body'=>$bodysearch]
]
]
]
]
];
$result = $client->search($params)
}
and wrote following to display result
foreach($result['hits']['hits'] as $hit)
{
$sources=$hit['_source'];
echo ''.$sources['title'].'';
echo substr($sources['body'], 0,100).'....';
}
It displays only the list document whose content matching body section provided in $bodysearch , but i want to get the list of document matching $titlesearch to title or matching $bodysearch to body content or matching both, what should i do for such.
Your bool/should is currently an associative array and the second match is overwriting the first one. Use a plain array instead by surrouding your match queries with [...]:
$params = [
'index' => 'pages',
'type' => 'page',
'body' => [
'query' => [
'bool'=>[
'should'=>[
[ 'match'=>['title'=>$titlesearch] ],
[ 'match'=>['body'=>$bodysearch] ]
]
]
]
]
];

Categories