I am working with drupal 8. I am trying to get the JSON of all nodes of the content type. I got a json as given bellow. But Now I want to change the Following JSON to
[
{
"nid": [
{
"value": "17"
}
],
"uuid": [
{
"value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
}
],
"vid": [
{
"value": "17"
}
],
"type": [
{
"target_id": "resume_creator"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "uyi"
}
],
"uid": [
{
"target_id": "1"
}
],
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1452060690"
}
],
"changed": [
{
"value": "1452060709"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "0"
}
],
"revision_timestamp": [
{
"value": "1452060709"
}
],
"revision_uid": [
{
"target_id": "1"
}
],
"revision_log": [],
"revision_translation_affected": [
{
"value": "1"
}
],
"default_langcode": [
{
"value": "1"
}
],
"path": [],
"field_communication_address": [
{
"value": "rtyrtytr\r\nuu;\r\nsdgfdh"
}
],
"field_education": [
{
"value": "ytutyuii"
}
],
"field_emails": [
{
"value": "gtf#fgfg.com"
}
],
"field_experiece": [
{
"value": "fghtutyu"
}
],
"field_name": [
{
"value": "ytt"
}
]
}
]
to a format of
[
{
"nid":"17",
"uuid":"3614e0c8-88d4-4e8d-a732-5089698556d5",
"vid": "17",
"type":"resume_creator",
"langcode":"en",
"title":"uyi",
"uid":"1",
"status":"1",
"created":"1452060690",
"changed":"1452060709",
"promote":"1",
"sticky":"0",
"revision_timestamp":"1452060709",
"revision_uid":"1",
"revision_log": [],
"path":[],
"field_communication_address":"rtyrtytr\r\nuu;\r\nsdgfdh",
"field_education":"ytutyuii",
"field_emails":"gtf#fgfg.com",
"field_experiece":"fghtutyu",
"field_name":"ytt"
}
]
using php. Then only I can manage a form angular js. Thanks in advance
Try this
$json = '{
"nid": [
{
"value": "17"
}
],
"uuid": [
{
"value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
}
],
"vid": [
{
"value": "17"
}
],
"type": [
{
"target_id": "resume_creator"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "uyi"
}
],
"uid": [
{
"target_id": "1"
}
],
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1452060690"
}
],
"changed": [
{
"value": "1452060709"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "0"
}
],
"revision_timestamp": [
{
"value": "1452060709"
}
],
"revision_uid": [
{
"target_id": "1"
}
],
"revision_log": [],
"revision_translation_affected": [
{
"value": "1"
}
],
"default_langcode": [
{
"value": "1"
}
],
"path": [],
"field_communication_address": [
{
"value": "rtyrtytr\r\nuu;\r\nsdgfdh"
}
],
"field_education": [
{
"value": "ytutyuii"
}
],
"field_emails": [
{
"value": "gtf#fgfg.com"
}
],
"field_experiece": [
{
"value": "fghtutyu"
}
],
"field_name": [
{
"value": "ytt"
}
]
}';
$json = json_decode($json,true);
foreach ($json as $key => $value){
if(isset($json[$key][0]['value'])){
$json[$key] = $json[$key][0]['value'];
}
if(isset($json[$key][0]['target_id'])){
$json[$key] = $json[$key][0]['target_id'];
}
// $json[$key] = $json[$key][0]['value'];
}
$json = json_encode($json);
print_r($json);
It is simple.
<?php
$arr = array('nid' => 17, 'uuid' => '3614e0c8-88d4-4e8d-a732-5089698556d5', ...);
echo json_encode($arr);
?>
If you have some misunderstanding, ask me.
Related
Is there a way in elasticsearch to give more priority for the prefix match than to the string that contains that word?
For ex.- priorities of words if I search for ram should be like this:
Ram Reddy
Joy Ram Das
Kiran Ram Goel
Swati Ram Goel
Ramesh Singh
I have tried mapping as given in here.
I have done like this:
$params = [
"index" => $myIndex,
"body" => [
"settings"=> [
"analysis"=> [
"analyzer"=> [
"start_with_analyzer"=> [
"tokenizer"=> "my_edge_ngram",
"filter"=> [
"lowercase"
]
]
],
"tokenizer"=> [
"my_edge_ngram"=> [
"type"=> "edge_ngram",
"min_gram"=> 3,
"max_gram"=> 15
]
]
]
],
"mappings"=> [
"doc"=> [
"properties"=> [
"label"=> [
"type"=> "text",
"fields"=> [
"keyword"=> [
"type"=> "keyword"
],
"ngramed"=> [
"type"=> "text",
"analyzer"=> "start_with_analyzer"
]
]
]
]
]
]
]
];
$response = $client->indices()->create($params); // create an index
and searching like this:
$body = [
"size" => 100,
'_source' => $select,
"query"=> [
"bool"=> [
"should"=> [
[
"query_string"=> [
"query"=> "ram*",
"fields"=> [
"value"
],
"boost"=> 5
]
],
[
"query_string"=> [
"query"=> "ram*",
"fields"=> [
"value.ngramed"
],
"analyzer"=> "start_with_analyzer",
"boost"=> 2
]
]
],
"minimum_should_match"=> 1
]
]
];
$params = [
'index' => $myIndex,
'type' => $myType,
'body' => []
];
$params['body'] = $body;
$response = $client->search($params);
The json of query is as follows:
{
"size": 100,
"_source": [
"label",
"value",
"type",
"sr"
],
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "ram*",
"fields": [
"value"
],
"boost": 5
}
},
{
"query_string": {
"query": "ram*",
"fields": [
"value.ngramed"
],
"analyzer": "start_with_analyzer",
"boost": 2
}
}
],
"minimum_should_match": 1,
"must_not": {
"match_phrase": {
"type": "propertyValue"
}
}
}
}
}
I am using elasticsearch 5.3.2
Is there any other way to sort the results for the search in the relational database using the search method in php?
You should not enable fielddata unless really required. To overcome this you can use sub field.
Make the following changes to your code:
"label"=>[
"type"=>"text",
//"fielddata"=> true, ---->remove/comment this line
"analyzer"=>"whitespace",
"fields"=>[
"keyword"=>[
"type"=>"keyword"
]
]
]
To sort on type field use type.keyword instead. This change apply to any field of text type and has a sub-field of type keyword available (assuming the name of this field is keyword). So change as below:
'sort' => [
["type.keyword"=>["order"=>"asc"]],
["sr"=>["order"=>"asc"]],
["propLabels"=>["order"=>"asc"]],
["value"=>["order"=>"asc"]]
]
Update : Index creation and query to get desired output
Create the index as below:
{
"settings": {
"analysis": {
"analyzer": {
"start_with_analyzer": {
"tokenizer": "my_edge_ngram",
"filter": [
"lowercase"
]
}
},
"tokenizer": {
"my_edge_ngram": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 15
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
},
"ngramed": {
"type": "text"
}
}
}
}
}
}
}
Use the query below to get the desired result:
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "Ram",
"fields": [
"name"
],
"boost": 5
}
},
{
"query_string": {
"query": "Ram",
"fields": [
"name.ngramed"
],
"analyzer": "start_with_analyzer",
"boost": 2
}
}
],
"minimum_should_match": 1
}
}
}
In the above the query with boost value 5 increases the score for those documents where Ram is present in name. The other query with boost 2 further increases the score for the documents where name starts with Ram.
Sample O/P:
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "2",
"_score": 2.0137746,
"_source": {
"name": "Ram Reddy"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.4384104,
"_source": {
"name": "Joy Ram Das"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "3",
"_score": 0.5753642,
"_source": {
"name": "Ramesh Singh"
}
}
]
I have the following expression: "noapte bună" and I'm trying to get the same result when I'm searching for "bună" or "buna".
I have followed to tutorial here : https://www.elastic.co/guide/en/elasticsearch/guide/current/asciifolding-token-filter.html but to no result.
This is my code:
$params = ['index' => 'asciiv3', 'body' => [
"settings" => [
"analysis" => [
"analyzer" => [
"folding" => [
"tokenizer" => "standard",
"filter" => [ "lowercase", "asciifolding" ]
]
]
]
],
"mappings" => [
"asciiv3" => [
"properties" => [
"saying" => [
"type" => "string",
"analyzer" => "standard",
"fields" => [
"folded" => [
"type" => "string",
"analyzer" => "folding"
]
]
]
]
]
]
]];
self::$instance->indices()->create($params);
and this is the query array:
'multi_match' =>
array(
"type" => "most_fields",
"query" => "bună",
"fields" => [ "saying", "saying.folded" ]
)
Does anyone know what I'm doing wrong?
It works for me. This is my setup:
PUT asciiv3
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"asciiv3": {
"properties": {
"saying": {
"type": "string",
"analyzer": "standard",
"fields": {
"folded": {
"type": "string",
"analyzer": "folding"
}
}
}
}
}
}
}
POST /asciiv3/asciiv3/1
{
"saying":"bună ziua"
}
POST /asciiv3/asciiv3/2
{
"saying":"buna ziua"
}
GET /asciiv3/_search
{
"query": {
"multi_match": {
"type": "most_fields",
"query": "bună",
"fields": [
"saying",
"saying.folded"
]
}
}
}
With these results:
"hits": {
"total": 2,
"max_score": 0.2712221,
"hits": [
{
"_index": "asciiv3",
"_type": "asciiv3",
"_id": "1",
"_score": 0.2712221,
"_source": {
"saying": "bună ziua"
}
},
{
"_index": "asciiv3",
"_type": "asciiv3",
"_id": "2",
"_score": 0.028130025,
"_source": {
"saying": "buna ziua"
}
}
]
}
How can I parsing JSON file in PHP with structure like this.
{
"url": "http://lotto.mthai.com",
"result": {
"extractorData": {
"url": "http://lotto.mthai.com",
"resourceId": "85407efc3b77dc0c03350101dbe4d644",
"data": [
{
"group": [
{
"title": [
{
"text": "รางวัลที่ 1"
}
],
"number": [
{
"text": "511825"
}
]
},
{
"title": [
{
"text": "เลขท้าย 2 ตัว"
}
],
"number": [
{
"text": "14"
}
]
},
{
"title": [
{
"text": "รางวัลเลขหน้า 3 ตัว"
}
],
"number": [
{
"text": "111 775"
}
]
},
{
"title": [
{
"text": "รางวัลเลขท้าย 3 ตัว"
}
],
"number": [
{
"text": "880 937"
}
]
},
{
"title": [
{
"text": "รางวัลข้างเคียงรางวัลที่ 1"
}
],
"number": [
{
"text": "511824 511826"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล รางวัลที่ 2"
}
],
"number": [
{
"text": "041316 051696 174632 262383 301461"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 3"
}
],
"number": [
{
"text": "151368 192853 218010 381563 441994 593440 608530 958702 960236 991980"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 4"
}
],
"number": [
{
"text": "005297 143532 183328 308705 457316 561361 692884 813010 891564 984090 027175 151262 203467 309366 464915 580426 725352 835838 895659 986826 034358 160471 207682 426987 477041 626565 727202 840597 927093 990225 055970 175123 221131 428397 542542 674499 761363 883250 936443 994258 100858 175672 285542 434884 558734 692634 795866 888874 967956 996299"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 5"
}
],
"number": [
{
"text": "018823 074535 190371 313865 352942 456747 541518 709349 861423 919589 030122 085212 200070 318968 359324 474167 571715 734617 862093 924308 047376 102785 206781 320576 376900 481372 573928 739412 863966 928572 049905 128721 209050 324031 386258 505554 599073 767614 867382 958601 051129 132159 227324 326875 396408 506582 637094 786639 869522 960148 051242 137750 265952 327702 405928 507898 654254 788560 878668 962359 052418 156993 269239 330428 416971 508844 664963 805061 881503 971836 054447 157814 286374 330762 434541 526775 705692 811851 890915 983191 060979 176311 301301 336489 451863 529673 707115 819033 902456 983528 061270 184563 308537 352516 455643 535855 707396 829248 908825 999541"
}
]
}
]
}
]
},
"pageData": {
"statusCode": 200,
"timestamp": 1464855539981
}
}
}
json file exported from import.io
I want to parsing title and number in group and keep it in array.
I tried many ways but always got error.
Thanks you.
Unless I misunderstood your question you could organize your JSON like below:
"group": [
{
"title": "Some text",
"number": "511825"
},
{
"title": "Some text 2",
"number": "98989"
},
My mapping is (part of it):
$index = [
"mappings" => [
"goods" => [
"dynamic_templates"=> [
[
"iattribute_id"=> [
"match_mapping_type"=> "string",
"match"=> "attribute_id",
"mapping"=> [
"type"=> "integer"
]
]
],
[
"iattribute_value"=> [
"match_mapping_type"=> "string",
"match"=> "attribute_value",
"mapping"=> [
"type"=> "string",
"index" => "not_analyzed"
]
]
]
],
"properties" => [
...
"individual_attributes" => [
"type" => "nested",
"properties" => [
"template_id" => ["type" => "integer"],
"attributes_set" => [
"type" => "nested",
"properties" => [
"attribute_id" => ["type" => "integer"],
"attribute_value" => ["type" => "string", "index" => "not_analyzed"]
]
]
]
]
...
]
]
]
];
How can I query attribute_id and attribute_value? They are nested inside nested. I can't understand how to specify path to fields.
I've composed query but it doesn't work.
GET /index/type/_search
{
"query" : {
"nested" : {
"path" : "individual_attributes.attributes_set",
"score_mode" : "none",
"filter": {
"bool": {
"must": [
{
"term" : {
"individual_attributes.attributes_set.attribute_id": "20"
}
},
{
"term" : {
"individual_attributes.attributes_set.attribute_value": "commodi"
}
}
]
}
}
}
}
}
Try this:
{
"query": {
"nested": {
"path": "individual_attributes",
"score_mode": "none",
"filter": {
"nested": {
"path": "individual_attributes.attributes_set",
"query": {
"bool": {
"must": [
{
"term": {
"individual_attributes.attributes_set.attribute_id": "20"
}
},
{
"term": {
"individual_attributes.attributes_set.attribute_value": "commodi"
}
}
]
}
}
}
}
}
}
}
For 24hrs I have been really struggling to find how to access the email and address fields. I think I've tried every combination there is
my last attempt
$obj = json_decode($json);
$emails = $obj->emails->address;
JSON OUTPUT
{
"#http_status_code": 200,
"#visible_sources": 1,
"#available_sources": 1,
"#search_id": "1507310336407868146373527620323253083",
"query": {
"emails": [
{
"address": "paulsparktest#googlemail.com",
"address_md5": "d92590f691eab834586686c563567382"
}
]
},
"possible_persons": [
{
"#search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1",
"names": [
{
"first": "Paul",
"last": "Johnson",
"display": "Paul Johnson"
}
],
"usernames": [
{
"content": "pauljohnson111"
}
],
"gender": {
"#inferred": true,
"content": "male"
},
"addresses": [
{
"country": "GB",
"state": "ENG",
"city": "London",
"display": "London, England"
}
],
"user_ids": [
{
"content": "374967130#twitter"
}
],
"images": [
{
"url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg",
"thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
}
]
}
]
}
This will get the address for you
$obj->query->emails[0]->address;
if you want to iterate and get other addresses
foreach ( $obj->query->emails as $email ) {
echo $email->address;
}
You can get address & name via follow code.
// Getting email
echo $data->query->emails[0]->address;
// Getting name
echo $data->possible_persons[0]->names[0]->display;
Full code
$data = '{
"#http_status_code": 200,
"#visible_sources": 1,
"#available_sources": 1,
"#search_id": "1507310336407868146373527620323253083",
"query": {
"emails": [
{
"address": "paulsparktest#googlemail.com",
"address_md5": "d92590f691eab834586686c563567382"
}
]
},
"possible_persons": [
{
"#search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1",
"names": [
{
"first": "Paul",
"last": "Johnson",
"display": "Paul Johnson"
}
],
"usernames": [
{
"content": "pauljohnson111"
}
],
"gender": {
"#inferred": true,
"content": "male"
},
"addresses": [
{
"country": "GB",
"state": "ENG",
"city": "London",
"display": "London, England"
}
],
"user_ids": [
{
"content": "374967130#twitter"
}
],
"images": [
{
"url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg",
"thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
}
]
}
]
}';
$data = json_decode($data);
// Getting email
echo $data->query->emails[0]->address;
// Getting name
echo $data->possible_persons[0]->names[0]->display;