# Facet search on nested fields

**URL:** <https://discuss.elastic.co/t/facet-search-on-nested-fields/7870>\
**Category:** Elasticsearch\
**Created:** [May 25, 2012, 6:03pm UTC](https://discuss.elastic.co/t/facet-search-on-nested-fields/7870 "2012-05-25T18:03:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Smolyak](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Michael\_Smolyak](https://discuss.elastic.co/u/Michael_Smolyak)\
**Post date:** [May 25, 2012, 6:03pm UTC](https://discuss.elastic.co/t/facet-search-on-nested-fields/7870/1 "2012-05-25T18:03:34Z")

</div>

Given a mapping and sample document below, I am trying to construct a  
query, which would return facet information on the fields of the nested  
objects (e.g., state.id or types.id). The mapping of serviceItem does not  
used 'nested' type. How do I include a facet for "state.id" in the query.  
When I include facets below, the one for 'launchUrl' returns results, while  
the one for 'state.id' does not. How can obtain facet information on fields  
of nested objects?

Thank you,

Michael

Facet fragment:

"facets": {  
"tag1": {  
"terms": {  
"fields": [  
"state.id"  
]  
}  
},  
"tag2": {  
"terms": {  
"fields": [  
"launchUrl"  
]  
}  
}  
}

Mapping fragment:

serviceItem: {

- properties: {
  - imageLargeUrl: {
    - include\_in\_all: true
    - index: not\_analyzed
    - type: string  
}

  - totalComments: {
    - include\_in\_all: false
    - type: integer  
}

  - launchUrl: {
    - include\_in\_all: true
    - index: not\_analyzed
    - type: string  
}

  - state: {
    - properties: {
      - id: {
        - include\_in\_all: false
        - index: no
        - type: long  
}

      - ref: {
        - include\_in\_all: false
        - index: no
        - type: string  
}

      - title: {
        - include\_in\_all: true
        - term\_vector: with\_positions\_offsets
        - type: string  
}

      - isPublished: {
        - index: not\_analyzed
        - type: boolean  
}

      - class: {
        - include\_in\_all: false
        - index: no
        - type: string  
}  
}  
}

A sample object:

{

- \_index: marketplace
- \_type: serviceItem
- \_id: 73
- \_version: 1
- \_score: 1
- \_source: {
  - imageLargeUrl:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
  - totalComments: 0
  - launchUrl:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
  - state: {
    - id: 12
    - class: marketplace.State
    - isPublished: true
    - title: Active  
}

  - editedDate: 2012-05-24T18:26:00.000Z
  - totalRate5: 0
  - screenshot2Url:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
  - totalRate4: 0
  - requirements: Req
  - author: {
    - id: 16
    - class: marketplace.Profile
    - avatar: null
    - displayName: Michael Parizer Admin
    - username: MikePAdmin  
}

  - organization: unknown
  - avgRate: 0
  - lastActivity: {
    - id: 85
    - class: marketplace.ModifyRelationshipActivity
    - activityDate: 2012-05-24T18:25:59.000Z
    - serviceItemVersion: 1  
}

  - title: Widget
  - releaseDate: 2012-01-30T05:00:00.000Z
  - totalRate2: 0
  - totalRate3: 0
  - dependencies: Dep
  - description: test
  - docUrl:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
  - totalRate1: 0
  - totalVotes: 0
  - approvalStatus: Approved
  - itemComments: []
  - types: {
    - id: 58
    - class: marketplace.Types
    - hasIcons: true
    - hasLaunchUrl: true
    - image: null
    - ozoneAware: true
    - title: Widget  
}

  - techPoc: MikePAdmin
  - screenshot1Url:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
  - customFields: []
  - serviceItemActivities: [
    - {
      - id: 80
      - class: marketplace.ServiceItemActivity
      - activityDate: 2012-05-24T18:25:56.000Z  
}

    - {
      - id: 85
      - class: marketplace.ModifyRelationshipActivity
      - activityDate: 2012-05-24T18:25:59.000Z
      - serviceItemVersion: 1  
}  
]

  - versionName: 1
  - approvedDate: null
  - owfProperties: {
    - id: 44
    - class: marketplace.OwfProperties
    - background: true
    - singleton: true
    - visibleInLaunch: true  
}

  - isHidden: 0
  - installUrl:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
  - categories: [
    - {
      - id: 24
      - class: marketplace.Category
      - title: Category C  
}

    - {
      - id: 12
      - class: marketplace.Category
      - title: Category A  
}

    - {
      - id: 33
      - class: marketplace.Category
      - title: Geospatial  
}

    - {
      - id: 18
      - class: marketplace.Category
      - title: Category B  
}

    - {
      - id: 47
      - class: marketplace.Category
      - title: Query  
}

    - {
      - id: 52
      - class: marketplace.Category
      - title: Reporting  
}

    - {
      - id: 57
      - class: marketplace.Category
      - title: Temporal  
}  
]

  - uuid: 02c71e13-aa1e-466c-b3e3-65c2602c4e3d
  - imageSmallUrl:  
[http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)  
}

}

---

<div class="post-metadata">

**Author:** ![Michael\_Smolyak](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Michael\_Smolyak](https://discuss.elastic.co/u/Michael_Smolyak)\
**Post date:** [May 25, 2012, 7:24pm UTC](https://discuss.elastic.co/t/facet-search-on-nested-fields/7870/2 "2012-05-25T19:24:25Z")

</div>

A correction to the original post:

I can facet on inner object fields that have unique names. For example,  
facet on "itemComments.text" works fine, while the one on  
"itemComments.class"

"facets": {  
"text": {  
"terms": {  
"fields": [  
"itemComments.class"  
]  
}  
},

returns:

facets: {

- text: {
  - \_type: terms
  - missing: 46
  - total: 0
  - other: 0
  - terms:   
}

The only difference is that field 'text only occurs under itemComments,  
while 'class' exists at other levels of object hierarchy. How do I deal  
with inner fields whose names are not unique across the document?

I do not mind renaming or aliasing these fields, but I do not know how. I  
am building the index using the Garls plugin, which gives rather limited  
options for configuring mapping.

Michael

On Friday, May 25, 2012 2:03:34 PM UTC-4, Michael Smolyak wrote:

> Given a mapping and sample document below, I am trying to construct a  
> query, which would return facet information on the fields of the nested  
> objects (e.g., state.id or types.id). The mapping of serviceItem does not  
> used 'nested' type. How do I include a facet for "state.id" in the query.  
> When I include facets below, the one for 'launchUrl' returns results, while  
> the one for 'state.id' does not. How can obtain facet information on  
> fields of nested objects?
> 
> Thank you,
> 
> Michael
> 
> Facet fragment:
> 
> "facets": {  
> "tag1": {  
> "terms": {  
> "fields": [  
> "state.id"  
> ]  
> }  
> },  
> "tag2": {  
> "terms": {  
> "fields": [  
> "launchUrl"  
> ]  
> }  
> }  
> }
> 
> Mapping fragment:
> 
> serviceItem: {
> 
> - properties: {
> - imageLargeUrl: {
> - include\_in\_all: true
> - index: not\_analyzed
> - type: string  
> }
> 
> - totalComments: {
> - include\_in\_all: false
> - type: integer  
> }
> 
> - launchUrl: {
> - include\_in\_all: true
> - index: not\_analyzed
> - type: string  
> }
> 
> - state: {
> - properties: {
> - id: {
> - include\_in\_all: false
> - index: no
> - type: long  
> }
> 
> - ref: {
> - include\_in\_all: false
> - index: no
> - type: string  
> }
> 
> - title: {
> - include\_in\_all: true
> - term\_vector: with\_positions\_offsets
> - type: string  
> }
> 
> - isPublished: {
> - index: not\_analyzed
> - type: boolean  
> }
> 
> - class: {
> - include\_in\_all: false
> - index: no
> - type: string  
> }  
> }  
> }
> 
> A sample object:
> 
> {
> 
> - \_index: marketplace
> - \_type: serviceItem
> - \_id: 73
> - \_version: 1
> - \_score: 1
> - \_source: {
> - imageLargeUrl:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
> - totalComments: 0
> - launchUrl:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
> - state: {
> - id: 12
> - class: marketplace.State
> - isPublished: true
> - title: Active  
> }
> 
> - editedDate: 2012-05-24T18:26:00.000Z
> - totalRate5: 0
> - screenshot2Url:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
> - totalRate4: 0
> - requirements: Req
> - author: {
> - id: 16
> - class: marketplace.Profile
> - avatar: null
> - displayName: Michael Parizer Admin
> - username: MikePAdmin  
> }
> 
> - organization: unknown
> - avgRate: 0
> - lastActivity: {
> - id: 85
> - class: marketplace.ModifyRelationshipActivity
> - activityDate: 2012-05-24T18:25:59.000Z
> - serviceItemVersion: 1  
> }
> 
> - title: Widget
> - releaseDate: 2012-01-30T05:00:00.000Z
> - totalRate2: 0
> - totalRate3: 0
> - dependencies: Dep
> - description: test
> - docUrl:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
> - totalRate1: 0
> - totalVotes: 0
> - approvalStatus: Approved
> - itemComments: 
> - types: {
> - id: 58
> - class: marketplace.Types
> - hasIcons: true
> - hasLaunchUrl: true
> - image: null
> - ozoneAware: true
> - title: Widget  
> }
> 
> - techPoc: MikePAdmin
> - screenshot1Url:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
> - customFields: 
> - serviceItemActivities: [
> - {
> - id: 80
> - class: marketplace.ServiceItemActivity
> - activityDate: 2012-05-24T18:25:56.000Z  
> }
> 
> - {
> - id: 85
> - class: marketplace.ModifyRelationshipActivity
> - activityDate: 2012-05-24T18:25:59.000Z
> - serviceItemVersion: 1  
> }  
> ]
> 
> - versionName: 1
> - approvedDate: null
> - owfProperties: {
> - id: 44
> - class: marketplace.OwfProperties
> - background: true
> - singleton: true
> - visibleInLaunch: true  
> }
> 
> - isHidden: 0
> - installUrl:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)
> - categories: [
> - {
> - id: 24
> - class: marketplace.Category
> - title: Category C  
> }
> 
> - {
> - id: 12
> - class: marketplace.Category
> - title: Category A  
> }
> 
> - {
> - id: 33
> - class: marketplace.Category
> - title: Geospatial  
> }
> 
> - {
> - id: 18
> - class: marketplace.Category
> - title: Category B  
> }
> 
> - {
> - id: 47
> - class: marketplace.Category
> - title: Query  
> }
> 
> - {
> - id: 52
> - class: marketplace.Category
> - title: Reporting  
> }
> 
> - {
> - id: 57
> - class: marketplace.Category
> - title: Temporal  
> }  
> ]
> 
> - uuid: 02c71e13-aa1e-466c-b3e3-65c2602c4e3d
> - imageSmallUrl:  
> [http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png](http://www.freeiconsweb.com/Icons-show/IconsExtra/Phone%20Blue.png)  
> }
> 
> }

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:27am UTC](https://discuss.elastic.co/t/facet-search-on-nested-fields/7870/3 "2017-07-06T03:27:03Z")

</div>


