No matches when using terms on object property

Hi there! I’m currently working on a project that requires me to search for emails on a specific field within the document. I’m trying the following query:

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "attendant.email": ["attendant@email.com"]
                    }
                }
            ]
        }
    },
    "size": 10
}

The documents within the index are like this one (I had to anonymize its data due to local laws):

{
    "_index": "opportunity",
    "_id": "3575511",
    "_score": 4.4735565,
    "_source": {
        "id": 3575511,
        "campaign_id": 260,
        "campaign_name": "Name",
        "campaign_message": "Campaign message",
        "campaign_end_date": "0001-01-01T00:00:00Z",
        "event_campaign": {
            "conversion": false,
            "type": false
        },
        "stage_id": 4,
        "stage": {
            "id": 4,
            "name": "Stage Name",
            "description": "Stage Description",
            "slug": "stage-slug"
        },
        "status_id": 6,
        "status": {
            "id": 6,
            "name": "Status Name",
            "description": "Status Description",
            "slug": "status-slug",
            "status_type": 0
        },
        "origin_id": 0,
        "attendant_id": 543,
        "title": "Opportunity Title",
        "value": 0,
        "subtotal": 0,
        "comment": "",
        "client": {
            "id": 2485260,
            "registered": true,
            "id_fc": 15982726,
            "email": "client@email.com",
            "name": "Client Name",
            "document": "01020300506070809",
            "representative": "",
            "phones": [
                {
                    "id": 5483488,
                    "type": true,
                    "ddi": 55,
                    "number": "000102030405",
                    "created_at": "2023-10-04T23:48:42Z",
                    "updated_at": "2024-09-26T14:23:28Z"
                }
            ],
            "created_at": "2023-10-04T23:48:42Z",
            "updated_at": "2024-09-26T14:23:28Z"
        },
        "created_by": "",
        "created_at": "2024-09-26T14:23:28Z",
        "updated_at": "2024-09-26T14:23:28Z",
        "lifetime": "Since 4 months and 4 days",
        "tasks_total": 0,
        "tasks_open": 0,
        "date_next_task": "",
        "favorite": false,
        "finished": false,
        "reason_loss_id": 0,
        "reason_loss_name": "",
        "sales_opportunity": 0,
        "attendant": {
            "id": 543,
            "email": "attendant@email.com",
            "name": "Attendant Name"
        },
        "order": null,
        "order_db": null,
        "tasks": null,
        "budget": null,
        "products": null,
        "type_change": null,
        "type_opportunity": false,
        "conversion_opportunity": false,
        "bond_exchange": null,
        "coupon": {
            "id": 0,
            "opportunity_id": 0,
            "promo_id": 0,
            "code": "",
            "active": false,
            "updated_at": "0001-01-01T00:00:00Z"
        }
    }
}

The idea is that there might be multiple emails within the termsclause and all documents whose attendant.email matches any of the given emails return from the search. I’ve tried with a quite big list of emails (which in theory should’ve matched nearly all - if not all - documents within the index), but got no results.

Here is the document mapping:

