Filtering with script on nested fields

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

Mapping:

    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:

{
       "_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:

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:

                {
                  "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!

1 Like

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