A
A
Artem Mikhnevich2020-06-09 17:48:44
elasticsearch
Artem Mikhnevich, 2020-06-09 17:48:44

How to compose a relevant search in Elastic Search?

Hello everyone, I'm trying to build a relevant search on the site. and partially implemented.
This request has already been

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [{
            "terms": {
              "visibility": [3, 4]
            }
          }, {
            "terms": {
              "status": [0, 1]
            }
          }, {
            "terms": {
              "sku": ["5111-0243", "9901-0131", "5111-0202", "5111-0301", "5115-0071"]
            }
          }]
        }
      },
      "must": {
        "query_string": {
          "fields": ["name"],
          "query": "*Ка*"
        }
      }
    }
  }
}

And I wrote this block separately and it seems that it gives the result I need only without filters.
{
  "query": {
    "simple_query_string": {
      "query": "Кар*",
      "fields": [
        "name^2",
        "shot_name"
      ],
      "default_operator": "and",
      "analyze_wildcard": true
    }
  }
}

But when I put them together, Elastic returns nothing, and wherever I move my block, it swears or an empty response. So, who is familiar with this topic, tell me in what order should these logical blogs be used? Because bool -> filter -> bool -> must is a little confusing, I took it out of their limits, in Bool and in filter it gives an error.
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "visibility": [
                  3,
                  4
                ]
              }
            },
            {
              "terms": {
                "status": [
                  0,
                  1
                ]
              }
            },
            {
              "terms": {
                "sku": [
                  "5111-0243",
                  "9901-0131"
                ]
              }
            }
          ]
        }
      },
      "simple_query_string": {
        "query": "Кар*",
        "fields": [
          "name^2",
          "shot_name"
        ],
        "default_operator": "and",
        "analyze_wildcard": true
      }
    }
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question