Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:23:36+00:00 2026-05-27T18:23:36+00:00

I can’t seem to understand the QueryDSL for facets in elastic search. Below are

  • 0

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" }
  }
},
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:23:37+00:00Added an answer on May 27, 2026 at 6:23 pm

    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
                    }
                ]
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we do following in SQL Server 2005 query with convert function. MaxRegID =
Can you set the internal [[Class]] property of an ECMAScript object?
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Can you guys help me understand a concept real quick, I'm having trouble understanding
can someone please help me understand what's going on here lists:dropwhile(fun(X) -> X <
Can someone help me understand why Win32 error codes don't match method signatures and
Can we add custom language for RecognizerIntent? I have search many SO Question like
can anyone tell me if it is possible to convert a dojo charting object
Can't work out a way to make an array of buttons in android. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.