栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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"
        }
    }
}
'
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/741895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号