Elasticsearch学习(二)————搜索

Elasticsearch

1.query string search

1.1.搜索全部
// 1. 
GET http://ip:9200/test/test/_search
	结果:
	{
    "took": 86, 				    # 耗费的时间:ms 
    "timed_out": false,				# 是否超时
    "_shards": {					# 数据存储在5个主分片上
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {						# 匹配结果
        "total": 3,                    # 查询到三个document
        "max_score": 1,                # 相关度的匹配分数:分数越高越相关
        "hits": [
            {
                "_index": "test",       # 索引 index
                "_type": "test",		# type
                "_id": "2",             # id:唯一
                "_score": 1,           # 相关度的匹配分数:分数越高越相关
                "_source": {			# 存储的json数据
                    "first_name": "小翠",   # field
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": 1,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            }
        ]
    }
}
1.2.以about字段中含有climbing字符查询并根据age字段降序排列 可以多个排序,用逗号分隔:
// 2.
GET http://ip:9200/test/test/_search?q=about:climbing&sort=age:desc,price:desc
GET http://ip:9200/test/test/_search?q=about:climbing&sort=age:desc
{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": null,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [           #排序字段的值
                    25
                ]
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "2",
                "_score": null,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [
                    18
                ]
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": null,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [
                    18
                ]
            }
        ]
    }
}

2.query DSL

2.1.搜索全部
// 1. 
POST http://ip:9200/test/test/_search
语法:
	{
		"query":{
			"match_all":{}
		}
	}
	结果:
	{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 1,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "price": 15000,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": 1,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "first_name": "小雪",
                    "last_name": "xiaoxue",
                    "age": 20,
                    "about": "climbing",
                    "interests": [
                        "dancing",
                        "music"
                    ]
                }
            }
        ]
    }
}
2.2.以about字段中含有climbing字符查询并根据age字段降序排列 可以多个排序,用逗号分隔
// 1.
POST http://ip:9200/test/test/_search
语法:
	{
	"query":{
		"match":{
			"about":"climbing"
		}
	},
	"sort":[
		{
			"age":"desc"
		}
	]
}

结果:
{
    "took": 115,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": null,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": null,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [
                    25
                ]
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "3",
                "_score": null,
                "_source": {
                    "first_name": "小雪",
                    "last_name": "xiaoxue",
                    "age": 20,
                    "about": "climbing",
                    "interests": [
                        "dancing",
                        "music"
                    ]
                },
                "sort": [
                    20
                ]
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "2",
                "_score": null,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [
                    18
                ]
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "4",
                "_score": null,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "price": 15000,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [
                    18
                ]
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": null,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "sort": [
                    18
                ]
            }
        ]
    }
}
2.3. 分页数据
// 1.
POST http://ip:9200/test/test/_search
语法:
{
	"query":{
		"match_all":{} 		# 查询所有
	},
	"from":0,   			# 从第几条数据开始  0:第一条
	"size":1				# 展示几条数据
}
2.4.只展示指定的filed
// 1.
POST http://ip:9200/test/test/_search
语法:
{
	"query":{
		"match_all":{}
	},
	"_source":[
		"first_name",
		"age"
		]
}

	结果:
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 1,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "first_name": "小雪",
                    "age": 20
                }
            }
        ]
    }
}

3.query filter

3.1.多个查询条件:about字段必须包含"climbing";age大于20岁
// 1.
POST http://ip:9200/test/test/_search
	语法:
{
	"query":{
		"bool":{
			"must":{
				"match":{
					"about":"climbing"
				}		
			},
			"filter":{
				"range":{
					"age":{
						"gt":20
					}
				}
			}
		}
	}
}

	结果
{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.26742277,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 0.26742277,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            }
        ]
    }
}
3.2. 多个查询条件:must、 should、 must_not
POST http://ip:9200/test/test/_search
{
	"query":{
		"bool":{
			"must":{               # 必须匹配
				"match":{
					"first_name":"小翠"
				}		
			},
			"should":{              # 可以匹配,也可以不匹配
				"match":{
					"last_name": "xue"
				}
			},
			"must_not":{			 # 必须不匹配
				"match":{
					"last_name": "cui"
				}
			}
		}
	}
}

4.full-test search

4.1.全文检索
// 1.
POST http://ip:9200/test/test/_search
	语法:
{
	"query":{
			"match":{
				"about":"go climbing"
			}		
	}
}
 分析:es将about这个filed拆解成每个词(term),建立倒排索引,每个term对应相应的document_id
	结果:
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 7,
        "max_score": 0.7447149,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "2",
                "_score": 0.7447149,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "4",
                "_score": 0.7447149,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "price": 15000,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 0.61562645,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": 0.61562645,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "5",
                "_score": 0.2876821,
                "_source": {
                    "first_name": "花花",
                    "last_name": "huahau",
                    "age": 16,
                    "price": 20000,
                    "about": "climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "7",
                "_score": 0.25759193,
                "_source": {
                    "about": "go"
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "3",
                "_score": 0.25759193,
                "_source": {
                    "first_name": "小雪",
                    "last_name": "xiaoxue",
                    "age": 20,
                    "about": "climbing",
                    "interests": [
                        "dancing",
                        "music"
                    ]
                }
            }
        ]
    }
}
4.2.短语搜索:匹配短语
// 1.
POST http://ip:9200/test/test/_search
语法:
{
	"query":{
			"match_phrase":{
				"about":"rock climbing"
			}		
	}
}

	结果:
{
    "took": 20,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0.9748371,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 0.9748371,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "2",
                "_score": 0.7447149,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "4",
                "_score": 0.7447149,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "price": 15000,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": 0.6156264,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            }
        ]
    }
}	

5.highlight search

5.1.关键字高亮
// 1. 
语法:
{
	"query":{
			"match":{
				"about":"climbing"    # 关键字
			}		
	},
	"highlight":{
		"fields":{
			"about":{}    			# 字段
		}
	}
}

	结果:
{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 0.48741856,
        "hits": [
            {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 0.48741856,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "highlight": {
                    "about": [
                        "I love to go rock <em>climbing</em>"   # <em> 标签html中高亮显示
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "2",
                "_score": 0.37235746,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cuicui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "highlight": {
                    "about": [
                        "I love to go rock <em>climbing</em>"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "4",
                "_score": 0.37235746,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "price": 15000,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "highlight": {
                    "about": [
                        "I love to go rock <em>climbing</em>"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "5",
                "_score": 0.2876821,
                "_source": {
                    "first_name": "花花",
                    "last_name": "huahau",
                    "age": 16,
                    "price": 20000,
                    "about": "climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "highlight": {
                    "about": [
                        "<em>climbing</em>"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "3",
                "_score": 0.25759193,
                "_source": {
                    "first_name": "小雪",
                    "last_name": "xiaoxue",
                    "age": 20,
                    "about": "climbing",
                    "interests": [
                        "dancing",
                        "music"
                    ]
                },
                "highlight": {
                    "about": [
                        "<em>climbing</em>"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "test",
                "_id": "AWrVpGsO0WDJvaOeQOjd",
                "_score": 0.12820786,
                "_source": {
                    "first_name": "小翠",
                    "last_name": "cui",
                    "age": 18,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                },
                "highlight": {
                    "about": [
                        "I love to go rock <em>climbing</em>"
                    ]
                }
            }
        ]
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值