{
  "opportunity": {
    "mappings": {
      "properties": {
        "attendant": {
          "properties": {
            "email": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "attendant_id": {
          "type": "long"
        },
        "budget": {
          "properties": {
            "cart_id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "cbid": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "created_at": {
              "type": "date"
            },
            "id": {
              "type": "long"
            },
            "link_cart": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "opportunity_id": {
              "type": "long"
            },
            "origin_id": {
              "type": "long"
            },
            "recipe_id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "sent_at": {
              "type": "date"
            },
            "updated_at": {
              "type": "date"
            }
          }
        },
        "campaign_end_date": {
          "type": "date"
        },
        "campaign_id": {
          "type": "long"
        },
        "campaign_message": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "campaign_name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "client": {
          "properties": {
            "created_at": {
              "type": "date"
            },
            "document": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "email": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            },
            "id_fc": {
              "type": "long"
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "phones": {
              "properties": {
                "created_at": {
                  "type": "date"
                },
                "ddi": {
                  "type": "long"
                },
                "id": {
                  "type": "long"
                },
                "number": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "type": {
                  "type": "boolean"
                },
                "updated_at": {
                  "type": "date"
                }
              }
            },
            "registered": {
              "type": "boolean"
            },
            "representative": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "updated_at": {
              "type": "date"
            }
          }
        },
        "comment": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "conversion_opportunity": {
          "type": "boolean"
        },
        "coupon": {
          "properties": {
            "active": {
              "type": "boolean"
            },
            "code": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            },
            "opportunity_id": {
              "type": "long"
            },
            "promo_id": {
              "type": "long"
            },
            "updated_at": {
              "type": "date"
            }
          }
        },
        "created_at": {
          "type": "date"
        },
        "created_by": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "date_next_task": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "event_campaign": {
          "properties": {
            "conversion": {
              "type": "boolean"
            },
            "type": {
              "type": "boolean"
            }
          }
        },
        "favorite": {
          "type": "boolean"
        },
        "finished": {
          "type": "boolean"
        },
        "id": {
          "type": "long"
        },
        "lifetime": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "order": {
          "properties": {
            "approval_at": {
              "properties": {
                "Time": {
                  "type": "date"
                },
                "Valid": {
                  "type": "boolean"
                }
              }
            },
            "created_at": {
              "type": "date"
            },
            "creation_at": {
              "type": "date"
            },
            "external_conversion": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "external_id": {
              "type": "long"
            },
            "id": {
              "type": "long"
            },
            "opportunity_id": {
              "type": "long"
            },
            "order_number": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "payment": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "updated_at": {
              "type": "date"
            }
          }
        },
        "origin_id": {
          "type": "long"
        },
        "products": {
          "properties": {
            "brand": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "cabecasa": {
              "type": "boolean"
            },
            "created_at": {
              "type": "date"
            },
            "id": {
              "type": "long"
            },
            "image": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "opportunity_id": {
              "type": "long"
            },
            "quantity": {
              "type": "long"
            },
            "seller": {
              "properties": {
                "id": {
                  "type": "long"
                },
                "name": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                }
              }
            },
            "sku": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "total_price": {
              "type": "float"
            },
            "type": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "unit_price": {
              "type": "float"
            },
            "updated_at": {
              "type": "date"
            }
          }
        },
        "reason_loss_id": {
          "type": "long"
        },
        "reason_loss_name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "sales_opportunity": {
          "type": "long"
        },
        "stage": {
          "properties": {
            "description": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "slug": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "stage_id": {
          "type": "long"
        },
        "status": {
          "properties": {
            "description": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "long"
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "slug": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "status_type": {
              "type": "long"
            }
          }
        },
        "status_id": {
          "type": "long"
        },
        "subtotal": {
          "type": "float"
        },
        "tasks": {
          "properties": {
            "comment": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "completed": {
              "type": "boolean"
            },
            "created_at": {
              "type": "date"
            },
            "day_of_week": {
              "type": "long"
            },
            "end_date": {
              "properties": {
                "Time": {
                  "type": "date"
                },
                "Valid": {
                  "type": "boolean"
                }
              }
            },
            "execution_date": {
              "type": "date"
            },
            "id": {
              "type": "long"
            },
            "id_fc": {
              "type": "long"
            },
            "op_id": {
              "type": "long"
            },
            "op_stage_Id": {
              "type": "long"
            },
            "op_title": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "op_value": {
              "type": "float"
            },
            "phone": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "removal_date": {
              "properties": {
                "Time": {
                  "type": "date"
                },
                "Valid": {
                  "type": "boolean"
                }
              }
            },
            "removed": {
              "type": "boolean"
            },
            "task_period_id": {
              "type": "long"
            },
            "task_type_id": {
              "type": "long"
            },
            "updated_at": {
              "type": "date"
            }
          }
        },
        "tasks_open": {
          "type": "long"
        },
        "tasks_total": {
          "type": "long"
        },
        "title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "type_opportunity": {
          "type": "boolean"
        },
        "updated_at": {
          "type": "date"
        },
        "value": {
          "type": "float"
        }
      }
    }
  }
}

I’m quite new to OpenSearch and I’m having a hard time on this. What I’m doing wrong? Thanks in advance!

No worries Firehawk! I think the answer you’re looking for is using the flat_object mapping type, which will let you use that dotted path notation for matching terms.

