0

Just to let things clear, first day working with Elastic... Moving to the problem.

I started to create my index with

curl -XPUT "http://localhost:9200/users" -d'
{
   "mappings": {
      "user": {
         "properties": {
            "education": {
               "type": "nested"
            },
            "job": {
               "type": "nested"
            }
         }
      }
   }
}'

and then

curl -XPOST "http://localhost:9200/users/user/" -d'
   {
      "name": "User A",
      "education": [
         {
            "school": "School A1",
            "course": "Course A1"
         },
         {
            "school": "School A2",
            "course": "Course A2"
         }
      ]
   }'

The problem that I'm facing now is the query part. I'm trying to get results with:

curl -XPOST "http://localhost:9200/users/user/_search?pretty" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "education",
               "filter": {
                  "bool": {
                     "must": [
                        {
                           "term": {
                              "education.school": "School A1"
                           }
                        }
                     ]
                  }
               }
            }
         }
      }
   }
}'

But nothing is getting returned.

5
  • You don't have a school in education field with value School A Commented Feb 15, 2016 at 15:31
  • Fixed, but still no results Commented Feb 15, 2016 at 15:35
  • Show the mapping school inside education object. Is it analysed String Commented Feb 15, 2016 at 15:41
  • { "users" : { "mappings" : { "user" : { "properties" : { "education" : { "type" : "nested", "properties" : { "course" : { "type" : "string" }, "school" : { "type" : "string" } } }, "job" : { "type" : "nested" }, "name" : { "type" : "string" } } } } } } Commented Feb 15, 2016 at 15:44
  • By default standard analyser will be applied on all Strings. Have provided an answer. Please see if it works for you Commented Feb 15, 2016 at 15:59

2 Answers 2

1

As per the mappings provided by you, school field is analyzed. Analyzed means the text School A will split over space and will be tokenized as School and A. you are searching using term query which looks for exact term. Study here about term query.

You can use Query_string with default_operator as AND

curl -XPOST "http://localhost:9200/users/user/_search?pretty" -d'
{
  "query": {
  "filtered": {
     "query": {
        "match_all": {}
     },
     "filter": {
        "nested": {
           "path": "education",
           "filter": {
              "bool": {
                 "must": [
                    {
                       "query": {
                          "query_string": {
                             "default_field": "education.school",
                             "query": "School A1",
                             "default_operator": "AND"
                          }
                       }
                    }
                 ]
              }
             }
        }
     }
     }
    }
 }'
Sign up to request clarification or add additional context in comments.

Comments

0

Just leaving my 2 cents here. I would avoid using filtered query as it is being deprecated Check this in latest release of ES.

I'll just rewrite the above query without using filtered query

curl -XPOST "http://localhost:9200/users/user/_search?pretty" -d'
{
   "query": {
      "nested": {
         "path": "education",
         "query": {
            "bool": {
               "must": [
                  {
                     "query_string": {
                        "default_field": "education.school",
                        "query": "School A1",
                        "default_operator": "AND"
                     }
                  }
               ]
            }
         }
      }
   }
}'

I followed this doc to write above query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.