elasticSearch核心概念的介绍(四):搜索的简单使用

搜索的简单使用

在上一章介绍了文档的基本使用,有兴趣的可以参考一下
elasticSearch核心概念的介绍(三):文档的增删改查
在这一章我们会进行搜索的简单使用。

一、我们先将上述内容中的索引都删除

  • 查看所有索引
curl -X GET "http://172.25.45.150:9200/_cat/indices?v"
  • 挨个进行删除
curl -X DELETE "http://172.25.45.150:9200/nba"

二、新建一个索引,并且指定mapping

curl -X PUT "http://172.25.45.150:9200/nba" -H 'Content-Type:application/json' -d '
	{
		"mappings":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "team_name":{
                    "type":"text"
                },
                "position":{
                    "type":"keyword"
                },
                "play_year":{
                    "type":"keyword"
                },
                "jerse_no":{
                    "type":"keyword"
                }
            }
		}
	}
'

三、新增document (数据)

  • doc1
curl -X PUT "http://172.25.45.150:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
    "name":"哈登",
    "team_name":"火箭",
    "position":"得分后卫",
    "play_year":"10",
    "jerse_no":"13"
}
'
  • doc2
curl -X PUT "http://172.25.45.150:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
    "name":"库里",
    "team_name":"勇士",
    "position":"控球后卫",
    "play_year":"10",
    "jerse_no":"30"
}
'
  • doc3
curl -X PUT "http://172.25.45.150:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
    "name":"詹姆斯",
    "team_name":"湖人",
    "position":"小前锋",
    "play_year":"15",
    "jerse_no":"23"
}
'

四、查询

  • term(词条)查询和full text(全文)查询

    • 词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索。
    • 全文查询:ElasticSearch引擎会优先分析查询字符串,将其拆分称多个分词,只要已分析的字段中包含词条的任意一个,或全部包含,就匹配查询条件,返回该文档;如果不包含任意一个分词,表示没有任何文档匹配查询条件
  • 单条term查询

    • 请求
    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    	{
    		"query":{
    			"term":{
    				"jerse_no":"23"
    			}
    		}
    	}
    '
    
  • 多条term查询

    • 请求

      curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
      	{
      		"query":{
      			"terms":{
      				"jerse_no":[
                          "23","13"
                      ]
      			}
      		}
      	}
      '
      
    • 响应

{
    "took": 1, //消耗的时间
    "timed_out": false, //是否超时
    "_shards": { //分片信息
        "total": 1, //这里是单台机器,这里是1
        "successful": 1, //成功1
        "skipped": 0, 
        "failed": 0
    },
    "hits": { //搜索结果命中
        "total": {
            "value": 2, //查询出来总数
            "relation": "eq" //=
        },
        "max_score": 1.0, //最大分数,排序使用,下面数据通过倒序去排列
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "哈登",
                    "team_name": "火箭",
                    "position": "得分后卫",
                    "play_year": "10",
                    "jerse_no": "13"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "詹姆斯",
                    "team_name": "湖人",
                    "position": "小前锋",
                    "play_year": "15",
                    "jerse_no": "23"
                }
            }
        ]
    }
}
  • full text(match)全文查询

    • match_all查询

    match_all 会查询所有的数据

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match_all":{}
        },
        "from":0,
        "size":100
    }
    '
    
    • match查询

      需要注意的是使用match进行查询的话,type类型要是text可分词的全文搜索类型。

      curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
      {
          "query":{
              "match":{
                 "name":"库小里宝宝" 
              }
          },
          "from":0,
          "size":100
      }
      '
      
  • multi_match 多个查询

    • 先修改一条数据(将id=2的新增title字段)

      curl -X POST "http://172.25.45.150:9200/nba/_update/2" -H 'Content-Type:application/json' -d '
      {
          {
          "doc":{
          "name":"库里",
          "team_name":"勇士",
          "position":"控球后卫",
          "play_year":"10",
          "jerse_no":"30",
          "title":"the best shooter"
      	}
      }
      '
      
    • 使用multi_match 多字段查询

      curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
      {
          "query":{
              "multi_match":{
                  "query":"shooter",
                  "fields":["title","name"]
              }
          },
          "from":0,
          "size":100
      }
      '
      
    • 响应

      {
          "took": 2,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 1,
                  "relation": "eq"
              },
              "max_score": 0.2876821,
              "hits": [
                  {
                      "_index": "nba",
                      "_type": "_doc",
                      "_id": "2",
                      "_score": 0.2876821,
                      "_source": {
                          "name": "库里",
                          "team_name": "勇士",
                          "position": "控球后卫",
                          "play_year": "10",
                          "jerse_no": "30",
                          "title": "the best shooter"
                      }
                  }
              ]
          }
      }
      
  • match_phrase 查询

    类似于词条查询,精确查询

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match_phrase":{
                "position":"得分后卫"
            }
        },
        "from":0,
        "size":100
    }
    '
    
  • match_phrase _prefix

    ​ 前缀查询类似于 mysql中 like查询

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match_phrase_prefix":{
                "title":"the"
            }
        }
    }
    '
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈橙橙丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值