# Issue with script accessing nested field

**URL:** <https://discuss.elastic.co/t/issue-with-script-accessing-nested-field/72781>\
**Category:** Elasticsearch\
**Created:** [January 25, 2017, 2:18pm UTC](https://discuss.elastic.co/t/issue-with-script-accessing-nested-field/72781 "2017-01-25T14:18:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diego.bernardes](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@diego.bernardes](https://discuss.elastic.co/u/diego.bernardes)\
**Post date:** [January 25, 2017, 2:18pm UTC](https://discuss.elastic.co/t/issue-with-script-accessing-nested-field/72781/1 "2017-01-25T14:18:14Z")

</div>

I work at a ecommerce company and we are rebuilding our search engine.  
One of our queries is to discover what kind of category a given user query represents, like **android** should return **smartphones** category.

But we don't have a category **android** , but real users when search for **android** usually clicks on products of **smartphones** category, so we built a app to track this clicks and we index along the category the most hitted queries.

This is the mapping:

```json
{
  "category": {
    "properties": {
      "name": {
        "type": "string",
        "fields": {
          
        }
      },
      "id": {
        "type": "long"
      },
      "queries": {
        "type": "nested",
        "properties": {
          "query": {
            "type": "string",
            "fields": {
              
            }
          },
          "count": {
            "type": "long"
          }
        }
      }
    }
  }
}

```

And i index documents like this:

```json
{
  "id": 1,
  "name": "Smartphones",
  "queries": [
    {
      "query": "android",
      "count": 100
    },
    {
      "query": "iphone",
      "count": 30
    },
    {
      "query": "moto g",
      "count": 20
    }
  ]
}

```

And i want to query they by name and also by the user query (there is only the user query on this es query):

```json
{
  "_source": [
    "name",
    "queries"
  ],
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "queries",
          "query": {
            "match": {
              "queries.query": {
                "query": "moto g",
                "type": "phrase"
              }
            }
          }
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "_score * (doc['queries.count'].value * 1.0)"
          }
        }
      ],
      "boost_mode": "replace",
      "score_mode": "max"
    }
  }
}

```

The problem is, don't know why, but the **queries.count** seems to be always 0, i need to use **nested** inside the filter to get the actual document hitted?

Just want to query for **moto g** and return the **smartphones** category.

Also we are using Elasticsearch 2.4.4.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 25, 2017, 2:33pm UTC](https://discuss.elastic.co/t/issue-with-script-accessing-nested-field/72781/2 "2017-01-25T14:33:49Z")

</div>

Have you considered running these sorts of queries against raw click data (query+clickedProduct+Category docs) rather than trying to roll all that behavioural information up into product documents? I demonstrated that approach at elasticon last year [1] using some click data and outlined some of the benefits of that approach.

[1] [https://www.elastic.co/elasticon/conf/2016/sf/graph-capabilities-in-the-elastic-stack](https://www.elastic.co/elasticon/conf/2016/sf/graph-capabilities-in-the-elastic-stack) (28 minutes into the presentation)

---

<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:** [February 22, 2017, 2:33pm UTC](https://discuss.elastic.co/t/issue-with-script-accessing-nested-field/72781/3 "2017-02-22T14:33:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
