Reputation: 5582
I'm new in Elastic Search
and using ES in my application. When I run simple query on single table on ES then it working file.. But When I'm using nested query then it is not giving me proper result.
Basically, I have two tables people
and interactions
both are separate tables. And interaction table has person_id which refer to people table id. and I want to fetch those interaction of people which have user_id: 2
. When I'm creating condition and running the query then I'm getting error.
Below is my query which I'm running.
{
"index": "my_index",
"type": "people",
"fields": "_source,_timestamp",
"size": 10,
"from": 0,
"body": {
"query": {
"bool": {
"should": {
"wildcard": {
"_all": "*a*"
}
}
},
"nested": {
"path": "interactions",
"query": {
"bool": {
"should": {
"match": {
"interactions.user_id": 2
}
}
}
}
}
},
"sort": [
{
"last_name": {
"order": "asc"
}
}
]
}
}
Below is the response which I'm getting
{
"status": false,
"error_code": 657,
"errors": [
"parse_exception: failed to parse search source. expected field name but got [START_OBJECT]"
]
}
Below is the data which we index in ES
People
[
{
"id": 1,
"first_name": "Test1",
"last_name": "Data1",
"date_of_birth": "1988-11-02",
"created_at": ".......",
"updated_at": ".......",
"status": 1,
"prefix": "Ms.",
"suffix": "MD"
},
{
"id": 1,
"first_name": "Test2",
"last_name": "Data2",
"date_of_birth": "1988-11-02",
"created_at": ".......",
"updated_at": ".......",
"status": 1,
"prefix": "Ms.",
"suffix": "MD"
}
]
Interactions
[
{
"id": 1,
"user_id": 11,
"person_id": 6,
"interaction_type": 1,
"initiated_by": 2,
"created_at": ".......",
"updated_at": "......."
},
{
"id": 2,
"user_id": 10,
"person_id": 5,
..........
}
]
Can anyone suggest me what I'm doing wrong in this query? Thanks in Advance.
Upvotes: 1
Views: 2104
Reputation: 2077
Updated answer. Try this.
{
"fields" : "_source,_timestamp",
"size" : 10,
"from" : 0,
"query" : {
"bool" : {
"should" : [{
"wildcard" : {
"_all" : "*a*"
}
}, {
"nested" : {
"path" : "interactions",
"query" : {
"bool" : {
"should" : {
"match" : {
"interactions.user_id" : 2
}
}
}
}
}
}
]
}
},
"sort" : [{
"last_name" : {
"order" : "asc"
}
}
]
}
Your json was malformed. You were missing a ']' for the Sort. Try the below
You can use a site like http://jsonlint.com/ to validate the jsons.
{
"index": "my_index",
"type": "people",
"fields": "_source,_timestamp",
"size": 10,
"from": 0,
"body": {
"query": {
"bool": {
"should": {
"wildcard": {
"_all": "*a*"
}
}
},
"nested": {
"path": "interactions",
"query": {
"bool": {
"should": {
"match": {
"interactions.user_id": 2
}
}
}
}
}
},
"sort": [{
"last_name": {
"order": "asc"
}
}]
}
}
Upvotes: 1