Solr stores indexed Documents values in array - php

I am indexing multiple documents to Solr cloud with on query via API and json like this:
[
{
"id": "1",
"title": "Doc 1",
"author": "exmaple"
},
{
"id": "2",
"title": "Doc 2",
"author": "exmaple"
}
]
The documents are indexed, including all values, but some values are stored in an array in solr:
"response":{"numFound":2,"start":0,"maxScore":1.0,"docs":[
{
"id":"1",
"title":["Doc 1"],
"author":"exmaple",
"author_s":"exmaple",
"_version_":1631766743831543808},
{
"id":"2",
"title":["Doc 2"],
"author":"exmaple",
"author_s":"exmaple",
"_version_":1631766743831543808}]
}
Does anyone have an idea why in this case title is stored in an array?

You must have added the attribute as multivalued = true for the field Title.
This attribute is useful when there are more than one value present for particular field. If don't want to store the field in multivalue form, remove the attribute for the same field.
Remove the same and restart the server.
Re-index the data.

Related

Any way to prevent Doctrine result to nest entities when adding custom field to QueryBuilder?

I am adding a calculated field named 'distance' to a Doctrine query but the result is then nesting the entities as follow:
[
{
"0": {
"name": "Some name",
"id": 3
},
"distance": "10"
},
{
...
]
Is there a way to tell Doctrine to format the response like this instead?
[
{
"name": "Some name",
"id": 3
"distance": "10"
},
{
...
]
I don't always add this field as it depends of the search criteria, so I am having inconsistent result format.
Also I can prevent the issue by adding the distance field as HIDDEN, but then I lose the distance information, which I would like to keep.
Any help appreciated, thanks.

I want a more consolidate json format

I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding

Podio PHP: category is empty when no value for item

We are using the Podio PHP library to create forms for our users. These forms load their data from Podio trough the Podio PHP library. A form is linked to a specific Podio Item ID for this.
With this ID we get the fields and their possible values. Most of them are Category fields. We get the values for this trough:
<?php
$item_data = PodioItem::get_basic( $podio_item_id );
$car_types = $item_data->fields["car-type"]->config['settings']['options'];
var_dump( $car_types );
?>
But the curious thing is, if the item in Podio has no value selected for this category, getting the options fail. It's empty. It doesn't exists.
How do we populate a form with values even if the item has no options selected for it?
Podio doesn't store empty value, it's empty, it doesn't exist, so there is nothing to store. In fact, what you are probably looking for is Application definition, which has list fields. Then category field has config with list of possible values and id's and colors.
Like this:
"fields": [
{
"status": "active",
"type": "category",
"field_id": 81772,
"label": "Status",
"config": {
"required": true,
"label": "Status",
.....some other config values .....
"settings": {
"multiple": false,
"options": [
{
"status": "deleted", <= example of deleted category option
"text": "Not groomed",
"id": 13,
"color": "DCEBD8"
},
{
"status": "active", <= example of active category option
"text": "Open",
"id": 1,
"color": "FFD5C2"
},
.... <= other category options goes here
You can read more about Apps here: https://developers.podio.com/doc/applications/get-app-22349

Getting array element which has minimum value on a specific field on the element

I'm trying to remove an element from an array inside a collection. To remove the element, I have to look at the index (or date) field, and remove the one which has the lowest value. An example representation of my collection named "pages":
{
"_id": {
"$oid": "52e12df7e4b06e4ed65a554c"
},
"posts": [
{
"postId": {
"$oid": "52e12e5933a9fbec1100002d"
},
"date": 1390489177.267876,
"index": 1
},
{
"postId": {
"$oid": "52e12e5f33a9fb141800002c"
},
"date": 1390489183.277084,
"index": 2
}
],
"skillname": "Bilardo",
"skilltag": "Bilardo",
"currentIndex": 2
}
I need to remove this element from the posts array:
{
"postId": {
"$oid": "52e12e5933a9fbec1100002d"
},
"date": 1390489177.267876,
"index": 1
}
Whatever I would do, I could not manage to find the minimum "index" field in the array. The last php code I achieved to write is below:
$this->db->pages->find(array( 'skilltag' => 'Bilardo'), array('posts.index' => 1))->sort( array("posts.index" => -1));
I'm not willing to use "index" fields and also "currentIndex" field. But I put them anyways in case of not being able to work on "date" field which has timestamp values.
The above code returns an array with only one element which is an array that holds 2 elements:
{
"postId": {
"$oid": "52e12e5933a9fbec1100002d"
},
"date": 1390489177.267876,
"index": 1
},
{
"postId": {
"$oid": "52e12e5f33a9fb141800002c"
},
"date": 1390489183.277084,
"index": 2
}
Isn't there an aggregation function which does filtering on inside the array? I could not find any aggregation that returns the minimum value. I only found aggregation to return values between min and max values which must be given by the user -- which does not work in my case.
Are the embedded objects in your posts array field always ordered by their date and index, ascending? Based on your schema, I would assume that new posts are added to the document via the $push update operator.
If the element to remove is always at the front of the array, you could very easily use $pop to remove it.

Comparing JSON arrays by a specific key in PHP

I want to use the data from array A (below), but only when the item ID from array A does NOT match an ID from items in array B (also, below). How would I go about comparing these two JSON array's by the key of ID (from items) via PHP? I imagine I first need to convert them with json_decode, but I'm not sure where to go after that?
Please note that array B has more nests ("items", "something", & "posts"), unlike array A. I want to compare the ID from items, not posts.
Array A:
{
"data": [{
"category": "Games",
"id": "45345"
},
{
"category": "Music",
"id": "345345345"
},
{
"category": "Food",
"id": "1"
},
{
"category": "Pets",
"id": "13245345"
}]
}
Array B:
{
"data": {
"something": "blah",
"posts": [{
"id": "34241",
"title": "orange"
}],
"items": [{
"id": "1",
"name": "orange"
},
{
"id": "2",
"name": "dog"
},
{
"id": "3",
"name": "cat"
},
{
"id": "4",
"name": "apple"
}]
}
}
With the case above, it would run through array A and output everything from array A except for the third item, since the id of that item (1) matches one of the id's in array B items.
Based on my understanding, you need a two step process. The first is extracting the ids from the first JSON blob, and the second is filtering the second JSON blob. So basically, we have map and filter. And it just so happens we can use PHP's inbuilt functions for this:
$ids = array_map(
function($value) {
return $value['id'];
},
$array2['data']['items']
);
First, we flatten the second array's items element into the individual ids. We "map" over the data.items array, and return the $id attribute of each array. Now, we have an array of ids...
$new = array_filter(
$array1['data'],
function($var) use ($ids) {
return !in_array($var['id'], $ids);
}
);
Now, we use that to filter the first blobs array to determine if an element is new or not. So we use array filter to handle it for us. All we need to do is check the $ids array to see if the current data's id is there (and if it is, throw it away). So we want to filter the array to be only variables that are not in array of $ids (hence !in_array($var['id'], $ids)...)
Decode the items into PHP arrays. Use a SPL like array_diff() to get the results of a diff comparison.
Referances to get you started:
http://www.php.net/manual/en/function.array-diff.php
http://php.net/manual/en/function.array-diff-key.php
http://www.php.net/manual/en/function.json-decode.php
Should be about what your looking for

Categories