【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":{}
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值