# Filtering with script on nested fields

**URL:** <https://discuss.elastic.co/t/filtering-with-script-on-nested-fields/116844>\
**Category:** Elasticsearch\
**Created:** [January 24, 2018, 10:18am UTC](https://discuss.elastic.co/t/filtering-with-script-on-nested-fields/116844 "2018-01-24T10:18:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![romkaspb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/romkaspb/32/26879_2.png) [@romkaspb](https://discuss.elastic.co/u/romkaspb)\
**Post date:** [January 24, 2018, 10:18am UTC](https://discuss.elastic.co/t/filtering-with-script-on-nested-fields/116844/1 "2018-01-24T10:18:35Z")

</div>

Hey guys, I'm building search for my project.  
Now I need filter by nested field.  
Using Rails, so using elasticsearch gem.

Mapping:

```auto
    mapping do
        indexes 'name', type: 'string', analyzer: 'russian'
        indexes 'description', type: 'text', analyzer: 'russian'
        indexes 'short_description', type: 'string', analyzer: 'russian'
        indexes 'priority', type: 'integer'
        indexes 'cities_ru', type: 'string', analyzer: 'russian'
        indexes 'cities_en', type: 'string', analyzer: 'english'
        indexes 'lessons', type: 'nested' do
          indexes 'price', type: 'integer'
          indexes 'sold_out', type: 'boolean'
          indexes 'lesson_timetable', type: 'nested' do
            indexes 'date_by_agreement', type: 'boolean'
            indexes 'start_date', type: 'date'
            indexes 'end_date', type: 'date'
          end
        end
      end

```

Sample of doc:

```auto
{
       "_index": "development-courses",
       "_type": "course",
       "_id": "1976",
       "_score": 17.177029,
       "_source": {
         "name": "foo",
         "description": "bar",
         "short_description": "baz",
         "views": 11,
         "cities_ru": "",
         "cities_en": "",
         "lessons": [
           {
             "price": 0,
             "sold_out": false,
             "lesson_timetable": {
               "date_by_agreement": true,
               "start_date": null,
               "end_date": null
             }
           }
         ]
       },
       "sort": [
         11,
         17.177029
       ]
     }

```

Searching request:

```auto
GET /development-courses/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "fffaaa",
            "fields": [
              "name^3",
              "short_description",
              "description"
            ],
            "default_operator": "OR"
          }
        }
      ],
      "filter": {
        "nested": {
          "path": "lessons",
          "query": {
            "bool": {
              "must": [
                {
                  "exists": {
                    "field": "lessons.price"
                  }
                },
                {
                  "term": {
                    "lessons.sold_out": false
                  }
                }
              ],
              "should": {
                "nested": {
                  "path": "lessons.lesson_timetable",
                  "query": {
                    "bool": {
                      "should": [
                        {
                          "term": {
                            "lessons.lesson_timetable.date_by_agreement": true
                          }
                        },
                        {
                          "range": {
                            "lessons.lesson_timetable.start_date": {
                              "gte": "now"
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "sort": [
    {
      "views": "desc"
    },
    {
      "_score": "desc"
    }
  ]
}

```

but when I adding to"query"-\>"filter"-\>"nested"-\>"query"-\>"bool"-\>"must" this JSON:

```auto
                {
                  "script": {
                    "script": {
                      "source": "def valid=false; for(int i = 0; i < doc['lessons']; ++i){if(doc.lessons[i].price > 0){valid = true;}} return valid;"
                    }
                  }
                }

```

I'm getting an error "No field found for [lessons] in mapping with types []"

What is it? Can't understand whats wrong, because I've added indexes already \>.\<

Thank you!

---

<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 21, 2018, 10:19am UTC](https://discuss.elastic.co/t/filtering-with-script-on-nested-fields/116844/2 "2018-02-21T10:19:14Z")

</div>

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