Related
I want to write Elasticsearch query where I am able to look into a nested array, and check a key for all those elements inside the array, if all of them have that value then the root document should be selected.
this is a sample data :
[
{
"_index": "allproperties",
"_type": "_doc",
"_id": "5241050",
"_score": 0.0,
"_source": {
"id": 5241050,
"type": "HOUSE",
"state": "NSW",
"agency": {
"id": 31,
"code": "AU_LNT",
"name": "Luthor Properties",
"config": "{}"
},
"branch": {
"id": 89,
"name": "Luthor Sales Unit Trust",
"tradingName": "Luthor Properties",
"internalCode": "AU_LNT_G"
},
"images": null,
"leases": [
{
"id": 26439,
"bond": 2000,
"leased": true,
"source": "PORTAL",
"status": "SIGNED",
"endDate": "2022-11-06T00:00:00",
"tenants": [
{
"id": 11106,
"role": "TENANT",
"user": {
"id": 38817,
"dob": "1900-01-01",
"name": "Liam",
"email": "tempo#tempo.com",
"phone": "+61400000000",
"surname": "Tempo",
"salutation": "Mr.",
"invitations": null,
"lastInAppActivity": null
},
"address": "45675 Bruce Hwy, Coolbie QLD 4850, Australia",
"extendedData": "{\"rentSplitAmount\":22.62}",
"roleAdjective": "PRIMARY",
"addressComponents": {
"state": "QLD",
"suburb": "Coolbie",
"country": "Australia",
"postcode": "4850",
"streetName": "Bruce Hwy",
"unitNumber": null,
"poboxNumber": null,
"streetNumber": "45675",
"addressComponentsUnavailable": null
}
}
],
"signDate": "2022-10-05T04:58:08.887",
"startDate": "2022-10-06T00:00:00",
"moveInDate": "2022-10-06T00:00:00",
"appointments": [
{
"id": 3506,
"type": "REMOTE",
"date_time": "2022-10-12T04:56:00+00:00",
"createdBy": "Lex Luthor",
"createdDate": "2022-10-05T04:56:52.936",
"lastModifiedBy": "Lex Luthor",
"lastModifiedDate": "2022-10-05T04:56:53.51"
}
],
"createdDate": "2022-10-05T04:55:42.247",
"rentalAmount": 500,
"dateAvailable": null,
"extendedData": {
"last_rent_adjustment_date": 1665014400000
},
"leaseDuration": 32,
"rentalFrequency": "2",
"signedManually": false
}
],
"refId": "b66326eb-a6b2-42b6-b058-b46847e13399",
"source": "PT",
"status": null,
"suburb": "Hurstville",
"address": "456 Forest Road Hurstville NSW AUSTRALIA 2220",
"country": "Australia",
"bedrooms": 3,
"category": null,
"pmsCode": "84c34d15-a0ab-4791-b9e3-1bdac215b99c",
"postcode": "2220",
"agencyId": 31,
"bathrooms": 3,
"branchId": 89,
"carspaces": 3,
"createdBy": "system",
"streetname": "Forest Road",
"unitnumber": null,
"description": null,
"createdDate": "2022-10-05T04:51:12.619",
"entitlements": [
{
"id": 3453799,
"role": "LANDLORD",
"user": {
"id": 22855,
"dob": null,
"name": "please enter ownership name",
"email": "mag.m#rr.com.au",
"phone": "0400000000",
"surname": "",
"salutation": null,
"lastInAppActivity": null
},
"company": {
"id": 137,
"abn": "",
"acn": "",
"phone": "0400000000",
"address": "1234 Park Avenue New York NY USA 10037-1702",
"companyName": "please enter ownership name",
"displayName": "please enter ownership name",
"email": "mag.m#rr.com.au"
},
"roleAdjective": "GROUP"
},
{
"id": 3453800,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "MANAGING"
},
{
"id": 3453801,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "LEASING"
}
],
"streetnumber": "456",
"advertised": false,
"commission_type": null,
"lastModifiedBy": "system",
"lastModifiedDate": "2022-10-05T04:58:09.043",
"_meta": {
"branches": {
"id": [
89
]
},
"agencies": {
"id": [
31
]
},
"user_property_entitlement": {
"id": [
3453799,
3453800,
3453801
]
},
"users": {
"id": [
20054,
22855,
38817
]
},
"companies": {
"id": [
137
]
},
"leases": {
"id": [
26439
]
},
"user_lease_entitlement": {
"id": [
11106
]
},
"appointments": {
"id": [
3506
]
}
}
},
"sort": [
1664945472619,
0.0
]
}
]
In this particular case, leases is a nested document, I want to select the documents where all the leases are in cancelled state or leases is null, i.e; either all of the objects inside leases should have status as CANCELLED or the leases key should be null.
Already went through this question, but did'nt quite get it as its an old answer from 2015 and methods used in this got deprecated.
Try this query:
GET idx_test/_search?filter_path=hits.hits
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "leases",
"query": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"term": {
"leases.status.keyword": "CANCELLED"
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "leases"
}
}
]
}
}
]
}
}
]
}
}
}
}
]
}
}
}
I have products in my index. Documents are basically structured like these:
{
"_id": "product",
"_source": {
...
"type": "product",
"id": 1,
"mainTaxon": {
"name": "T-SHIRT",
},
"attributes": [
{
"code": "name",
"name": "Name",
"value": [
"BANANA T-SHIRT"
],
"score": 50
},
]
}
},
{
"_id": "product",
"_source": {
...
"type": "product",
"id": 2,
"mainTaxon": {
"name": "JEANS",
},
"attributes": [
{
"code": "name",
"name": "Name",
"value": [
"BANANA JEANS"
],
"score": 50
},
]
}
}
}
When I search for 'BANANA' I would prioritize products with mainTaxon different from JEANS. So, every product with the mainTaxon name T_SHIRT or something else would be listed before products with mainTaxon JEANS.
You can use boosting query to prioritize documents
{
"query": {
"boosting": {
"positive": {
"match": {
"attributes.value": "banana"
}
},
"negative": {
"match": {
"mainTaxon.name": "JEANS"
}
},
"negative_boost": 0.5
}
}
}
Search Result will be
"hits": [
{
"_index": "67164768",
"_type": "_doc",
"_id": "1",
"_score": 0.5364054,
"_source": {
"type": "product",
"id": 1,
"mainTaxon": {
"name": "T-SHIRT"
},
"attributes": [
{
"code": "name",
"name": "Name",
"value": [
"BANANA T-SHIRT"
],
"score": 50
}
]
}
},
{
"_index": "67164768",
"_type": "_doc",
"_id": "2",
"_score": 0.32743764,
"_source": {
"type": "product",
"id": 2,
"mainTaxon": {
"name": "JEANS"
},
"attributes": [
{
"code": "name",
"name": "Name",
"value": [
"BANANA JEANS"
],
"score": 50
}
]
}
}
]
I am reading The ElasticSearch Definitive Guide,about sorting on nested field in elastic search.
I am using Elastic Search 7.10.1
The following is the code that creates the index and prepares the data.
Create the index and setup the mapping
PUT /my_index
{
"mappings": {
"properties": {
"body": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"comments": {
"type": "nested",
"properties": {
"age": {
"type": "long"
},
"comment": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"date": {
"type": "date"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"stars": {
"type": "long"
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
Insert the data:
PUT /my_index/_doc/1
{
"title": "Nest eggs",
"body": "Making your money work...",
"tags": [
"cash",
"shares"
],
"comments": [
{
"name": "John Smith",
"comment": "Great article",
"age": 28,
"stars": 4,
"date": "2014-09-01"
},
{
"name": "Alice White",
"comment": "More like this please",
"age": 31,
"stars": 5,
"date": "2014-10-22"
}
]
}
PUT /my_index/_doc/2
{
"title": "Investment secrets",
"body": "What they don't tell you ...",
"tags": [
"shares",
"equities"
],
"comments": [
{
"name": "Mary Brown",
"comment": "Lies, lies, lies",
"age": 42,
"stars": 1,
"date": "2014-10-18"
},
{
"name": "John Smith",
"comment": "You're making it up!",
"age": 28,
"stars": 2,
"date": "2014-10-16"
}
]
}
query and sort against the comments.starts field
GET /my_index/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
},
"sort": {
"comments.stars": {
"order": "asc",
"mode": "min",
"nested_filter": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
}
}
The result is as follows:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"title" : "Nest eggs",
"body" : "Making your money work...",
"tags" : [
"cash",
"shares"
],
"comments" : [
{
"name" : "John Smith",
"comment" : "Great article",
"age" : 28,
"stars" : 4,
"date" : "2014-09-01"
},
{
"name" : "Alice White",
"comment" : "More like this please",
"age" : 31,
"stars" : 5,
"date" : "2014-10-22"
}
]
},
"sort" : [
9223372036854775807
]
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"title" : "Investment secrets",
"body" : "What they don't tell you ...",
"tags" : [
"shares",
"equities"
],
"comments" : [
{
"name" : "Mary Brown",
"comment" : "Lies, lies, lies",
"age" : 42,
"stars" : 1,
"date" : "2014-10-18"
},
{
"name" : "John Smith",
"comment" : "You're making it up!",
"age" : 28,
"stars" : 2,
"date" : "2014-10-16"
}
]
},
"sort" : [
9223372036854775807
]
}
]
}
}
I don't understand the result, I can't figure out how the starts is sorted. There is no difference if the have specied the "order":"asc" and "order": "desc",
You need to modify your sort query as shown below. Refer to this official documentation, to know more about nested sorting.
In the below sort query, first the documents are filtered on the basis of range query, then comments.stars value is sorted either in asc or desc order.
The mode is used to decide what to do in case a field has multiple comments.stars values. For example in your case, in the given range, there are two documents under the comments having stars value 1 and 2. When the mode is set to min, stars value 1 is considered.
{
"query": {
"nested": {
"path": "comments",
"query": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
},
"inner_hits": {}
}
},
"sort": [
{
"comments.stars": {
"order": "asc",
"mode": "min",
"nested": {
"path": "comments",
"filter": { // note this
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
}
}
]
}
Search Result will be
"hits": [
{
"_index": "66254927",
"_type": "_doc",
"_id": "6ZHts3cBlRfCLR0mFU3F",
"_score": null,
"_source": {
"title": "Investment secrets",
"body": "What they don't tell you ...",
"tags": [
"shares",
"equities"
],
"comments": [
{
"name": "Mary Brown",
"comment": "Lies, lies, lies",
"age": 42,
"stars": 1,
"date": "2014-10-18"
},
{
"name": "John Smith",
"comment": "You're making it up!",
"age": 28,
"stars": 2,
"date": "2014-10-16"
}
]
},
"sort": [
1
],
"inner_hits": {
"comments": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "66254927",
"_type": "_doc",
"_id": "6ZHts3cBlRfCLR0mFU3F",
"_nested": {
"field": "comments",
"offset": 0
},
"_score": 1.0,
"_source": {
"name": "Mary Brown",
"comment": "Lies, lies, lies",
"age": 42,
"stars": 1,
"date": "2014-10-18" // note this
}
},
{
"_index": "66254927",
"_type": "_doc",
"_id": "6ZHts3cBlRfCLR0mFU3F",
"_nested": {
"field": "comments",
"offset": 1
},
"_score": 1.0,
"_source": {
"name": "John Smith",
"comment": "You're making it up!",
"age": 28,
"stars": 2,
"date": "2014-10-16" // note this
}
}
]
}
}
}
},
{
"_index": "66254927",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"title": "Nest eggs",
"body": "Making your money work...",
"tags": [
"cash",
"shares"
],
"comments": [
{
"name": "John Smith",
"comment": "Great article",
"age": 28,
"stars": 4,
"date": "2014-09-01"
},
{
"name": "Alice White",
"comment": "More like this please",
"age": 31,
"stars": 5,
"date": "2014-10-22"
}
]
},
"sort": [
5
],
"inner_hits": {
"comments": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "66254927",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "comments",
"offset": 1
},
"_score": 1.0,
"_source": {
"name": "Alice White",
"comment": "More like this please",
"age": 31,
"stars": 5,
"date": "2014-10-22" // note this
}
}
]
}
}
}
}
]
In elasticsearch, let's say I have documents like
{
"name": "John",
"department": "Biology",
"address": "445 Mount Eden Road"
},
{
"name": "Jane",
"department": "Chemistry",
"address": "32 Wilson Street"
},
{
"name": "Laura",
"department": "BioTechnology",
"address": "21 Greens Road"
},
{
"name": "Mark",
"department": "Physics",
"address": "Random UNESCO Bio-reserve"
}
There is a use-case where, if I type "bio" in a search bar, I should get the matching field-value(s) from elasticsearch along with the field name.
For this example,
Input: "bio"
Expected Output:
{
"field": "department",
"value": "Biology"
},
{
"field": "department",
"value": "BioTechnology"
},
{
"field": "address",
"value": "Random UNESCO Bio-reserve"
}
What type of query should I use? I can think of using NGram Tokenizer and then use match query. But, I am not sure how shall I get only the matching field value (not the entire document) and the corresponding field name as the output.
After reading further about Completion Suggesters and Context Suggesters, I could solve this problem in the following way:
1) Keep a separate "suggest" field for each record with type "completion" with context-mapping of type "category". The mapping I created looks like as follows:
{
"properties": {
"suggest": {
"type": "completion",
"contexts": [
{
"name": "field_type",
"type": "category",
"path": "cat"
}
]
},
"name": {
"type": "text"
},
"department": {
"type": "text"
},
"address": {
"type": "text"
}
}
}
2) Then I insert the records as shown below (adding search metadata to the "suggest" field with proper "context").
For example, to insert the first record, I execute the following:
POST: localhost:9200/test_index/test_type/1
{
"suggest": [
{
"input": ["john"],
"contexts": {
"field_type": ["name"]
}
},
{
"input": ["biology"],
"contexts": {
"field_type": ["department"]
}
},
{
"input": ["445 mount eden road"],
"contexts": {
"field_type": ["address"]
}
}
],
"name": "john",
"department": "biology",
"address": "445 mount eden road"
}
3) If we want to search terms occurring in the middle of a sentence (as the search-term "bio" occurs in middle of the address field in the 4th record, we can index the entry as follows:
POST: localhost:9200/test_index/test_type/4
{
"suggest": [
{
"input": ["mark"],
"contexts": {
"field_type": ["name"]
}
},
{
"input": ["physics"],
"contexts": {
"field_type": ["department"]
}
},
{
"input": ["random unesco bio-reserve", "bio-reserve"],
"contexts": {
"field_type": ["address"]
}
}
],
"name": "mark",
"department": "physics",
"address": "random unesco bio-reserve"
}
4) Then search for the keyword "bio" like this:
localhost:9200/test_index/test_type/_search
{
"_source": false,
"suggest": {
"suggestion" : {
"text" : "bio",
"completion" : {
"field" : "suggest",
"size": 10,
"contexts": {
"field_type": [ "name", "department", "address" ]
}
}
}
}
}
The response:
{
"hits": {
"total": 0,
"max_score": 0,
"hits": []
},
"suggest": {
"suggestion": [
{
"text": "bio",
"offset": 0,
"length": 3,
"options": [
{
"text": "bio-reserve",
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"contexts": {
"field_type": [
"address"
]
}
},
{
"text": "biology",
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"contexts": {
"field_type": [
"department"
]
}
},
{
"text": "biotechnology",
"_index": "test_index",
"_type": "test_type",
"_id": "3",
"_score": 1,
"contexts": {
"field_type": [
"department"
]
}
}
]
}
]
}
}
Can anyone please suggest any better approach?
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
}
]
}
}
}