常规操作elasticSearch -分词检索

本文详细介绍了如何使用Elasticsearch进行常规的分词检索、排序、分页、多字段匹配及短语匹配等操作,包括通过REST API和Kibana进行操作。此外,还展示了如何进行布尔组合查询、范围查询以及精确值和文本匹配的区别。最后,通过实例演示了无效检索和精确文本检索的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常规操作elasticSearch -分词检索

借助REST API
GET bank/_search?q=*&sort=account_number:asc

检索条件全部 按照account_number 升序

借助Kibana
GET bank/_search
{ 
  "query": 
    { "match_all": {}},
  "sort": [
    { "account_number": { "order": "desc"}}
  ]
}

匹配全部 按照account_number 倒序

指定字段分页排序查找
GET bank/_search
{
  "query":{
    "match_all": {}
  },
  "sort": [
    {
      "balance": {
        "order": "desc"
      }
    }
  ],
  "from":0,
  "size":2,
  "_source": ["balance","firstname"]
}

匹配全部

balance字段倒序

from :0 ,size:2 相当于mysql 的 limit 0,2

“_source”: [“balance”,“firstname”] 相当于 select balance,firstname from …

结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "248",
        "_score" : null,
        "_source" : {
          "firstname" : "West",
          "balance" : 49989
        },
        "sort" : [
          49989
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "854",
        "_score" : null,
        "_source" : {
          "firstname" : "Jimenez",
          "balance" : 49795
        },
        "sort" : [
          49795
        ]
      }
    ]
  }
}

分词匹配查找 match
GET bank/_search
{
  "query": {
    "match": {
      "address": "Mill lane"
    }
  }
}

分词: Mill lane -> Mill , lane

查找 address包含 Mill 或者lane的检索。匹配度越高,得分越高,排序越靠前

短语匹配查找 match_phrase
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "Mill lane"
    }
  }
}

查找 address包含 Mill lane的数据

多列字段分词查找multi_match
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill Movico", 
      "fields": ["address","city"]
    }
  }
}

分词:“mill Movico” mill ,Movico

查找字段属性address ,city对分词的查找。匹配度越高 得分越高越靠前

综合查找
GET bank/_search
{
  "query": {
    "bool":{
      "must": [
        {
          "match": {
            "gender": "m"
          }
        },
        {
          "match": {
            "address": "mill"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "age": "18"
          }
        }
      ],
      "should": [
        {
          "match": {
            "lastname": "Wallace"
          }
        }
      ]
    }
  }
}

must必须成立 (提供得分)

must_not必须不成立(无得分)

should 应当不影响查询结果,但影响得分,从而影响排序

匹配属性gender必须为m,age必须不是18,lastname为Wallace更好(非Wallace的也能查到,但是Wallace匹配的会提升得分,会靠前)

节段查询
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 18,
              "lte": 30
            }
          }
        }
        
      ]
    }
  }
}

检索 18<=age<=30 的数据

附:

gt: greater than 大于

gte: greater than or equal 大于等于

lt: less than 小于

lte: less than or equal 小于等于

区分精确检索和文本匹配
GET bank/_search
{
  "query": {
    "match": {
      "age": 28
    }
  }
}

GET bank/_search
{
  "query": {
    "term": {
      "age": 28
    }
  }
}

age为精确数字,结果集一样。
但是,match通常用于文本匹配检索。
     term通常用于精确数字匹配。

无效检索:
GET bank/_search
{
  "query": {
    "term": {
      "address": "789 Madison Street"
    }
  }
}
如果非要用term匹配文本,查不到结果
精确文本检索
#精确匹配
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "789 Madison "
    }
  }
}
这个检索,即使: "address" : "789 Madison Street" 也查不到它
只要你的存储内容与address.keyword完全匹配才可以!
对比精确文本检索的短语检测
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "789 Madison "
    }
  }
}
这个检索,可以查到: "address" : "789 Madison Street"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

溜达的大象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值