【Elasticsearch實踐】(六)ES搜索

一、複雜查詢

1.1、複雜條件搜索

# 複雜條件搜索
GET tindex/_doc/_search
{
  "query":{
    "match":{
      "name": "wells"
    }
  }
}

在這裏插入圖片描述

1.2、指定輸出字段

# 指定輸出字段
GET /tindex/_doc/_search
{
  "query": {
    "match":{
      "name": "wells"
    }
  },
  "_source": ["name", "age"]
}

在這裏插入圖片描述

1.3、排序

# 排序
GET /tindex/_doc/_search
{
  "query": {
    "match": {
      "name": "wells"
    }
  },
  "_source": ["name", "age"],
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

在這裏插入圖片描述

1.4、分頁查詢

# 分頁查詢
GET /tindex/_doc/_search
{
  "query": {
    "match": {
      "name": "wells"
    }
  },
  "from": 0,
  "size": 1
}

在這裏插入圖片描述

1.5、布爾值查詢

1.5.1、must (and)

GET /tindex/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "wells"
          }
        },
        {
          "match": {
            "age": 17
          }
        }
      ]
    }
  }
}

在這裏插入圖片描述

1.5.2、should (or)

# bool: should 查詢 where name=wells or age = 17
GET /tindex/_doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "wells"
          }
        },
        {
          "match": {
            "age": 17
          }
        }
      ]
    }
  }
}

在這裏插入圖片描述

1.5.3、must_not (not)

# bool: must_not 查詢 where name != wells
GET /tindex/_doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "wells"
          }
        },
        {
          "match": {
            "name": "tom"
          }
        }
      ]
    }
  }
}
# 或者 bool: must_not 查詢 where name != wells
GET /tindex/_doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
          	# 通過空格隔開,設置多個值
            "name": "wells tom"
          }
        }
      ]
    }
  }
}

在這裏插入圖片描述

1.5.4、過濾器filter

# filter
GET /tindex/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "wells"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "lt": 30,
            "gt": 18
          }
        }
      }
    }
  }
}

在這裏插入圖片描述

1.6、匹配多個值

# 匹配多個值
GET /tindex/_doc/_search
{
  "query": {
    "match": {
      "tags": "man 技術"
    }
  }
}

在這裏插入圖片描述

二、精確查詢

兩個類型:

  • text:會被分詞
  • keyword:不會被分詞

通過 _analyze 對詞進行分析查看,text與keyword都是fields的類型,通過類型可以區分是否精確查詢

# text 與 keyword
GET _analyze
{
  "analyzer": "standard",
  "text": "wells學es"
}

# text 與 keyword
GET _analyze
{
  "analyzer": "keyword",
  "text": "wells學es"
}

在這裏插入圖片描述
在這裏插入圖片描述
關於分詞:

  • term:查詢是直接通過倒排索引指定的詞條進行精確查找的,不會進行分詞
  • match:使用分詞器進行解析,再進行查詢
# term 與 match
GET /tindex/_doc/_search
{
  "query": {
    "match": {
      "tags": "技術宅男"
    }
  }
}

# term 與 match
GET /tindex/_doc/_search
{
  "query": {
    "term": {
      "tags": "技術宅男"
    }
  }
}

2.1、精確匹配多個值

2.2、高亮

2.2.1、默認高亮

高亮

GET /tindex/_doc/_search
{
  "query": {
    "match": {
      "tags": "技術"
    }
  },
  "highlight":{
    "fields":{
      "name":{}
    }
  }
}

2.2.2、自定義高亮

GET /tindex/_doc/_search
{
  "query": {
    "match": {
      "tags": "技術"
    }
  },
  "highlight":{
    "pre_tags": "<p color='red'>",
    "post_tags": "</p>",
    "fields":{
      "name":{}
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章