The drawback is that they don’t technically get “indexed” in order to avoid mapping explosions, but for searching docs like this it works great.

Good luck!

1 Like

Hey, thanks for the response! Sadly, I don’t think that it’ll do - or at least I’d need a pretty good argument to use a field type that doesn’t get indexed when the whole point to move this particular document from SQL to OpenSearch was to take advantage of OpenSearch’s indexing and speed.

I don’t really think that it will be an issue - the data volume is really low (in the last 6 months it generated an total ~700 documents), and I’m not going to store more than six months worth of data.

Are there other options? Like using a nested object instead of a “normal” object? There are other fields in the document that I’m facing the same issue, so the same solution will be applied to all of them, if possible.

Another thing: I noticed that on the sample document I posted before the products property is null. It is not always the case - in other documents, it is an array of objects, and I have to search fields inside those objects too. A more complete document will look like this:

{
    "_index": "opportunity",
    "_id": "3575511",
    "_score": 4.4735565,
    "_source": {
        "id": 3575511,
        "campaign_id": 260,
        "campaign_name": "Name",
        "campaign_message": "Campaign message",
        "campaign_end_date": "0001-01-01T00:00:00Z",
        "event_campaign": {
            "conversion": false,
            "type": false
        },
        "stage_id": 4,
        "stage": {
            "id": 4,
            "name": "Stage Name",
            "description": "Stage Description",
            "slug": "stage-slug"
        },
        "status_id": 6,
        "status": {
            "id": 6,
            "name": "Status Name",
            "description": "Status Description",
            "slug": "status-slug",
            "status_type": 0
        },
        "origin_id": 0,
        "attendant_id": 543,
        "title": "Opportunity Title",
        "value": 0,
        "subtotal": 0,
        "comment": "",
        "client": {
            "id": 2485260,
            "registered": true,
            "id_fc": 15982726,
            "email": "client@email.com",
            "name": "Client Name",
            "document": "01020300506070809",
            "representative": "",
            "phones": [
                {
                    "id": 5483488,
                    "type": true,
                    "ddi": 55,
                    "number": "000102030405",
                    "created_at": "2023-10-04T23:48:42Z",
                    "updated_at": "2024-09-26T14:23:28Z"
                }
            ],
            "created_at": "2023-10-04T23:48:42Z",
            "updated_at": "2024-09-26T14:23:28Z"
        },
        "created_by": "",
        "created_at": "2024-09-26T14:23:28Z",
        "updated_at": "2024-09-26T14:23:28Z",
        "lifetime": "Since 4 months and 4 days",
        "tasks_total": 0,
        "tasks_open": 0,
        "date_next_task": "",
        "favorite": false,
        "finished": false,
        "reason_loss_id": 0,
        "reason_loss_name": "",
        "sales_opportunity": 0,
        "attendant": {
            "id": 543,
            "email": "attendant@email.com",
            "name": "Attendant Name"
        },
        "order": null,
        "order_db": null,
        "tasks": null,
        "budget": null,
        "products": [
            {
                "id": 8924399,
                "opportunity_id": 3575069,
                "sku": "652991",
                "name": "Product #1",
                "brand": "Brand #1",
                "seller": {
                    "id": 0,
                    "name": "Seller #1"
                },
                "type": "1P",
                "cabecasa": true,
                "image": "img.url",
                "quantity": 1,
                "unit_price": 3841.51,
                "total_price": 3841.51,
                "created_at": "2024-07-26T17:29:25Z",
                "updated_at": "2024-07-26T17:29:25Z"
            },
            {
                "id": 8924400,
                "opportunity_id": 3575069,
                "sku": "695864",
                "name": "Product #2",
                "brand": "Brand #2",
                "seller": {
                    "id": 0,
                    "name": "Seller #1"
                },
                "type": "1P",
                "cabecasa": true,
                "image": "img.url",
                "quantity": 1,
                "unit_price": 288.22,
                "total_price": 288.22,
                "created_at": "2024-07-26T17:29:25Z",
                "updated_at": "2024-07-26T17:29:25Z"
            },
            {
                "id": 8924800,
                "opportunity_id": 3575069,
                "sku": "695864",
                "name": "Product #3",
                "brand": "Brand #1",
                "seller": {
                    "id": 0,
                    "name": "Seller #2"
                },
                "type": "1P",
                "cabecasa": true,
                "image": "img.url",
                "quantity": 1,
                "unit_price": 19.22,
                "total_price": 19.22,
                "created_at": "2024-07-26T17:29:25Z",
                "updated_at": "2024-07-26T17:29:25Z"
            }
        ],
        "type_change": null,
        "type_opportunity": false,
        "conversion_opportunity": false,
        "bond_exchange": null,
        "coupon": {
            "id": 0,
            "opportunity_id": 0,
            "promo_id": 0,
            "code": "",
            "active": false,
            "updated_at": "0001-01-01T00:00:00Z"
        }
    }
}

