I can't seem to get faceted search working in elastic search - faceted-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
}
]
}
}
}

Related

How to make geo Location sorting work in elastic

I'm trying to sort by location. Similar data and tests work correctly in Elasticsearch but fail using Elastic App Search (latest 8.3 version). The results should be in the following order "Item-2, Item-3, Item-1", instead they are in this order "Item-1, Item-2, Item-3".
Request body:
{
"query": "",
"sort": {
"location": {
"center": [
0,
14
],
"order": "asc"
}
},
"page": {
"size": 10,
"current": 1
}
}
Response body
{
"meta": {
"alerts": [],
"warnings": [],
"precision": 2,
"engine": {
"name": "test-core-item",
"type": "default"
},
"page": {
"current": 1,
"total_pages": 1,
"total_results": 6,
"size": 10
},
"request_id": "c8f5aaaa71d9f152f203f5effd995031"
},
"results": [
{
"location": {
"raw": "0.0,0.0"
},
"_meta": {
"id": "Item-1",
"engine": "test-core-item",
"score": null
},
"id": {
"raw": "Item-1"
}
},
{
"location": {
"raw": "0.0,10.0"
},
"_meta": {
"id": "Item-2",
"engine": "test-core-item",
"score": null
},
"id": {
"raw": "Item-2"
}
},
{
"location": {
"raw": "0.0,20.0"
},
"_meta": {
"id": "Item-3",
"engine": "test-core-item",
"score": null
},
"id": {
"raw": "Item-3"
}
}
]
}
According to the docs Geolocation raw data can be repsesented in different ways, e.g. as a string of ", " or as an array with elements [, ] (see docs). Notice that when passing as string latitude is passed first but when passing as an array longitude is first.
Please mind the difference in the order of coordinates and either pass
"center": [14, 0] or
"center": "0, 14"
So in the example above, the center should have been passed as [14, 0] instead of [0, 14].

Search with more than one parameter over more than one field in elastic search

