0

I'm new to Elasticsearch. I have a question about creating conditional search queries between nested and parent types.

I have a mapping below named "homes"

PUT /homes/_mapping
{
    "properties": {
        "name": { "type": "text" },
        "ownerID": { "type": "integer" },
        "status": { "type": "text" }, // active or inactive
        "minDays": { "type": "integer" },
        "dayData": {
            "type": "nested",
            "properties": {
              "date": { "type": "date"  },
              "minDays": { "type": "integer" },
              "closed": { "type": "boolean" }
            }
        }
    }
}

Homeowners can define custom minDays for homes day by day. Of course, there is a default value for minDays in the parent.

I want to make a search query by date range

For example, users can search in homes by start and end date. If there is dayData defined with minDays on the start date, it will take the value here, if not, the parent value will be taken for filtering.

How can I achieve that?

Update

I have documents like below

For example if user is searching avaibility for the dates between "2021-01-01" and 2021-01-04. Total day count is "3" for requested dates. We need search for homes which has minDays value greater or equeal than "3".

In this situation:

  1. I need to get docs with status is "active"
  2. The min day value for _doc/1 is "2", cause there is a custom dayData value with minDays. So this property won't be available.
  3. The min day value for _doc/2 is "4". We get default value from parent, cause there is no custom dayData value. This property will be available.
  4. If they dayData includes a "closed" value with "true" between searched dates, home won't be available.

I have tried several search queries to achieve that but failed for this context.

Thank you in advance.

PUT homes/_doc/1
{
  "name" : "Home Test",
  "status" : "active",
  "ownerID": 1,
  "minDays" : "3",
  "dayData" : [
    {
      "date" : "2021-01-01",
      "minDays" : 2
    },
    {
      "date" : "2021-01-04",
      "minDays" : 5
    },
    {
      "date" : "2021-01-05",
      "closed": true
    }
  ]
}

PUT homes/_doc/2
{
  "name" : "Home Test 2",
  "status" : "active",
  "ownerID": 2,
  "minDays" : "4",
  "dayData" : [
    {
      "date" : "2021-05-02",
      "minDays" : 2
    },
    {
      "date" : "2021-05-03",
      "minDays" : 2
    },
    {
      "date" : "2021-05-03",
      "minDays" : 2,
      "closed" :true
    },
    {
      "date" : "2021-05-10",
      "minDays" : 5
    }
  ]
}
Vedat
  • 1
  • 1
  • 1
    Can you also show some sample documents? – Val Jan 19 '21 at 04:31
  • @val I have updated my question. Thank you for your help, in advance. – Vedat Jan 19 '21 at 20:35
  • I just tried scripting for query but i couldn't access nested values from script... @Val – Vedat Jan 20 '21 at 00:13
  • Now i have this problem —> https://stackoverflow.com/questions/54022283/elasticsearch-search-query-why-params-source-nested-field-size-is-not-workin – Vedat Jan 20 '21 at 01:15
  • @Vedat I've answered that linked question. Let me know whether you still need help w/ this one. – Joe - GMapsBook.com Jan 20 '21 at 12:59
  • @JoeSorocin I guess with that there will be some performance problems. What do you think about perfomance ? – Vedat Jan 20 '21 at 19:38
  • I think scripting is meant to be the last resort. Try to prepare your data such that scripting won't be necessary in the first place... But generally speaking, let's not overthink and prematurely optimize. I don't really think about performance before I've encountered actual bottlenecks... – Joe - GMapsBook.com Jan 20 '21 at 20:48
  • You’re right. On the other side , There will be over 10k doc. And every doc will include dayData. Do you have another idea over my structure? @JoeSorocin – Vedat Jan 20 '21 at 22:01

0 Answers0