How to query all fields of a document with nested fields in OpenSearch (or Elasticsearch)?

I’m currently using version 2.17.1 and referring to this official documentation. OpenSearch – Query DSL – Joining Queries – Nested query – Multi-level nested queries.

Using this, I’m able to query for results where all terms must match in a single nested item.

But after introducing the nested fields, I’m no longer able to just search in every single field in the whole document with a simple string query like “(John) AND (Jane)”.

Is there a way to query in a list of items where all terms must match in a single item to return the parent where necessary, but still be able to query for the terms all over the document?

——— Update: Long version ———–

Still referring to the official documentation found here: OpenSearch – Query DSL – Joining Queries – Nested query – Multi-level nested queries as this matches one part of my requirements.

Let’s say I’ve a document like this one:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> "patient": {
"name": "John Doe",
"contacts": [
{
"name": "Jane Doe",
"relationship": "mother",
"phone": "5551111"
},
{
"name": "Joe Doe",
"relationship": "father",
"phone": "5552222"
}
]
}
</code>
<code> "patient": { "name": "John Doe", "contacts": [ { "name": "Jane Doe", "relationship": "mother", "phone": "5551111" }, { "name": "Joe Doe", "relationship": "father", "phone": "5552222" } ] } </code>
  "patient": {
    "name": "John Doe",
    "contacts": [
      {
        "name": "Jane Doe",
        "relationship": "mother",
        "phone": "5551111"
      },
      {
        "name": "Joe Doe",
        "relationship": "father",
        "phone": "5552222"
      }
    ]
  }

When I search for ‘jane’ and ‘father’ I don’t expect to find this document, because although both terms are present, they are in different items in the contracts list.
I only expect to find it when the terms match within one item, like when searching for ‘jane’ and ‘mother’.

As far as I understand it, to be able to query for patients where my search terms must match in a single ‘contact’, I’ve to declare them as type ‘nested’ like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> "mappings": {
"properties": {
"patient": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"contacts": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"relationship": {
"type": "text"
},
"phone": {
"type": "keyword"
}
}
}
}
}
}
}
</code>
<code> "mappings": { "properties": { "patient": { "type": "nested", "properties": { "name": { "type": "text" }, "contacts": { "type": "nested", "properties": { "name": { "type": "text" }, "relationship": { "type": "text" }, "phone": { "type": "keyword" } } } } } } } </code>
  "mappings": {
    "properties": {
      "patient": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          },
          "contacts": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text"
              },
              "relationship": {
                "type": "text"
              },
              "phone": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }

Using this mapping and a nested query like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> "query": {
"nested": {
"path": "patient",
"query": {
"nested": {
"path": "patient.contacts",
"query": {
"bool": {
"must": [
{ "match": { "patient.contacts.relationship": "mother" } },
{ "match": { "patient.contacts.name": "Jane" } }
]
}
}
}
}
}
}
</code>
<code> "query": { "nested": { "path": "patient", "query": { "nested": { "path": "patient.contacts", "query": { "bool": { "must": [ { "match": { "patient.contacts.relationship": "mother" } }, { "match": { "patient.contacts.name": "Jane" } } ] } } } } } } </code>
  "query": {
    "nested": {
      "path": "patient",
      "query": {
        "nested": {
          "path": "patient.contacts",
          "query": {
            "bool": {
              "must": [
                { "match": { "patient.contacts.relationship": "mother" } },
                { "match": { "patient.contacts.name": "Jane" } }
              ]
            }
          }
        }
      }
    }
  }

I get exactly that behavior.

But I lose the possibility to just query over the whole document with a string query like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> "query": {
"query_string": {
"query": "(John) AND (5552222)"
}
}
/* 'John' as property of root object of type patient and
'5552222' as property of one of the contact's properties */
</code>
<code> "query": { "query_string": { "query": "(John) AND (5552222)" } } /* 'John' as property of root object of type patient and '5552222' as property of one of the contact's properties */ </code>
   "query": {
     "query_string": {
       "query": "(John) AND (5552222)"
     }
   }
/* 'John' as property of root object of type patient and
   '5552222' as property of one of the contact's properties */

Before I introduced the nested field type, such a query would find the document just fine which is the second part of the requirement because sometimes I know which term belongs to which field and can go for the nested query, but other times I don’t and therefor can’t use them and want to fallback to a search over all fields of the document.

Is this possible? Is maybe the nested type the wrong approach? Is using the nested type correct but I need something other than a string query?

Thank you, kopi

3

To search multiple fields at once, especially with a single query, it’d be better to normalize the data by keeping them on the same level. To do that, you can enrich the data. Here is my suggestion:

  1. Keep one doc per patient and save it as text field.
  2. Have multiple contact per patient as nested field to secure the relation in each sub-fields.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PUT _ingest/pipeline/add_patient_name
{
"processors": [
{
"set": {
"field": "_temp_patient_name",
"value": "{{patient.name}}"
}
},
{
"foreach": {
"field": "patient.contacts",
"processor": {
"set": {
"field": "_ingest._value.patient_name",
"value": "{{_temp_patient_name}}"
}
}
}
},
{
"remove": {
"field": "_temp_patient_name"
}
}
]
}
</code>
<code>PUT _ingest/pipeline/add_patient_name { "processors": [ { "set": { "field": "_temp_patient_name", "value": "{{patient.name}}" } }, { "foreach": { "field": "patient.contacts", "processor": { "set": { "field": "_ingest._value.patient_name", "value": "{{_temp_patient_name}}" } } } }, { "remove": { "field": "_temp_patient_name" } } ] } </code>
PUT _ingest/pipeline/add_patient_name
{
  "processors": [
    {
      "set": {
        "field": "_temp_patient_name",
        "value": "{{patient.name}}"
      }
    },
    {
      "foreach": {
        "field": "patient.contacts",
        "processor": {
          "set": {
            "field": "_ingest._value.patient_name",
            "value": "{{_temp_patient_name}}"
          }
        }
      }
    },
    {
      "remove": {
        "field": "_temp_patient_name"
      }
    }
  ]
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PUT hospital
{
"mappings": {
"properties": {
"patient": {
"properties": {
"name": {
"type": "text"
},
"contacts": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"relationship": {
"type": "text"
},
"phone": {
"type": "keyword"
}
}
}
}
}
}
},
"settings": {
"index.default_pipeline": "add_patient_name"
}
}
</code>
<code>PUT hospital { "mappings": { "properties": { "patient": { "properties": { "name": { "type": "text" }, "contacts": { "type": "nested", "properties": { "name": { "type": "text" }, "relationship": { "type": "text" }, "phone": { "type": "keyword" } } } } } } }, "settings": { "index.default_pipeline": "add_patient_name" } } </code>
PUT hospital
{
  "mappings": {
    "properties": {
      "patient": {
        "properties": {
          "name": {
            "type": "text"
          },
          "contacts": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text"
              },
              "relationship": {
                "type": "text"
              },
              "phone": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  },
  "settings": {
    "index.default_pipeline": "add_patient_name"
  }
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PUT hospital/_doc/1
{
"patient": {
"name": "John Doe",
"contacts": [
{
"name": "Jane Doe",
"relationship": "mother",
"phone": "5551111"
},
{
"name": "Joe Doe",
"relationship": "father",
"phone": "5552222"
}
]
}
}
</code>
<code>PUT hospital/_doc/1 { "patient": { "name": "John Doe", "contacts": [ { "name": "Jane Doe", "relationship": "mother", "phone": "5551111" }, { "name": "Joe Doe", "relationship": "father", "phone": "5552222" } ] } } </code>
PUT hospital/_doc/1
{
  "patient": {
    "name": "John Doe",
    "contacts": [
      {
        "name": "Jane Doe",
        "relationship": "mother",
        "phone": "5551111"
      },
      {
        "name": "Joe Doe",
        "relationship": "father",
        "phone": "5552222"
      }
    ]
  }
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET hospital/_search
{
"query": {
"nested": {
"path": "patient.contacts",
"query": {
"query_string": {
"default_field": "*",
"query": "John AND 5552222"
}
}
}
}
}
</code>
<code>GET hospital/_search { "query": { "nested": { "path": "patient.contacts", "query": { "query_string": { "default_field": "*", "query": "John AND 5552222" } } } } } </code>
GET hospital/_search
{
  "query": {
    "nested": {
      "path": "patient.contacts",
      "query": {
        "query_string": {
          "default_field": "*",
          "query": "John AND 5552222"
        }
      }
    }
  }
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật