Laravel: Advanced Polymorphic relationship - laravel

I am attempting to scope an answer to a User or Project via a polymorphic relationship via multiple levels but I am having some trouble getting my head around it.
It's tricky to explain, but I will show you what I have at the moment, minus the Answer model/relationship.
I have a Category and a Role model. These are in a belongsTo relationship (a role belongs to a category).
I also have a Question model, this also has a belongsTo relationship with a Role (a question belongs to a role).
I have created a roleables polymorphic relationship, allowing me to assign roles to either a User or a Project.
All of this is working as expected. I am able to return the assigned roles via my endpoint user/1/roles or project/1/roles, and in this endpoint, I am able to return the questions for that role (see example output below).
{
"data": [
{
"id": 20,
"name": "Actor",
"entity_id": 1,
"category_id": 3,
"questions": [
{
"id": 20,
"role_id": 20,
"field_type": "text",
"field_name": "Height",
"field_default_value": null,
"field_validation_rules": "required,string,max:5",
"field_options": false,
"order": 0
},
{
"id": 21,
"role_id": 20,
"field_type": "text",
"field_name": "Weight",
"field_default_value": null,
"field_validation_rules": "required,string,max:5",
"field_options": false,
"order": 1
},
{
"id": 22,
"role_id": 20,
"field_type": "select",
"field_name": "Race",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": [
{
"id": "white",
"name": "White"
},
{
"id": "mixed",
"name": "Mixed"
}
],
"order": 2
},
{
"id": 23,
"role_id": 20,
"field_type": "select",
"field_name": "Gender",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": [
{
"id": "male",
"name": "Male"
},
{
"id": "female",
"name": "Female"
},
{
"id": "other",
"name": "Other"
}
],
"order": 3
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 23,
"name": "Singer",
"entity_id": 1,
"category_id": 3,
"questions": [
{
"id": 26,
"role_id": 23,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 32,
"name": "Console Operator",
"entity_id": 1,
"category_id": 5,
"questions": [
{
"id": 35,
"role_id": 32,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 60,
"name": "Composer",
"entity_id": 1,
"category_id": 7,
"questions": [
{
"id": 63,
"role_id": 60,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
}
]
}
The problem that I am trying to get my head around is Answers to the questions. Just a little background, which may help me explain this in writing:
In the front-end, I have a page in which the user can update their information, on this page they also have the ability to assign themselves some roles. When the click on a role the list of questions is displayed and they have the ability to answer them.
The same is also true for a Project. A user can create projects, and have the ability to assigned roles and answer the relevant questions.
So, I guess I will need to create a new table in the database to store the answers.
The question is how/what relationship should this new answers table have to the question, user/project in order to get all the roles, questions and any answers scoped to the entity (user/project) and it's roles?
I would love the each role in the output from my user/1/roleendpoint to look more like the following (notice the new answers key in questions):
{
"id": 60,
"name": "Composer",
"entity_id": 1,
"category_id": 7,
"questions": [
{
"id": 63,
"role_id": 60,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0,
"answers": { ... }
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
}

Related

How to filter laravel collection from given data

i have a collection data
{
"success": true,
"doctor": [
{
"id": 1,
"name": "Dr. Mayank",
"dob": "1975-01-01",
"about": "This is description",
"status": 1,
"rating": 2,
"rating_given_by": 1,
"alternative_number": "7686876876",
"profile_photo": [],
"speciality": [
{
"id": 3,
"name": "Acupuncture",
"image": null,
"dashboard_flag": 1
},
{
"id": 4,
"name": "Acupuncturist",
"image": null,
"dashboard_flag": 1
},
{
"id": 1,
"name": "Accident and emergency medicine",
"image": "http://192.168.16.21/remidify/media/174/detail.png",
"dashboard_flag": 1
}
],
"service": [
{
"id": 78,
"name": "Correction of gummy smile",
"cost": "12.00"
},
{
"id": 77,
"name": "Dental aesthetics",
"cost": "43.00"
}
],
"clinics": [
{
"id": 1,
"name": "akram",
"entity_id": 1,
"entity_type": "App\Doctor",
"contact_number": "2132132132132",
"status": 0,
"consultancy_fee": "12.00",
"available_today": "No",
"owner_name": "Dr. Mayank",
"pivot": {
"doctor_id": 1,
"clinic_id": 1
},
"address": {
"id": 1,
"address_1": "asdasdasdsa",
"address_2": "",
"locality": "downtown",
"city": "noida",
"state": "up",
"postal_code": "41561566"
},
"speciality": [],
"service": [
{
"id": 11,
"name": "Laminates",
"cost": "20.00"
},
{
"id": 12,
"name": "Dental surgery",
"cost": "300.00"
}
],
"clinic_image": [
{
"id": 7,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "clinic_image",
"file_name": "1494863957588.566162.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/7/1494863957588.566162.jpg"
}
],
"id_image": [
{
"id": 8,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "id_image",
"file_name": "1494863966218.348877.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/8/1494863966218.348877.jpg"
}
],
"location": {
"id": 1,
"latitude": 0,
"longitude": 0,
"entity_id": 1,
"entity_type": "App\Clinic",
"created_at": "2017-05-16 03:00:10",
"updated_at": "2017-05-16 03:00:10"
},
"clinic_timings": [
{
"day": "sun",
"opens_at": "09:28:00",
"closes_at": "21:28:00"
}
]
}
],
"education": [
{
"id": 19,
"degree": "MBBS",
"university": "Univercity",
"year": "2017",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:11",
"updated_at": "2017-05-16 05:44:11",
"location": "Delhi"
}
],
"experience": [
{
"id": 19,
"hospital": "Hospital name",
"post": "pta ni hai",
"from": "1970-01-01",
"to": "0000-00-00",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:12",
"updated_at": "2017-05-16 05:44:12",
"location": "Locations12",
"is_currently_working": 1
}
],
"registration": {
"id": 1,
"registration_number": "Reg # 2324324",
"registration_year": 1975,
"registration_council": "Council",
"experience": null,
"doctor_id": 1,
"created_at": "2017-05-16 02:56:37",
"updated_at": "2017-05-16 02:56:37",
"adhaar_number": "232131231232",
"id_proof": [
{
"id": 2,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "id_proof",
"file_name": "1494863680447.329102.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/2/1494863680447.329102.jpg"
}
],
"registration_proof": [
{
"id": 3,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "registration_proof",
"file_name": "1494863687436.266846.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/3/1494863687436.266846.jpg"
}
],
"qualification_proof": [
{
"id": 4,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "qualification_proof",
"file_name": "1494863695576.803955.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/4/1494863695576.803955.jpg"
}
]
},
"preference": {
"availability": 1,
"appointment_confirmation_method": "manual",
"average_time": 7,
"holiday_from": null,
"holiday_till": null,
"patients_per_hour": null,
"preferred_appointment_type": "timeslot",
"appointment_frequency": null,
"preferred_payment_method": [
{
"payment_method": "cash"
},
{
"payment_method": "online"
}
]
},
"user": null,
"doctor_clinic": [
{
"doctor_id": 1,
"clinic_id": 1,
"consultancy_fee": "12.00",
"deleted_at": null,
"workdays": [
{
"day": "sun",
"available": 1,
"workhours": [
{
"from": "09:28:00",
"to": "21:28:00"
}
]
}
],
"service": []
}
]
}
]
}
Now how can i find the doctors whose "about or specialty name" matches with some given search string.
Thanks in advance.
Try using dd() helper and use like this below
$youcollectionvariables = { "success": true, "doctor": [ { "id": 1, "name": "Dr. Mayank", "dob": "1975-01-01"................. }
dd($youcollectionvariables)
you will see a formatted data and can find what you need clearly.
Hope that helps.

Error when saving composites id inside a for loop

I am trying to persist some objects that have a composite id. If I am only sending an array with one element it works fine, but it the array has more than one it throws an exception when saving the first one. public boolean
addParamsToChart(List<ChartParams> chartParams, Long chartId) {
List<ChartParams> chartParamsList = new ArrayList<>();
for(ChartParams chartParam: chartParams) {
ChartParamsId id = new ChartParamsId();
// id.setChartId(chartId);
id.setChartId(chartParam.getChart().getId());
id.setParamId(chartParam.getParam().getId());
id.setContextSourceId(chartParam.getContextSource().getId());
chartParam.setChartParamsId(id);
if(chartParamsRepository.save(chartParams) !=null) {
chartParamsList.add(chartParam);
}
}
if(chartParamsList.size()!=chartParams.size()) {
// something went wrong, delete previous inserted
deleteChartParams(chartParamsList);
chartRepository.delete(chartId);
return false;
}
return true;
}
[{
"chart": {
"id": 49,
"cv": {
"id": 1,
"name": "Money",
"category": {
"id": 1,
"name": "Euros"
},
"definition": "\"European curreny.\" [EU:euro]",
"enabled": true,
"cvid": "CC:1010"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Euro saving charts"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 3,
"name": "euro",
"internal": "eu",
"abbreviatedName": "eu"
} }]
But for this having two objects inside instead of one is not working, throwing an exception at the first save.
[{
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 6,
"name": "stock",
"internal": "st",
"abbreviatedName": "st"
} }, {
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 7,
"name": "Sold stock",
"internal": "st_sold",
"abbreviatedSequence": "st_sold"
} }]
The exception I got is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class eu.stocks.chart.chartParams.ChartParams

Laravel eloquent and query builder Eager Loading Multiple Relationships with where clause

I have few tables, they are room_types, rooms, rates, prices I want to get
room_types with rooms with rates and rates with prices where(price room_type_id = room_types.id) . I tried to do something like that
$roomTypeIds = [];
foreach ($hotel->rooms as $room) {
array_push($roomTypeIds, $room->roomtype->id);
}
$roomsByType = RoomType::with(['rooms' => function ($query) {
$query->with(['rates' => function($q) {
$q->with('policy', 'prices');
}]);
}])->whereIn('id', array_unique($roomTypeIds))->get();
but in this example Im getting all prices but I want to get only that prices which room_type_id IS EQUAL TO current room_type.id. So for that I have tried to do some think like this
$roomsByType = RoomType::with(['rooms' => function ($query) {
$query->with(['rates' => function($q) {
$q->with(['policy', 'prices' => function($q1) {
$q1->where('room_type_id', '');
}]);
}]);
}])->whereIn('id', array_unique($roomTypeIds))->get();
But I don't know what to put in where clouse. So are there any way to do this?
And result that I want to have is
[
{
"id": 2,
"name": "Twin/Double",
"created_at": "2017-12-11 08:56:16",
"updated_at": "2017-12-11 08:56:16",
"rooms": [
{
"id": 4,
"hotel_id": 1,
"room_type_id": 2,
"custom_name": null,
"room_name": "3",
"number_of_type": 2,
"number_of_bedrooms": null,
"number_of_livingrooms": null,
"number_of_bathrooms": null,
"created_at": "2017-12-12 06:37:34",
"updated_at": "2017-12-12 06:37:34",
"rates": [
{
"id": 4,
"user_id": 19,
"custom_name": "Default rate",
"price": 0,
"automatic": 0,
"rate_id": null,
"operand": null,
"amount": null,
"currency": null,
"policy_id": 1,
"meal_types": null,
"created_at": "2017-12-12 09:27:31",
"updated_at": "2017-12-12 09:27:29",
"pivot": {
"room_id": 4,
"rate_id": 4
},
"policy": {
"id": 1,
"name": "Free cancellation before 3 / 60 %",
"hotel_id": 1,
"free": 1,
"before_day": 3,
"before_day_price": 60,
"until_day_price": null,
"created_at": "2017-12-08 14:03:31",
"updated_at": "2017-12-08 14:03:31"
},
"prices": [
{
"id": 1,
"rate_id": 4,
"room_type_id": 2,
"from": "2017-12-01 09:18:46",
"to": "2017-12-18 09:18:57",
"amount": 100,
"created_at": "2017-12-18 09:19:11",
"updated_at": "2017-12-18 09:19:12"
},
{
"id": 3,
"rate_id": 4,
"room_type_id": 3,
"from": "2017-12-22 10:36:30",
"to": "2017-12-30 10:36:35",
"amount": 3000,
"created_at": null,
"updated_at": null
}
]
}
]
},
{
"id": 5,
"hotel_id": 1,
"room_type_id": 2,
"custom_name": null,
"room_name": "3",
"number_of_type": 2,
"number_of_bedrooms": null,
"number_of_livingrooms": null,
"number_of_bathrooms": null,
"created_at": "2017-12-12 06:37:34",
"updated_at": "2017-12-12 06:37:34",
"rates": [
{
"id": 4,
"user_id": 19,
"custom_name": "Default rate",
"price": 0,
"automatic": 0,
"rate_id": null,
"operand": null,
"amount": null,
"currency": null,
"policy_id": 1,
"meal_types": null,
"created_at": "2017-12-12 09:27:31",
"updated_at": "2017-12-12 09:27:29",
"pivot": {
"room_id": 5,
"rate_id": 4
},
"policy": {
"id": 1,
"name": "Free cancellation before 3 / 60 %",
"hotel_id": 1,
"free": 1,
"before_day": 3,
"before_day_price": 60,
"until_day_price": null,
"created_at": "2017-12-08 14:03:31",
"updated_at": "2017-12-08 14:03:31"
},
"prices": [
{
"id": 1,
"rate_id": 4,
"room_type_id": 2,
"from": "2017-12-01 09:18:46",
"to": "2017-12-18 09:18:57",
"amount": 100,
"created_at": "2017-12-18 09:19:11",
"updated_at": "2017-12-18 09:19:12"
},
{
"id": 3,
"rate_id": 4,
"room_type_id": 3,
"from": "2017-12-22 10:36:30",
"to": "2017-12-30 10:36:35",
"amount": 3000,
"created_at": null,
"updated_at": null
}
]
}
]
}
]
}
]
Look at the whereHas function instead of using with
https://laravel.com/docs/5.5/eloquent-relationships#querying-relations

Include Child Documents in Elastic Search Query

I am trying to include child documents into a query with elastic search, but I cannot figure it out. I have an entity called posts that has an optional parent to support a parent child hierarchy. I am hoping to do a query on the parent or top level items and include the top child with the results, preventing doing multiple calls to elastic search.
Mapping
{
"mappings": {
"post": {
"_parent": {
"type": "post"
},
"id": { "type": "integer"},
"text": { "type": "string"},
"parent_id": { "type": "integer"},
"rank": { "type": "integer"}
}
}
}
Desired Response
{
"hits": {
"total": 10,
"max_score": null,
"hits": [
{
"id": 1,
"text": "test",
"parent_id": null,
"rank": 10,
"topResponse": {
"id": 2,
"text": "test response",
"parent_id": null,
"rank": 4
}
},
{
"id": 1,
"text": "test 2",
"parent_id": null,
"rank": 8,
"topResponse": {
"id": 2,
"text": "test response",
"parent_id": null,
"rank": 3
}
},
{
"id": 1,
"text": "test 3",
"parent_id": null,
"rank": 6,
"topResponse": {
"id": 2,
"text": "test response",
"parent_id": null,
"rank": 1
}
}
]
}
}

I can't seem to get faceted search working in elastic search

I can't seem to understand the QueryDSL for facets in elastic search. Below are my query object, and the mapping for my tags array. I'm trying to get these to put in a faceted navigation based on tags. Each "element" will have multiple tags associated in the tags array [Not all elements will have tags. Some will have an empty array.]. Each tag is an object with id and tag properties.
I've tried the nested facet approach and get the error that "tags is not nested", so then I try this below. I don't receive an error, but there is no facets object in the return JSON. I've been using this page for help: http://www.elasticsearch.org/guide/reference/api/search/facets/index.html.
Can anyone help me to format this correctly and understand the organization of these? I appreciate any help!
// this is my query object
{
"sort":{ "created_at":{ "order":"desc" } },
"query":{
"constant_score":{
"filter":{
"and":[
{ "missing":{ "field":"parent_id" } },
{ "missing":{ "field":"wall_id" } },
{ "term":{ "active":true } }
]
}
}
},
"facets":{
"tags":{ "terms":{ "field":"tags.tag" } }
}
}
// this is the mapping for the tags array
"tags":{
"type":"nested",
"include_in_parent":true,
"properties":{
"id":{ "type":"integer" },
"tag":{ "type":"string" }
}
},
I've tried hard to replicate this (using 0.18.5) but without any luck. Some details below. The first example tries to replicate what you describe. The second doesn't map tags as include_in_parent and uses the "nested" field in the facet request. As you can see, in both cases the facet is returned.
This leads me to believe that there is either something wrong with the rest of the mappings or some document (not the sample one you provided) that is causing issues.
When you tried the nested approach, perhaps you used "nested":"tags.tag" instead of "nested":"tags"?
(Un)Successful attempt #1:
Create index with mappings:
$ curl -XPOST 'http://localhost:9200/testindex/' -d '{
"mappings": {
"element": {
"properties": {
"tags": {
"type": "nested",
"include_in_parent": true,
"properties": {
"id": {
"type": "integer"
},
"tag": {
"type": "string"
}
}
}
}
}
}
}'
Index document:
$ curl -XPOST 'http://localhost:9200/testindex/element' -d '{
"element_id": 4682,
"parent_id": null,
"wall_id": null,
"username": "John Doe",
"avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg",
"title": "Easy Chart is a great easy comparison chart maker for math and...",
"description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>",
"groups": [
{
"id": 6,
"name": "Fourth Grade (All Subjects)",
"name_short": "4th Grade"
},
{
"id": 17,
"name": "Eighth Grade Science",
"name_short": "8th Science"
},
{
"id": 13,
"name": "Seventh Grade Science",
"name_short": "7th Science"
},
{
"id": 9,
"name": "Sixth Grade Science",
"name_short": "6th Science"
}
],
"tags": [
{
"id": 33,
"tag": "iPad"
},
{
"id": 32,
"tag": "iPod"
}
],
"webpages": [],
"videos": [],
"documents": [],
"photos": [],
"reports": [],
"bookmarks": [],
"likes": [],
"dislikes": [],
"element_type": "Post",
"active": true,
"deleted": false,
"updated_at": "2011-11-27T21:37:38-0600",
"created_at": 1322451458000,
"created_at_formatted": "2 weeks ago"
}'
Search:
$ curl -XPOST 'http://localhost:9200/testindex/element/_search' -d '{
"sort":{ "created_at":{ "order":"desc" } },
"query":{
"constant_score":{
"filter":{
"and":[
{ "missing":{ "field":"parent_id" } },
{ "missing":{ "field":"wall_id" } },
{ "term":{ "active":true } }
]
}
}
},
"facets":{
"tags":{ "terms":{ "field":"tags.tag" } }
}
}'
Result:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "testindex",
"_type": "element",
"_id": "RZK41LngTKOhMUS6DXRi7w",
"_score": null,
"_source": {
"element_id": 4682,
"parent_id": null,
"wall_id": null,
"username": "John Doe",
"avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg",
"title": "Easy Chart is a great easy comparison chart maker for math and...",
"description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>",
"groups": [
{
"id": 6,
"name": "Fourth Grade (All Subjects)",
"name_short": "4th Grade"
},
{
"id": 17,
"name": "Eighth Grade Science",
"name_short": "8th Science"
},
{
"id": 13,
"name": "Seventh Grade Science",
"name_short": "7th Science"
},
{
"id": 9,
"name": "Sixth Grade Science",
"name_short": "6th Science"
}
],
"tags": [
{
"id": 33,
"tag": "iPad"
},
{
"id": 32,
"tag": "iPod"
}
],
"webpages": [],
"videos": [],
"documents": [],
"photos": [],
"reports": [],
"bookmarks": [],
"likes": [],
"dislikes": [],
"element_type": "Post",
"active": true,
"deleted": false,
"updated_at": "2011-11-27T21:37:38-0600",
"created_at": 1322451458000,
"created_at_formatted": "2 weeks ago"
},
"sort": [
1322451458000
]
}
]
},
"facets": {
"tags": {
"_type": "terms",
"missing": 0,
"total": 2,
"other": 0,
"terms": [
{
"term": "ipod",
"count": 1
},
{
"term": "ipad",
"count": 1
}
]
}
}
}
(Un)Successful attempt #2:
Create index with mappings:
$ curl -XPOST 'http://localhost:9200/testindex2/' -d '{
"mappings": {
"element": {
"properties": {
"tags": {
"type": "nested",
"include_in_parent": false,
"properties": {
"id": {
"type": "integer"
},
"tag": {
"type": "string"
}
}
}
}
}
}
}'
Index document:
$ curl -XPOST 'http://localhost:9200/testindex2/element' -d '{
"element_id": 4682,
"parent_id": null,
"wall_id": null,
"username": "John Doe",
"avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg",
"title": "Easy Chart is a great easy comparison chart maker for math and...",
"description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>",
"groups": [
{
"id": 6,
"name": "Fourth Grade (All Subjects)",
"name_short": "4th Grade"
},
{
"id": 17,
"name": "Eighth Grade Science",
"name_short": "8th Science"
},
{
"id": 13,
"name": "Seventh Grade Science",
"name_short": "7th Science"
},
{
"id": 9,
"name": "Sixth Grade Science",
"name_short": "6th Science"
}
],
"tags": [
{
"id": 33,
"tag": "iPad"
},
{
"id": 32,
"tag": "iPod"
}
],
"webpages": [],
"videos": [],
"documents": [],
"photos": [],
"reports": [],
"bookmarks": [],
"likes": [],
"dislikes": [],
"element_type": "Post",
"active": true,
"deleted": false,
"updated_at": "2011-11-27T21:37:38-0600",
"created_at": 1322451458000,
"created_at_formatted": "2 weeks ago"
}'
Search:
$ curl -XPOST 'http://localhost:9200/testindex2/element/_search' -d '{
"sort":{ "created_at":{ "order":"desc" } },
"query":{
"constant_score":{
"filter":{
"and":[
{ "missing":{ "field":"parent_id" } },
{ "missing":{ "field":"wall_id" } },
{ "term":{ "active":true } }
]
}
}
},
"facets":{
"tags":{ "terms":{ "field":"tags.tag" }, "nested":"tags" }
}
}'
Result:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "testindex2",
"_type": "element",
"_id": "_F1TTGJETOipo8kVR7ZXkQ",
"_score": null,
"_source": {
"element_id": 4682,
"parent_id": null,
"wall_id": null,
"username": "John Doe",
"avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg",
"title": "Easy Chart is a great easy comparison chart maker for math and...",
"description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>",
"groups": [
{
"id": 6,
"name": "Fourth Grade (All Subjects)",
"name_short": "4th Grade"
},
{
"id": 17,
"name": "Eighth Grade Science",
"name_short": "8th Science"
},
{
"id": 13,
"name": "Seventh Grade Science",
"name_short": "7th Science"
},
{
"id": 9,
"name": "Sixth Grade Science",
"name_short": "6th Science"
}
],
"tags": [
{
"id": 33,
"tag": "iPad"
},
{
"id": 32,
"tag": "iPod"
}
],
"webpages": [],
"videos": [],
"documents": [],
"photos": [],
"reports": [],
"bookmarks": [],
"likes": [],
"dislikes": [],
"element_type": "Post",
"active": true,
"deleted": false,
"updated_at": "2011-11-27T21:37:38-0600",
"created_at": 1322451458000,
"created_at_formatted": "2 weeks ago"
},
"sort": [
1322451458000
]
}
]
},
"facets": {
"tags": {
"_type": "terms",
"missing": 0,
"total": 2,
"other": 0,
"terms": [
{
"term": "ipod",
"count": 1
},
{
"term": "ipad",
"count": 1
}
]
}
}
}

Resources