PHP uses ElasticSearch advanced query, filtering, sorting

1. Advanced query

1. Boolean query (bool)

bool combines various other queries by means of must (and), must_not (not), should (or).

GET /yoshop/_search
{
    "query":{
        "bool":{
          "must":     { "match": { "title": "大米" }},
          "must_not": { "match": { "title":  "电视" }},
          "should":   { "match": { "title": "手机" }}
        }
    }
}

2. Range query (range)

The range query finds numbers or times that fall within a specified range.

GET /heima/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  1000.0,
                "lt":   2800.00
            }
      }
    }
}

3. Fuzzy query (fuzzy)

A fuzzy query is the fuzzy equivalent of a term query. It allows the user's search terms to deviate from the spelling of the actual terms, but the edit distance of the deviation must not exceed 2.

GET /youshop/_search
{
    "query": {
      "fuzzy": {
        "title": "applo"
      }
    }
}

The above query can also query Apple mobile phones.

We can specify the allowable edit distance through fuzziness.

GET /youshop/_search
{
  "query": {
    "fuzzy": {
        "title": {
            "value":"appla",
            "fuzziness":1
        }
    }
  }
}

2. Filter

The filter method is used to filter the query results and will not affect the scoring.

GET /youshop/_search
{
    "query":{
        "bool":{
          "must":{ "match": { "title": "小米手机" }},
          "filter":{
                "range":{"price":{"gt":3000,"lt":3800.00}}
          }
        }
    }
}

Note: Bool combination condition filtering can also be performed again in the filter.

3. Sorting

1. Single field sorting

sort allows us to sort by different fields, and specify the sorting method through order.

GET /youshop/_search
{
  "query": {
    "match": {
      "title": "小米手机"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

2. Multi-field sorting

Suppose we want to query using price and _id in combination, and the matching results are sorted first by price and then by id.

GET /goods/_search
{
    "query":{
        "bool":{
          "must":{ "match": { "title": "小米手机" }},
          "filter":{
                "range":{"price":{"gt":2000,"lt":3000}}
          }
        }
    },
    "sort": [
      { "price": { "order": "desc" }},
      { "_id": { "order": "desc" }}
    ]
}

918eb2075a67416087e73734d343c70b.jpeg

Guess you like

Origin blog.csdn.net/lxw1844912514/article/details/132074272