Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple exists fields with OR operation in elasticsearch

I have the following query:

{
  "query": {
    "exists": {
      "field": "field1"
    }
  }
}

I would like my query to be modified in a way that I could add another filter with existing field having 'OR' operation between them.

Any idea how to do so?

Thanks.

like image 236
rayman Avatar asked Aug 23 '17 12:08

rayman


People also ask

What is .RAW in Elasticsearch?

raw field is a keyword version of the city field. The city field can be used for full text search. You can add multi-fields to an existing field using the update mapping API.

What is bool query in Elasticsearch?

The bool query is a go-to query because it allows you to construct an advanced query by chaining together several simple ones. The results must match the queries in this clause. If you have multiple queries, every single one must match. Acts as an and operator.

How do I create a particular field in Elasticsearch?

To retrieve specific fields in the search response, use the fields parameter. Because it consults the index mappings, the fields parameter provides several advantages over referencing the _source directly. Specifically, the fields parameter: Returns each value in a standardized way that matches its mapping type.

What is difference between filter and must in Elasticsearch?

The clause (query) must appear in matching documents and will contribute to the score. The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.


1 Answers

You can use Bool Query with should.

{
    "query": {
        "bool": {
            "should": [{
                "exists": {
                    "field": "field1"
                }
            },
            {
                "exists": {
                    "field": "field2"
                }
            }
            ]
        }
    }
}
like image 143
alexf Avatar answered Oct 23 '22 14:10

alexf