2

I have field in elasticsearch doc. Which has date field and two string field.I want to apply range on date and match filter on two string fields.How to apply this?

Let's say I have this doc

movies
{
  name    :"titanic",
  director:"james Cameron",
  releaseDate:"06-07-1997"
}

2 Answers2

1

Try something like this

query: {
          bool: {
            filter: [
              {
                multi_match: {
                  query: query,
                  fields: ['description^30','material^10']
                }
              },
              {
                range: {
                  purchased: { gte: 1000}
                }
              }
            ]
          }
        },
        field_value_factor: {
          field: "purchased"
        }
      }
Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
1

How about a boolean query with filters? edit, since multi match was requested

PUT /movies
{
  "mappings": {
    "properties": {
      "director": {
        "type": "text"
      },
      "name": {
        "type": "text"
      },
      "releaseDate": {
        "type": "date",
        "format": "dd-MM-yyyy"
      }
    }
  }
}

PUT movies/_doc/1
{
    "name": "titanic",
    "director": "james Cameron",
    "releaseDate": "06-07-1997"
}

GET /movies/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "releaseDate": {
              "gte": "05-07-1990",
              "lte": "05-07-2000"
            }
          }
        },
        {
          "multi_match": {
            "query": "james",
            "fields": ["director", "name"]
          }
        }
      ]
    }
  }
}

edit: As per the comment, you could also do a terms query instead of multi-match:

GET /movies/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "releaseDate": {
              "gte": "05-07-1990",
              "lte": "05-07-2000"
            }
          }
        },
        {
          "term": {
            "name": "titanic"
          }
        },
        {
          "term": {
            "director": "james cameron"
          }
        }
      ]
    }
  }
}
Stefano Branco
  • 658
  • 3
  • 14
  • I want docs which have name: "titanic" and director:"james cameron". both at same time – Manoharsinh Rana Jul 05 '19 at 14:04
  • Well if you want a multi-match quere, the above example should work, shouldn't it? Just search with `"query": "james cameron titanic"`. Otherwise, a multi-match query might not be the right choice here. See my edited answer for an approach with a terms query instead – Stefano Branco Jul 08 '19 at 09:53