I’m also going to search the product name, brand and the product’s seller name.

I’m still on development phrase, so I can do anything with indexes when/if needed. I noted that I can’t change products type (for example) from object to nested, so I guess I’ll have to recreate the index to change it.

I do believe the nested object type would get you what you want. Take note that the query syntax and type changes for searching this type - the query changes from a terms query to a nested query, which specifies a path. I’m not super familiar with the ins and outs of this query. Say , if you wanted to specify more than one ‘path’ .

I wouldn’t worry about some of your documents having null where there’s normally an array of objects. OpenSearch will match appropriately.

You’ll definitely have to recreate your index with the appropriate field mapping. I’d love to hear whether this works out for you - come back and let us know!

Well, either I’m not making the query correctly or it isn’t really working the way I expect. I’ve updated the mapping to the following:

{
    "opportunity": {
        "mappings": {
            "properties": {
                "attendant": {
                    "type": "nested",
                    "properties": {
                        "email": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "long"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "attendant_id": {
                    "type": "long"
                },
                "budget": {
                    "type": "nested",
                    "properties": {
                        "cart_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "cbid": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "created_at": {
                            "type": "date"
                        },
                        "id": {
                            "type": "long"
                        },
                        "link_cart": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "opportunity_id": {
                            "type": "long"
                        },
                        "origin_id": {
                            "type": "long"
                        },
                        "recipe_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "sent_at": {
                            "type": "date"
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "campaign_end_date": {
                    "type": "date"
                },
                "campaign_id": {
                    "type": "long"
                },
                "campaign_message": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "campaign_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "client": {
                    "type": "nested",
                    "properties": {
                        "created_at": {
                            "type": "date"
                        },
                        "document": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "email": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "long"
                        },
                        "id_fc": {
                            "type": "long"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "phones": {
                            "properties": {
                                "created_at": {
                                    "type": "date"
                                },
                                "ddi": {
                                    "type": "long"
                                },
                                "id": {
                                    "type": "long"
                                },
                                "number": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "type": {
                                    "type": "boolean"
                                },
                                "updated_at": {
                                    "type": "date"
                                }
                            }
                        },
                        "registered": {
                            "type": "boolean"
                        },
                        "representative": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "comment": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "conversion_opportunity": {
                    "type": "boolean"
                },
                "coupon": {
                    "type": "nested",
                    "properties": {
                        "active": {
                            "type": "boolean"
                        },
                        "code": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "long"
                        },
                        "opportunity_id": {
                            "type": "long"
                        },
                        "promo_id": {
                            "type": "long"
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "created_at": {
                    "type": "date"
                },
                "created_by": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "date_next_task": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "event_campaign": {
                    "type": "nested",
                    "properties": {
                        "conversion": {
                            "type": "boolean"
                        },
                        "type": {
                            "type": "boolean"
                        }
                    }
                },
                "favorite": {
                    "type": "boolean"
                },
                "finished": {
                    "type": "boolean"
                },
                "id": {
                    "type": "long"
                },
                "lifetime": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "order": {
                    "type": "nested",
                    "properties": {
                        "approval_at": {
                            "properties": {
                                "Time": {
                                    "type": "date"
                                },
                                "Valid": {
                                    "type": "boolean"
                                }
                            }
                        },
                        "created_at": {
                            "type": "date"
                        },
                        "creation_at": {
                            "type": "date"
                        },
                        "external_conversion": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "external_id": {
                            "type": "long"
                        },
                        "id": {
                            "type": "long"
                        },
                        "opportunity_id": {
                            "type": "long"
                        },
                        "order_number": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "payment": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "origin_id": {
                    "type": "long"
                },
                "products": {
                    "type": "nested",
                    "properties": {
                        "brand": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "cabecasa": {
                            "type": "boolean"
                        },
                        "created_at": {
                            "type": "date"
                        },
                        "id": {
                            "type": "long"
                        },
                        "image": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "opportunity_id": {
                            "type": "long"
                        },
                        "quantity": {
                            "type": "long"
                        },
                        "seller": {
                            "properties": {
                                "id": {
                                    "type": "long"
                                },
                                "name": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        },
                        "sku": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "total_price": {
                            "type": "float"
                        },
                        "type": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "unit_price": {
                            "type": "float"
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "reason_loss_id": {
                    "type": "long"
                },
                "reason_loss_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "sales_opportunity": {
                    "type": "long"
                },
                "stage": {
                    "type": "nested",
                    "properties": {
                        "description": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "long"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "slug": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "stage_id": {
                    "type": "long"
                },
                "status": {
                    "type": "nested",
                    "properties": {
                        "description": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "long"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "slug": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "status_type": {
                            "type": "long"
                        }
                    }
                },
                "status_id": {
                    "type": "long"
                },
                "subtotal": {
                    "type": "float"
                },
                "tasks": {
                    "type": "nested",
                    "properties": {
                        "comment": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "completed": {
                            "type": "boolean"
                        },
                        "created_at": {
                            "type": "date"
                        },
                        "day_of_week": {
                            "type": "long"
                        },
                        "end_date": {
                            "properties": {
                                "Time": {
                                    "type": "date"
                                },
                                "Valid": {
                                    "type": "boolean"
                                }
                            }
                        },
                        "execution_date": {
                            "type": "date"
                        },
                        "id": {
                            "type": "long"
                        },
                        "id_fc": {
                            "type": "long"
                        },
                        "op_id": {
                            "type": "long"
                        },
                        "op_stage_Id": {
                            "type": "long"
                        },
                        "op_title": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "op_value": {
                            "type": "float"
                        },
                        "phone": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "removal_date": {
                            "properties": {
                                "Time": {
                                    "type": "date"
                                },
                                "Valid": {
                                    "type": "boolean"
                                }
                            }
                        },
                        "removed": {
                            "type": "boolean"
                        },
                        "task_period_id": {
                            "type": "long"
                        },
                        "task_type_id": {
                            "type": "long"
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "tasks_open": {
                    "type": "long"
                },
                "tasks_total": {
                    "type": "long"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "type_opportunity": {
                    "type": "boolean"
                },
                "updated_at": {
                    "type": "date"
                },
                "value": {
                    "type": "float"
                }
            }
        }
    }
}

I’ve used the reindex API to move data from the original index to a new, temporary and remapped index, deleted the original index, created a new one with the same mapping and used the reindex API again to repeat the process from the temporary index to the newly created one.

Now, I’m using this query:

{
    "query": {
        "nested": {
            "path": "attendant",
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "attendant.email": [
                                    "email.one@domain.com",
                                    "email.two@domain.com",
                                    "email.three@domain.com"
                                ]
                            }
                        }
                    ]
                }
            }
        }
    },
    "size": 10
}

What I expected: documents whose attendant.email was one of the given emails. What I’ve got: nothing! I’ve also tried to use the nested query inside a bool → must, same result. I’m considering changing the source documents so they’ll be flattened before being sent to OpenSearch.

Well, I guess I found a way! I’ve came across this StackOverflow response and decided to try the same:

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "attendant",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "attendant.email.keyword": [
                                                "email.one@domain.com"
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "match": {
                        "campaign_name": "Campaign"
                    }
                }
            ]
        }
    },
    "size": 10
}

And it worked! I’ve added a second clause because I wanted to see how it would work with more restraints, as I’m going to mix them with many other queries.

Hope this helps somebody else!

1 Like

Fantastic. I was going to suggest that you read this little something written by one of our partners “Opster” that talks about using the nested vs object field type - OpenSearch Nested: Nested Fields VS. Object Fields but it looks like you’ve gotten where you need to be!

Thanks for bringing your solution back here to share!

1 Like