In elasticsearch, must match one of array, with optional extra query term

Eric

I am trying to create an elasticsearch query that has to match one of three queries on seperate fields and also have an extra query that is not a requirement to be matched. The problem with doing bool queries and the MUST clause is that it has to match all 3, and with the SHOULD it not always matches the ones that are required, except when using minimum_should_match set to 2. In that case, it doesnt match on documents that match one of the 3 required ones because of the minimum match.

My current query is like this (sorry, dont have the code right here)

query - bool - must - query 1 - query 2 - query 3 - should - query 4

I have also tried the following, but it doesnt make the 4th query optional

query - bool - should - query 1 - query 2 - query 3 - query 4 - minimum_should_match: 2

How would one go about and create a query that MUST match one of three queries and optionally rank those with the 4th query higher.

IanGabes

Nesting bool queries should get you the results you are looking for. We need to match either query one, two or three, while optionally matching query 4.

"query": {
    "bool": {
        "must": [
           {
               "bool": {
                   "should": [
                      {
                          //query1
                      },
                      {
                          //query2
                      },
                      {
                          //query3
                      }
                   ],
                   "minimum_number_should_match": 1
               }
           }
        ],
        "should": [
           {
               //query4
           }
        ]
    }
}

The query must match at least one of the should clauses of queries one through 3, and optionally should match query 4.

Documents that match query 1,2,3 and 4 all at once will rank the highest, as I assumed you did not mean exclusive or.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Elasticsearch term query with must and should

Using bool must match and match _all in one elasticsearch query

elasticsearch match vs term query

Elasticsearch query match + term boolean

Elasticsearch works for one term in must, but not two

Diference between term and match in Elasticsearch in a bool query

What is the difference between a term query and a match one?

Term, nested documents and must_not query incompatible in ElasticSearch?

Elasticsearch query match one word

must match query not working as expected in Elasticsearch

Elasticsearch query must not match text from field

Elasticsearch top level query.term and query.bool.must.term: equivalent?

Elasticsearch term vs match

Elasticsearch: Difference between "Term", "Match Phrase", and "Query String"

Query to partially match every word in a search term in Elasticsearch

What is difference between match and bool must match query in Elasticsearch

elasticsearch: term query fails

Elasticsearch query: range query on two fields, but one is optional field

ElasticSearch: is bool-must-term(3) equivalent to old terms with minimum_should_match 3

ElasticSearch / NEST / No results with "Term" but with "Match"

Elasticsearch must_not query doesn't match regex

Elasticsearch [Query Bool Must Match] performs OR operation instead of AND

Query an Array of Objects to be an Exact Match in ElasticSearch

Elasticsearch: why can't find by `term` query but can find by `match` query?

Elasticsearch term query to number token

Elasticsearch: Filter (or Query) by Term Frequency

Optional match extends query

Match exactly one regex term

Elasticsearch 6.2 / Kibana query: One field must exists and one field must not exists

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive