Elasticsearch query operation of Elastic

1. Data preparation

POST /liush/_bulk
{"index":{"_id":1}}
{ "title":"小米手机", "images":"http://image.jd.com/12479122.jpg", "price":1999, "stock": 200, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":2}}
{"title":"超米手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":3}}
{ "title":"小米电视", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "电视", "brand": "小米" } }
{"index":{"_id":4}}
{ "title":"小米笔记本", "images":"http://image.jd.com/12479122.jpg", "price":4999, "stock": 200, "attr": { "category": "笔记本", "brand": "小米" } }
{"index":{"_id":5}}
{ "title":"华为手机", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":6}}
{ "title":"华为笔记本", "images":"http://image.jd.com/12479122.jpg", "price":5999, "stock": 200, "attr": { "category": "笔记本", "brand": "华为" } }
{"index":{"_id":7}}
{ "title":"荣耀手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":8}}
{ "title":"oppo手机", "images":"http://image.jd.com/12479122.jpg", "price":2799, "stock": 400, "attr": { "category": "手机", "brand": "oppo" } }
{"index":{"_id":9}}
{ "title":"vivo手机", "images":"http://image.jd.com/12479122.jpg", "price":2699, "stock": 300, "attr": { "category": "手机", "brand": "vivo" } }
{"index":{"_id":10}}
{ "title":"华为nova手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }

2. Match query (match)

GET /liush/_search
{
  "query": {
    "match": {
      "title": "小米手机"
    }
  }
}

A lot of data is queried, not only including `Xiaomi mobile phone`, but also related to `Xiaomi` or `mobile phone`, indicating that there is an `or` relationship between multiple words.

In some cases, we need more precise search, we want this relationship to become `and`, we can do this:

GET /liush/_search
{
  "query": {
    "match": {
      "title": {
        "query": "小米手机",
        "operator": "and"
      }
    }
  }
}

3. Term query (term)

`term` queries are used to match exact values, which may be numbers, times, booleans, or strings that are not tokenized.

GET /liush/_search
{
    "query":{
        "term":{
            "price": 4999
        }
    }
}

4. Range query (range)

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

`range` queries allow the following characters: 

operator illustrate
gt more than the
gte greater or equal to
lt less than
lte less than or equal to
GET /liush/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  1000,
                "lt":   3000
            }
    	}
    }
}

5. Boolean combination (bool)

Boolean query is also called compound query

`bool` combines various other queries with `must` (and), `must_not` (not), `should` (or)

GET /liush/_search
{
    "query":{
        "bool":{
        	"must": [
        	  {
        	    "range": {
        	      "price": {
        	        "gte": 1000,
        	        "lte": 3000
        	      }
        	    }
        	  },
        	  {
        	    "range": {
        	      "price": {
        	        "gte": 2000,
        	        "lte": 4000
        	      }
        	    }
        	  }
        	]
        }
    }
}

Note: Only one combination can appear in a combination query, and cannot be mixed 

6. Filter

All queries affect the scoring and ranking of documents. If we need to filter the query results and don't want the filter conditions to affect the scoring, then don't use the filter conditions as query conditions. Instead use the `filter` method

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

 Note: In `filter`, `bool` combined condition filtering can also be performed again.

7. Sorting (sort)

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

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

8. Pagination (from/size)

from: which one to start from

size: how many pieces to take

GET /liush/_search
{
  "query": {
    "match": {
      "title": "小米手机"
    }
  },
  "from": 2,
  "size": 2
}

9. Highlight

Discovery: The essence of highlighting is to add <em> tags to keywords, and then add styles to the tags at the front end.

  • fields: highlight field
  • pre_tags: pre-label
  • post_tags: post tags
GET /liush/_search
{
  "query": {
    "match": {
      "title": "小米"
    }
  },
  "highlight": {
    "fields": {"title": {}}, 
    "pre_tags": "<em>",
    "post_tags": "</em>"
  }
}

10. Result filtering (_source)

By default, elasticsearch will return all fields stored in `_source` in the document in the search results.

If we only want to get some of the fields, we can add `_source` filtering

GET /liush/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "price": 2699
    }
  }
}

Guess you like

Origin blog.csdn.net/qq_45037155/article/details/129697631