Search code examples
elasticsearchkibana

Multiple must conditions in Elasticsearch


I am a beginner in Elasticsearch and I'm trying to use span_near clauses (a document with the searched query in first position must have a higher score than a document with the searched query in second position). I'm using Elasticsearch 7.

I tried this:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "query_string": {
                                    "query": "chaudiere",
                                    "default_operator": "OR",
                                    "analyzer": "french",
                                    "fields": [
                                        "name"
                                    ],
                                    "type": "cross_fields",
                                    "lenient": true,
                                    "fuzziness": "AUTO:10,20"
                                }
                            }
                        ]
                    },
                    "must": {
                        "span_near": {
                            "slop": 2,
                            "in_order": false,
                            "clauses": [
                                {
                                    "span_multi": {
                                        "match": {
                                            "fuzzy": {
                                                "label": {
                                                    "value": "chaudiere",
                                                    "fuzziness": "AUTO"
                                                }
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            ],
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "store_uid": 1
                                }
                            },
                            {
                                "bool": {
                                    "should": [
                                        {
                                            "bool": {
                                                "must_not": [
                                                    {
                                                        "term": {
                                                            "life_cycle": "FDS"
                                                        }
                                                    }
                                                ]
                                            }
                                        },
                                        {
                                            "bool": {
                                                "must": [
                                                    {
                                                        "term": {
                                                            "life_cycle": "FDS"
                                                        }
                                                    },
                                                    {
                                                        "bool": {
                                                            "should": [
                                                                {
                                                                    "bool": {
                                                                        "must": [
                                                                            {
                                                                                "range": {
                                                                                    "stock": {
                                                                                        "gt": 0
                                                                                    }
                                                                                }
                                                                            }
                                                                        ]
                                                                    }
                                                                },
                                                                {
                                                                    "bool": {
                                                                        "must": [
                                                                            {
                                                                                "range": {
                                                                                    "stock_delivery": {
                                                                                        "gt": 0
                                                                                    }
                                                                                }
                                                                            }
                                                                        ]
                                                                    }
                                                                }
                                                            ]
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        ],
                        "must_not": [
                            {
                                "term": {
                                    "is_base": true
                                }
                            },
                            {
                                "term": {
                                    "is_replaced": true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

But I have this message

[bool] malformed query, expected [END_OBJECT]

Do you know why?

Thanks for your help


Solution

  • the error is because you are setting up the must clause incorrectly. you cannot use a bool-query at the same level and must.

    Query with error:

    {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "FIELD": "TEXT"
                    }
                  }
                ]
              },
              "must": {
                "match": {
                  "FIELD": "TEXT"
                }
              }
            }
          ]
        }
      }
    }
    

    I want corrected: The suggestion is to have the query_string and span_near clauses in the bool-query inside the must.

    {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {
                    "query_string": {
                      "query": "chaudiere",
                      "default_operator": "OR",
                      "analyzer": "french",
                      "fields": [
                        "name"
                      ],
                      "type": "cross_fields",
                      "lenient": true,
                      "fuzziness": "AUTO:10,20"
                    }
                  },
                  {
                    "span_near": {
                      "slop": 2,
                      "in_order": false,
                      "clauses": [
                        {
                          "span_multi": {
                            "match": {
                              "fuzzy": {
                                "label": {
                                  "value": "chaudiere",
                                  "fuzziness": "AUTO"
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ],
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "store_uid": 1
                    }
                  },
                  {
                    "bool": {
                      "should": [
                        {
                          "bool": {
                            "must_not": [
                              {
                                "term": {
                                  "life_cycle": "FDS"
                                }
                              }
                            ]
                          }
                        },
                        {
                          "bool": {
                            "must": [
                              {
                                "term": {
                                  "life_cycle": "FDS"
                                }
                              },
                              {
                                "bool": {
                                  "should": [
                                    {
                                      "bool": {
                                        "must": [
                                          {
                                            "range": {
                                              "stock": {
                                                "gt": 0
                                              }
                                            }
                                          }
                                        ]
                                      }
                                    },
                                    {
                                      "bool": {
                                        "must": [
                                          {
                                            "range": {
                                              "stock_delivery": {
                                                "gt": 0
                                              }
                                            }
                                          }
                                        ]
                                      }
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                ],
                "must_not": [
                  {
                    "term": {
                      "is_base": true
                    }
                  },
                  {
                    "term": {
                      "is_replaced": true
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }