I'm im using zendframework/zend-diactoros with Laravel. I have a problem when I sent json data like
{
"data": {
"value": ""
}
}
Then, in controller
// #var $request Psr\Http\Message\ServerRequestInterface
var_dump($request->getParsedBody())
/*
array:1 [
"data" => array:1 [
"value" => null // I expect "value" => ""
]
]
More examples:
I sent
{
"data": {
"value": null
}
}
var_dump
var_dump($request->getParsedBody())
/*
array:1 [
"data" => array:1 [
"value" => null // that's OK
]
]
I sent
{
"data": {
"value": "x"
}
}
var_dump
var_dump($request->getParsedBody())
/*
array:1 [
"data" => array:1 [
"value" => "x" // that's OK
]
]
Only first example has a problem, but I dont have any idea if this is correct. Maybe exists a way to dump exact received data with this PSR7 library.
Related
{"cf_items":{"aliases":{},"mappings":{"properties":{"CFDocumentURI":{"properties":{"identifier":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"title":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"uri":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"CFItemType":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"CFItemTypeURI":{"properties":{"identifier":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"title":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"uri":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"educationLevel":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"fullStatement":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"humanCodingScheme":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"identifier":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"lastChangeDateTime":{"type":"date"},"uri":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"settings":{"index":{"routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"number_of_shards":"1","provided_name":"cf_items","creation_date":"1654520558130","number_of_replicas":"1","uuid":"wMIBuI59QoWhR2-p69ER8Q","version":{"created":"7130099"}}}}}
and I want to search inside any field with matching keyword? How can I do that?
I tried below way
array:2 [
"index" => "cfitems-*"
"body" => array:1 [
"query" => array:1 [
"bool" => array:1 [
"should" => array:1 [
0 => array:1 [
"match_all" => array:1 [
0 => "Default"
]
]
]
]
]
]
]
but not working
use multi-match query
{
"query": {
"multi_match" : {
"query": "Will Smith",
"fields": ["*"] // for all fields or pass in specific fields
}
}
}
I don't see you have many fields in your index. you can use the multi-match query which also supports the wildcard in the fields name.
Refer fields with wildcard example from official Elasticsearch document.
I have a nested collection that I want to transform, pulling some keys "up a level" and discarding some other keys.
Every item in the collection has an allergens property.
"allergens": [
{
"id": 2001,
"info": "Allergy advice",
"status": "does_contain",
"product_id": 71576,
"allergen_id": 1,
"allergen": {
"id": 1,
"name": "Celery"
}
},
{
"id": 2002,
"info": "Allergy advice",
"status": "may_contain",
"product_id": 71576,
"allergen_id": 11,
"allergen": {
"id": 11,
"name": "Peanuts"
}
}
],
I need to make each items allergens property, look like
"allergens": [
{
"id": 1,
"name": "Celery"
"status": "does_contain",
},
{
"id": 11,
"name": "Peanuts"
"status": "does_contain",
},
],
I've tried the following:
$collection = $collection->transform(function ($item, $key) {
$item->allergens = $item->allergens->map(function ($allergen) {
return [
'id' => $allergen->allergen->id,
'name' => $allergen->allergen->name,
'status' => $allergen->status,
];
});
return $item;
});
But it doesn't overwrite the allergens property
Since you're posting your collection as JSON, I reverse engineered what your actual collection would look like. Turns out, your transform() works fine as far as I can tell. Maybe that helps you to find differences between my and your collection which might lead you to your problem/solution:
$collection = collect([
(object)[
"allergens" => collect([
(object)[
"id" => 2001,
"info" => "Allergy advice",
"status" => "does_contain",
"product_id" => 71576,
"allergen_id" => 1,
"allergen" => (object)[
"id" => 1,
"name" => "Celery"
]
],
(object)[
"id" => 2002,
"info" => "Allergy advice",
"status" => "may_contain",
"product_id" => 71576,
"allergen_id" => 11,
"allergen" => (object)[
"id" => 11,
"name" => "Peanuts"
]
]
]),
]
]);
$collection = $collection->transform(function ($item, $key) {
$item->allergens = $item->allergens->map(function ($allergen) {
return [
'id' => $allergen->allergen->id,
'name' => $allergen->allergen->name,
'status' => $allergen->status,
];
});
return $item;
});
dd($collection);
Result:
Illuminate\Support\Collection {#1779 ▼
#items: array:1 [▼
0 => {#1791 ▼
+"allergens": Illuminate\Support\Collection {#1775 ▼
#items: array:2 [▼
0 => array:3 [▼
"id" => 1
"name" => "Celery"
"status" => "does_contain"
]
1 => array:3 [▼
"id" => 11
"name" => "Peanuts"
"status" => "may_contain"
]
]
}
}
]
}
I am passing below data through API in postman.
I want to access these values from collection inside array for Items.
{
"user":"abc",
"supplier":"xyz",
"pdate":"1",
"items":[
{
"product":"Apple",
"qty":"1",
"rate":"40",
"amount":"40"
},
{
"product":"Banana",
"qty":"6",
"rate":"4",
"amount":"24"
}
]
}
Do you mean that you want to conver the items into Collection? If that's the case, you can do:
$data = json_decode('{ "user":"abc", "supplier":"xyz", "pdate":"1", "items":[ { "product":"Apple", "qty":"1", "rate":"40", "amount":"40" }, { "product":"Banana", "qty":"6", "rate":"4", "amount":"24" } ] }', true, 512, JSON_THROW_ON_ERROR);
$items = collect($data['items']);
dd($items);
This will output:
Illuminate\Support\Collection {#1545
#items: array:2 [
0 => array:4 [
"product" => "Apple"
"qty" => "1"
"rate" => "40"
"amount" => "40"
]
1 => array:4 [
"product" => "Banana"
"qty" => "6"
"rate" => "4"
"amount" => "24"
]
]
}
Please note, I have just added and json_decoded the data, for the sake of the example, but just do this in your controller where the data is passed in.
I am trying to create a POST to a REST API to create a new object. I cannot figure out how to properly format my JSON.
Here's the response from the GET of an existing object:
{
"name": "product 2 mem"
"type": "simple"
"categories": array:1 [▼
0 => {
"id": 75
}
]
"meta_data": array:1 [▼
"id": 3665
"key": "_yith_wcbm_product_meta"
"value": {
"id_badge": "2955"
}
}
]
}
Here is the POST I'm trying to create:
$data = [
'name' => 'product name',
'type' => 'simple',
'categories' => [
[
'id' => 75
],
'meta_data' => [
'_yith_wcbm_product_meta' => [
'id_badge' => '2955'
]
]
];
You got typo in you json data.
$response = '{
"id": 3665,
"key": "_yith_wcbm_product_meta",
"value": {
"id_badge": "2955"
}
}';
$array = json_decode($response,true);
$return = ['meta_data'=>['key'=>$array['key'],'value'=>$array['value']]];
echo json_encode($return);
I figured out how to format it:
'meta_data' => [
[
'key' => '_yith_wcbm_product_meta',
'value' => ['id_badge' => '2955']
]
I have a Elasticsearch setup which will allow user to search indexes as wild cards.
array:3 [
"index" => "users"
"type" => "user"
"body" => array:4 [
"from" => 0
"size" => 25
"sort" => array:1 [
1 => array:1 [
"order" => "asc"
]
]
"query" => array:1 [
"bool" => array:1 [
"should" => array:1 [
0 => array:1 [
0 => array:1 [
"wildcard" => array:1 [
"full_name" => "john doe"
]
]
]
]
]
]
]
]
when I pass this array to search function, it is returning an empty array. But there is a document related to "John Doe" and when I run "full_name" => "john" search is returning the document.
I feel that the problem is with the space.
{
"users": {
"user": {
"properties": {
"address": {
"type": "string"
},
"full_name": {
"type": "string"
},
"industry_name": {
"type": "string"
}
}
}
}
}
Assuming field full_name is analyzed by elasticsearch.
The problem in your case is fact that wildcard query doesn't analyze search string
Matches documents that have fields matching a wildcard expression (not
analyzed).
In you case it means, that elasticsearch stored john and doe tokens in inverted index, but wildcard query is searching for john doe token, and it fails.
What you can do about this:
Change index mapping, so full_name filed is not analyzed anymore.
Note: you will have to search for John Doe to get match, because
value wasn't analyzed so john doe won't match.
You can improve first solution, just by leaving full_name
analyzed, but with custom analyzer(wildcard, lowercase). It will
allow you to search for text john doe or John Doe.
{
"settings" : {
"index" : {
"analysis" : {
"analyzer" : {
"lowercase_analyzer" : {
"tokenizer" : "keyword",
"filter" : [
"lowercase"
],
"type" : "custom"
}
}
}
}
},
"mappings" : {
"user" : {
"properties" : {
"id" : {
"type" : "integer"
},
"fullName" : {
"analyzer" : "lowercase_analyzer",
"type" : "string"
}
}
}
}
}
You can take advantage of multi field, and search against raw
field.
"full_name.raw" => "John Doe"
Hope it will help you handle your use case.
UPDATE
Here you can find more information how to control index mapping.
I think standard tokenizer will be applied by default.
In that case, it will consider the text john doe as phrase.
So try phrase search
"full_name" => "\"john doe\""
If you want to consider spaces you could do something like:
{
"match" : {
"full_name" : {
"query" : "john doe",
"operator" : "and",
"zero_terms_query": "all"
}
}
}
check this: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html