PUT test
{
"mappings":{
"folks":{
"properties":{
"works_at": {
"type": "nested"
}
}
}
}
}
PUT /test/folks/1
{
"type": "lawyer",
"works_at": [
{ "location":"New York"},
{ "location":"Boston"}
]
}
PUT /test/folks/2
{
"type": "lawyer",
"works_at": [
{"location":"Chicago"},
{"location":"Boston"}
]
}
PUT /test/folks/3
{
"type": "writer",
"works_at": [
{"location":"San Francisco"},
{"location":"Boston"}
]
}
此搜索带来零结果:
GET /test/folks/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"works_at.location": "Boston"
}
},
{
"match": {
"type": "lawyer"
}
}
]
}
}
}
如果将位置部分从搜索中删除:
GET /test/folks/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"type": "lawyer"
}
}
]
}
}
}
它将带来正确的结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "test",
"_type": "folks",
"_id": "2",
"_score": 0.2876821,
"_source": {
"type": "lawyer",
"works_at": [
{
"location": "Chicago"
},
{
"location": "Boston"
}
]
}
},
{
"_index": "test",
"_type": "folks",
"_id": "1",
"_score": 0.2876821,
"_source": {
"type": "lawyer",
"works_at": [
{
"location": "New York"
},
{
"location": "Boston"
}
]
}
}
]
}
}
我的多搜索查询中缺少什么?
答案 0 :(得分:1)
要搜索嵌套对象,您需要使用nested query。
由于location
是works_at
嵌套对象的属性,因此您必须按以下方式修改查询:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "works_at",
"query": {
"match": {
"works_at.location": "Boston"
}
}
}
},
{
"match": {
"type": "lawyer"
}
}
]
}
}
}