I only want to return this course if 'Grade' = 'G6' and Type = 'Open' are a match in the SAME audience tag, they must exist in the SAME tag to return this course. Currently this course is returned if it finds G6 and OPEN is DIFFERENT audiences which is not what I want.
This is not correct and i am getting incorrect data back, I need to query to apply in each audience and only return data if it is true in the same audience
here is my json:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 71,
"max_score": 3.3118114,
"hits": [
{
"_index": "courses",
"_type": "course",
"_id": "LBTBWdzyRw-jgiiYssjv8A",
"_score": 3.3118114,
"_source": {
"id": "LBTBWdzyRw-jgiiYssjv8A",
"title": "1503 regression testing",
"shortDescription": "asdf",
"description": "asdf",
"learningOutcomes": "",
"modules": [],
"learningProvider": {
"id": "ig2-zIY_QkSpMC4O0Lm0hw",
"name": null,
"termsAndConditions": [],
"cancellationPolicies": []
},
"audiences": [
{
"id": "VfDpsS_5SXi8iZubzTkUBQ",
"name": "comm",
"areasOfWork": [
"Communications"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "OPEN",
"eventId": null
},
{
"id": "eZPPPqTqRdiDAE3xCPlJMQ",
"name": "analysis",
"areasOfWork": [
"Analysis"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "REQUIRED",
"eventId": null
}
],
"preparation": "",
"owner": {
"scope": "LOCAL",
"organisationalUnit": "co",
"profession": 63,
"supplier": ""
},
"visibility": "PUBLIC",
"status": "Published",
"topicId": ""
}
}
]
}
}
My ES Code:
BoolQueryBuilder boolQuery = boolQuery();
boolQuery.should(QueryBuilders.matchQuery("audiences.departments.keyword", department));
boolQuery.should(QueryBuilders.matchQuery("audiences.areasOfWork.keyword", areaOfWork));
boolQuery.should(QueryBuilders.matchQuery("audiences.interests.keyword", interest));
BoolQueryBuilder filterQuery = boolQuery();
filterQuery.must(QueryBuilders.matchQuery("audiences.grades.keyword", "G6"));
filterQuery.must(QueryBuilders.matchQuery("audiences.type", "OPEN"));
Here is index mapping:
{
"media": {
"aliases": {}
},
"courses": {
"aliases": {}
},
"feedback": {
"aliases": {}
},
"learning-providers": {
"aliases": {}
},
"resources": {
"aliases": {}
},
"courses-0.4.0": {
"aliases": {}
},
".security-6": {
"aliases": {
".security": {}
}
},
"payments": {
"aliases": {}
}
}
Since you want your query to apply in each audience and only return data if it is true in the same audience, you need to specify nested datatype for audiences field otherwise ElasticSearch stores it in form of Objects and it doesnt have concept of nested objects because of which Elasticsearch flattens object hierarchies into a simple list of field names and values.You can refer this for more detail https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
Taking your example suppose this was your document :
"audiences": [
{
"id": "1",
"field": "comm"
},
{
"id": "2",
"field": "arts"
}
]
Elasticsearch flattens in the form of :
{
"audiences.id":[1,2],
"audiences.field":[comm,arts]
}
Now here if you search query says that audience must have id:1 and field:arts then also above document will get matched.
So, in order to avoid this such type of objects should be defined as nested object. ElasticSearch will store each object separately instead of flattening it as a result each object will be searched separately.
Mapping of your above mentioned document should be :
Mapping
{
"mappings": {
"properties": {
"shortDescription": {
"type": "text"
},
"audiences": {
"type": "nested"
},
"description": {
"type": "text"
},
"modules": {
"type": "text"
},
"preparation": {
"type": "text"
},
"owner": {
"properties": {
"scope": {
"type": "text"
},
"organisationalUnit": {
"type": "text"
},
"profession": {
"type": "text"
},
"supplier": {
"type": "text"
}
}
},
"learningProvider": {
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"termsAndConditions": {
"type": "text"
},
"cancellationPolicies": {
"type": "text"
}
}
},
"visibility": {
"type": "text"
},
"status": {
"type": "text"
},
"topicId": {
"type": "text"
}
}
}
}
Now, if we index this document :
Document
{
"shortDescription": "asdf",
"description": "asdf",
"learningOutcomes": "",
"modules": [],
"learningProvider": {
"id": "ig2-zIY_QkSpMC4O0Lm0hw",
"name": null,
"termsAndConditions": [],
"cancellationPolicies": []
},
"audiences": [
{
"id": "VfDpsS_5SXi8iZubzTkUBQ",
"name": "comm",
"areasOfWork": [
"Communications"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "OPEN",
"eventId": null
},
{
"id": "eZPPPqTqRdiDAE3xCPlJMQ",
"name": "analysis",
"areasOfWork": [
"Analysis"
],
"departments": [],
"grades": [
"G7"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "REQUIRED",
"eventId": null
}
],
"preparation": "",
"owner": {
"scope": "LOCAL",
"organisationalUnit": "co",
"profession": 63,
"supplier": ""
},
"visibility": "PUBLIC",
"status": "Published",
"topicId": ""
}
If you search query is this :
Search Query 1
:
{
"query": {
"nested": {
"path": "audiences",
"query": {
"bool": {
"must": [
{
"match": {
"audiences.type.keyword": "OPEN"
}
},
{
"match": {
"audiences.grades.keyword": "G6"
}
}
]
}
}
}
}
}
Result
"hits": [
{
"_index": "product",
"_type": "_doc",
"_id": "1",
"_score": 0.9343092,
"_source": {
"shortDescription": "asdf",
"description": "asdf",
"learningOutcomes": "",
"modules": [],
"learningProvider": {
"id": "ig2-zIY_QkSpMC4O0Lm0hw",
"name": null,
"termsAndConditions": [],
"cancellationPolicies": []
},
"audiences": [
{
"id": "VfDpsS_5SXi8iZubzTkUBQ",
"name": "comm",
"areasOfWork": [
"Communications"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "OPEN",
"eventId": null
},
{
"id": "eZPPPqTqRdiDAE3xCPlJMQ",
"name": "analysis",
"areasOfWork": [
"Analysis"
],
"departments": [],
"grades": [
"G7"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "REQUIRED",
"eventId": null
}
],
"preparation": "",
"owner": {
"scope": "LOCAL",
"organisationalUnit": "co",
"profession": 63,
"supplier": ""
},
"visibility": "PUBLIC",
"status": "Published",
"topicId": ""
}
}
]
But now if your search query is :
Search Query 2 :
{
"query": {
"nested": {
"path": "audiences",
"query": {
"bool": {
"must": [
{
"match": {
"audiences.type.keyword": "OPEN"
}
},
{
"match": {
"audiences.grades.keyword": "G7"
}
}
]
}
}
}
}
}
Result :
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
So, in short you need to change datatype of audiences field in your mapping and your rest query as well so that it can search for nested datatype.
So, instead of this code fragment :
BoolQueryBuilder filterQuery = boolQuery();
filterQuery.must(QueryBuilders.matchQuery("audiences.grades.keyword", "G6"));
filterQuery.must(QueryBuilders.matchQuery("audiences.type", "OPEN"));
you should use this nested query :
BoolQueryBuilder filterQuery = new BoolQueryBuilder();
filterQuery.must(QueryBuilders.matchQuery("audiences.grades.keyword", "G6"));
filterQuery.must(QueryBuilders.matchQuery("audiences.type", "OPEN"));
NestedQueryBuilder nested = new NestedQueryBuilder("audiences", filterQuery, ScoreMode.None);

How should I write the query to get only nested properties in elasticsearch

I have the index as below.
{
"id": {
"type": "keyword"
},
"title": {
"type": "text"
},
"comments": {
"type": "nested"
}
}
And I put some docs as below.
{
"id": 1,
"title": "Types",
"comments": [
{
"id": 113,
"contents": "Number"
},
{
"id": 2005,
"contents": "String"
},
{
"id": 317,
"contents": "Boolean"
}
]
}
{
"id": 2,
"title": "Animals",
"comments": [
{
"id": 45,
"contents": "Dog"
},
{
"id": 175,
"contents": "Cat"
},
{
"id": 26,
"contents": "Pig"
}
]
}
{
"id": 3,
"title": "Colors",
"comments": [
{
"id": 97,
"contents": "Red"
},
{
"id": 28,
"contents": "Green"
},
{
"id": 56,
"contents": "Blue"
}
]
}
When I use a nested query as below that the total count is just 3 but I want to get the 9 (All about nested properties).
{
"query": {
"nested": {
"path": "comment",
"query": {
"match_all": {}
}
}
}
}
I got the result as below.
{
"hits": {
"total": 3,
"hits": [
{
"_source": {
"id": 1,
"title": "Types",
"comments": [...]
}
},
{
"_source": {
"id": 2,
"title": "Animals",
"comments": [...]
}
},
{
"_source": {
"id": 3,
"title": "Colors",
"comments": [...]
}
}
]
}
}
But I wanted the result is like below format.
{
"hits": {
"total": 9,
"hits": [
{
"_source": {
"id": 113,
"contents": "Number"
}
},
...,
{
"_source": {
"id": 56,
"contents": "Blue"
}
}
]
}
}
How should I write the query to get the only 9 nested properties as I wanted result like above?
You need to use nested inner_hits like this:
{
"_source": false, <-- add this to prevent the parent doc from showing up
"query": {
"nested": {
"path": "comment",
"query": {
"match_all": {}
},
"inner_hits": {} <-- and this to only show the matching nested documents
}
}
}

elasticsearch get specific fields

I am trying to search data from following indexed data
PUT /nested_test100/t/1
{
"title": "Nest eggs1",
"body": "Making your money work...",
"tags": [ "cash1", "shares1" ],
"comments": [
{
"name": "John Smith1",
"comment": "Great article1",
"age": 28,
"stars": 1,
"date": "2014-09-01"
},
{
"name": "Alice White1",
"comment": "More like this please1",
"age": 31,
"stars": 1,
"date": "2014-10-22"
}
]
}
PUT /nested_test100/t/2
{
"title": "Nest eggs2",
"body": "Making your money work...",
"tags": [ "cash", "shares" ],
"comments": [
{
"name": "John Smith2",
"comment": "Great article2",
"age": 30,
"stars": 2,
"date": "2014-09-01"
},
{
"name": "Alice White2",
"comment": "More like this please2",
"age": 31,
"stars": 2,
"date": "2014-10-22"
}
]
}
PUT /nested_test100/t/3
{
"title": "Nest eggs3",
"body": "Making your money work...",
"tags": [ "cash3", "shares3" ],
"comments": [
{
"name": "John Smith3",
"comment": "Great article3",
"age": 28,
"stars": 3,
"date": "2014-09-01"
},
{
"name": "Alice White3",
"comment": "More like this please3",
"age": 30,
"stars": 3,
"date": "2014-10-22"
}
]
}
GET /nested_test100/t/_search
what I want is to get title, body tags only and only comments having age=28.
How I should write query dsl for that.
what I have written is following
POST /nested_test100/t/_search
{
"fields" : ["title","comments.age","body","tags"],
"query" : {
"term" : { "comments.age" : "28" }
}
}
and its giving me data like this
"hits": [
{
"_index": "nested_test100",
"_type": "t",
"_id": "1",
"_score": 1,
"fields": {
"comments.age": [
28,
31
],
"title": [
"Nest eggs1"
],
"body": [
"Making your money work..."
],
"tags": [
"cash1",
"shares1"
]
}
},
{
"_index": "nested_test100",
"_type": "t",
"_id": "3",
"_score": 1,
"fields": {
"comments.age": [
28,
30
],
"title": [
"Nest eggs3"
],
"body": [
"Making your money work..."
],
"tags": [
"cash3",
"shares3"
]
}
}
]
but I don't want comments having age other than 28.
I am using elasticsearch version 1.7
Please use inner_hits to include nested inner objects as inner hits to a search hit.
The search request would be like :
POST test/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"match": {"comments.age" : 28}
},
"inner_hits": {}
}
}
}
P.S : You will need to map the comments as a nested field (as already mentioned by Val)

