R
R
Ruslan2018-01-18 16:51:38
Yii
Ruslan, 2018-01-18 16:51:38

How to search in elasticsearch with filtering by attributes?

There is an index, it contains a collection with the following mapping:

{
  "sitcenter": {
    "mappings": {
      "periodical": {
        "properties": {
          "access_index": {
            "type": "integer"
          },
          "category_id": {
            "type": "integer"
          },
          "keywords": {
            "type": "text"
          },
          "keywords_kk": {
            "type": "text"
          },
          "publish_date": {
            "type": "date"
          }
        }
      }
    }
  }
}

I'm trying to search for the word bus* while it is necessary that access_index be equal to or less than 10 The
request was made like this:
{
  "size": 50,
  "query": {
    "bool": {
      "should": {
        "wildcard": {
          "keywords": {
            "value": "автобус*",
            "boost": 2
          }
        }
      },
      "filter": {
        "range": {
          "access_index": {
            "lte": 10
          }
        }
      }
    }
  }
}

Finds everything that matches the filter access_index <= 10, and does not take into account the wildcard, if I remove the filter, then everything is searched correctly. What am I doing wrong? Tell.
And another question, how to add a condition to the filter that the category_id can be 30, 38, 49 (as in SQL IN)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan, 2018-01-19
@Tiasar

This is how it works on elasticsearch version 6.0.0

{
  "query": { 
    "bool": { 
      "must": {
        "wildcard": {
          "keywords": {
            "value": "автобус*",
            "boost":2
          }
        }
      },
      "filter": [ 
        { "terms":  { "category_id": [33, 35] }}, 
        { "range": { "access_index": { "lte": 30 }}} 
      ]
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question