# Search for availability of resources by dates

**URL:** <https://discuss.elastic.co/t/search-for-availability-of-resources-by-dates/32731>\
**Category:** Elasticsearch\
**Created:** [October 21, 2015, 11:29pm UTC](https://discuss.elastic.co/t/search-for-availability-of-resources-by-dates/32731 "2015-10-21T23:29:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RickP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rickp/32/5399_2.png) [@RickP](https://discuss.elastic.co/u/RickP)\
**Post date:** [October 21, 2015, 11:29pm UTC](https://discuss.elastic.co/t/search-for-availability-of-resources-by-dates/32731/1 "2015-10-21T23:29:43Z")

</div>

Hi,

I have a database with resources that have 2 different types of availability on certain days.

The data looks like this (only the relevant part):

{name: "resource\_1", available\_on: ["2015-12-12", "2015-12-13", "2015-12-15"], probably\_available\_on: ["2015-12-18", "2015-12-20", "2015-12-24"]}

I want to search for resources that are available on at least X% of days in a certain date range. The search should return hits ordered by most 'available\_on' and 'probably\_available\_on' (the first one with a higher boost).

I managed to do that just with the 'available\_on' days (Example for the range 2015-12-11 to 2015-12-15):

{ "query": { "bool": { "should": [{ "term": { "available\_on": "2015-12-11" } }, { "term": { "available\_on": "2015-12-12" } }, { "term": { "available\_on": "2015-12-13" } }, { "term": { "available\_on": "2015-12-14" } }, { "term": { "available\_on": "2015-12-15" } }], "minimum\_should\_match": "70%" } } }

- Is this the correct way to do it or can that be archived with something like a range filter?
- How do I add the 'probably\_available\_on' field with a lower boost?
- Is it possible to retrieve the actual number of matching 'available\_on' and 'probably\_available\_on' days for every hit with the search result?

Have a nice day,  
Rick

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 22, 2015, 10:29am UTC](https://discuss.elastic.co/t/search-for-availability-of-resources-by-dates/32731/2 "2015-10-22T10:29:03Z")

</div>

> [@RickP](#):
>
> Is this the correct way to do it

Doesn't look like a bad way.

Here's the answer to your other questions on boosting etc:

```
POST test/doc
{
	"name":"mark",
	"availability":["monday","tuesday"],
	"possible_availability":["friday"]

}
GET test/doc/_search
{
   "query": {
	  "bool": {
		 "should": [
			{
			   "bool": {
				  "should": [
					 {
						"term": {
						   "possible_availability": "monday",
						   "_name": "monday"
						}
					 },
					 {
						"term": {
						   "availability": "monday",
						   "_name": "monday",
						   "boost":2
						}
					 }
				  ]
			   }
			},
			{
			   "bool": {
				  "should": [
					 {
						"term": {
						   "possible_availability": "tuesday",
						   "_name": "tuesday"
						}
					 },
					 {
						"term": {
						   "availability": "tuesday",
						   "_name": "tuesday",
						   "boost":2
						}
					 }
				  ]
			   }
			},            
			{
			   "bool": {
				  "should": [
					 {
						"term": {
						   "possible_availability": "wednesday",
						   "_name": "wednesday"
						}
					 },
					 {
						"term": {
						   "availability": "wednesday",
						   "_name": "wednesday",
						   "boost":2
						}
					 }
				  ]
			   }
			}                        
		 ],
		 "minimum_number_should_match": 2
	  }
   }
}

```

The usual default ranking heuristics of IDF (how rare a word is) are still in effect here so it would also make sense to wrap each term query in a constant\_score query [1].

> [@RickP](#):
>
> Is it possible to retrieve the actual number of matching 'available\_on' and 'probably\_available\_on'

The use of "\_name" in the query provides metadata that is echoed back in each hit to show what matched or not.  
For fancier ranking you may want to look at the function\_score [2]

Cheers  
Mark

[1] [Constant score query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html)  
[2] [Function score query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_using_function_score)

---

<div class="post-metadata">

**Author:** ![RickP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rickp/32/5399_2.png) [@RickP](https://discuss.elastic.co/u/RickP)\
**Post date:** [October 22, 2015, 11:23am UTC](https://discuss.elastic.co/t/search-for-availability-of-resources-by-dates/32731/3 "2015-10-22T11:23:38Z")

</div>

Thanks a lot! That's exactly what I need.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:43pm UTC](https://discuss.elastic.co/t/search-for-availability-of-resources-by-dates/32731/4 "2017-07-05T23:43:19Z")

</div>

Closed on July 5, 2017, 11:43pm UTC