Query/Filter by nested objects getting unexpected results in some nested objects

I'm having weird results on some nested objects that I can't understand.
I mean, in mapping the nested objects are all the same, they have an id and a tree id also, both are long and thats it, but profession and category do not return the expected values, but the others do (professionType, professionSubtype and attributeType).
So I got index mapped as:
Mapping:
{
"attribute-tree": {
"mappings": {
"attribute": {
"dynamic": "strict",
"properties": {
"attributeType": {
"properties": {
"id": {"type": "long"},
"tree": {"type": "long"}
}
},
"category": {
"properties": {
"id": {"type": "long"},
"tree": {"type": "long"}
}
},
"family": {"type": "long"},
"id": {"type": "long"},
"name": {
"type": "string",
"index": "not_analyzed"
},
"parentTree": {"type": "long"},
"profession": {
"properties": {
"id": {"type": "long"},
"tree": {"type": "long"}
}
},
"professionSubtype": {
"properties": {
"id": {"type": "long"},
"tree": {"type": "long"}
}
},
"professionType": {
"properties": {
"id": {"type": "long"},
"tree": {"type": "long"}
}
},
"sorter": {
"properties": {
"id": {"type": "long"},
"name": {
"type": "string",
"index": "not_analyzed"
},
"tree": {"type": "long"}
}
},
"suggester": {
"type": "completion",
"index_analyzer": "edgeNGram_analyzer",
"search_analyzer": "whitespace_analyzer",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50
},
"tree": {"type": "long"},
"type": {"type": "string"}
}
},
"division": {
// same as "attribute"
},
"profession-subtype": {
// same as "attribute"
},
"profession-type": {
// same as "attribute"
},
"profession": {
// same as "attribute"
},
"category": {
// same as "attribute"
}
}
}
}
If any kind of information is missing, please say
Thanks in advance
Example
Filtering by category.id:
POST /attribute-tree/_search
{
"size": 2,
"query": {
"filtered": {
"filter": {
"term": {"category.id": 1}
}
}
}
}
I get:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 31,
"max_score": 1,
"hits": [
{
"_index": "attribute-tree",
"_type": "category",
"_id": "4064",
"_score": 1,
"_source": {
"id": 1,
"tree": 4064,
"name": "Profession",
"type": "C",
"parentTree": 4063,
"profession": {
"id": null,
"tree": null
},
"professionType": {
"id": 1,
"tree": 1
},
"professionSubtype": {
"id": 6,
"tree": 4063
},
"category": {
"id": null,
"tree": null
},
"attributeType": {
"id": null,
"tree": null
},
"family": [
4063,
1
],
"suggester": {
"input": [
"Profession"
],
"output": "Profession"
},
"sorter": {
"name": "Profession",
"id": 1,
"tree": 4064
}
}
},
{
"_index": "attribute-tree",
"_type": "category",
"_id": "4083",
"_score": 1,
"_source": {
"id": 1,
"tree": 4083,
"name": "Profession",
"type": "C",
"parentTree": 4082,
"profession": {
"id": null,
"tree": null
},
"professionType": {
"id": 2,
"tree": 4072
},
"professionSubtype": {
"id": 8,
"tree": 4082
},
"category": {
"id": null,
"tree": null
},
"attributeType": {
"id": null,
"tree": null
},
"family": [
4082,
4072
],
"suggester": {
"input": [
"Profession"
],
"output": "Profession"
},
"sorter": {
"name": "Profession",
"id": 1,
"tree": 4083
}
}
}
]
}
}
Notice that on this example the category.id doesn't have the expected value (this is also valid for when it is 0 instead of null)
However I have nodes with category.id = 1:
POST /attribute-tree/_search
{
"size": 2,
"query": {
"filtered": {
"filter": {
"term": {"tree": 4}
}
}
}
}
Item:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "attribute-tree",
"_type": "profession",
"_id": "4",
"_score": 1,
"_source": {
"id": 1,
"tree": 4,
"name": "A&R Administrator",
"type": "P",
"parentTree": 3,
"profession": {
"id": null,
"tree": null
},
"professionType": {
"id": 1,
"tree": 1
},
"professionSubtype": {
"id": 1,
"tree": 2
},
"category": {
"id": 1,
"tree": 3
},
"attributeType": {
"id": null,
"tree": null
},
"family": [
3,
2,
1
],
"suggester": {
"input": [
"A&R",
"Administrator"
],
"output": "A&R Administrator"
},
"sorter": {
"name": "A&R Administrator",
"id": 1,
"tree": 4
}
}
}
]
}
}
Filtering by professionType.id:
POST /attribute-tree/_search
{
"size": 2,
"query": {
"filtered": {
"filter": {
"term": {"professionSubtype.id": 1}
}
}
}
}
I get:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3890,
"max_score": 1,
"hits": [
{
"_index": "attribute-tree",
"_type": "category",
"_id": "251",
"_score": 1,
"_source": {
"id": 4,
"tree": 251,
"name": "Medium",
"type": "C",
"parentTree": 2,
"profession": {
"id": null,
"tree": null
},
"professionType": {
"id": 1,
"tree": 1
},
"professionSubtype": {
"id": 1,
"tree": 2
},
"category": {
"id": null,
"tree": null
},
"attributeType": {
"id": null,
"tree": null
},
"family": [
2,
1
],
"suggester": {
"input": [
"Medium"
],
"output": "Medium"
},
"sorter": {
"name": "Medium",
"id": 4,
"tree": 251
}
}
},
{
"_index": "attribute-tree",
"_type": "profession",
"_id": "4",
"_score": 1,
"_source": {
"id": 1,
"tree": 4,
"name": "A&R Administrator",
"type": "P",
"parentTree": 3,
"profession": {
"id": null,
"tree": null
},
"professionType": {
"id": 1,
"tree": 1
},
"professionSubtype": {
"id": 1,
"tree": 2
},
"category": {
"id": 1,
"tree": 3
},
"attributeType": {
"id": null,
"tree": null
},
"family": [
3,
2,
1
],
"suggester": {
"input": [
"A&R",
"Administrator"
],
"output": "A&R Administrator"
},
"sorter": {
"name": "A&R Administrator",
"id": 1,
"tree": 4
}
}
}
]
}
}
Also tested as:
POST /attribute-tree/_search
{
"query":{
"filtered": {
"query": {"match_all":{}},
"filter": {
"nested": {
"path": "category",
"filter": {
"bool": {
"must": [
{"term": {"category.id": 1}}
]
}
}
}
}
}
}
}
But this gives an error
org.elasticsearch.index.query.QueryParsingException: [attribute-tree] [nested] nested object under path [category] is not of nested type
From the mapping it looks like the index has a type of category as well as a field in attribute type called category.
To allow for proper field resolution and disambiguate between field id in type category vs field category.id in type attribute you would need to specify the entire path to the field including the type i.e <type>.<fieldname>.
Example:
POST /attribute-tree/_search
{
"size": 2,
"query": {
"filtered": {
"filter": {
"term": {"attribute.category.id": 1}
}
}
}
}
This issue thread has more discussion with regard to this.

Resources