Reputation: 81
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.
Upvotes: 0
Views: 40
Reputation: 51
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.
Upvotes: 0
Reputation: 7649
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"
}
}
}
]
}
}
}
}
}
}
}'
Upvotes: 1