20.Elasticsearch索引基础查询-2
2022-04-19 本文已影响0人
大勇任卷舒
20.1 确切值搜索
- 默认情况下,搜索会返回所有字段。如果我们不希望返回整个源文档,我们可以从源文档中只求几个字段来返回。
- 下面的例子展示了只返回文档中的两个字段:account_number 和 balance字段。
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -
d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
‘
-
搜索结果:
20.2 bool查询
- bool查询允许我们使用布尔逻辑将较小的查询组合成较大的查询。
- 示例:将两个match查询组合在一起,返回address中包含“mill”和“lane”的账户。
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
‘
- 此搜索相当于:SELECT * FROM bank WHERE address LIKE‘%mill%lane%‘,符合条件的只有一条。
-
运行结果:
- 以上bool查询中使用的条件为must,意思为所有条件必须同时符合,另外还有更多的条件可选
- should:多个条件中满足一个即可
- must_not:必须没有设定条件
- must:必须同时符合所有条件
20.3 全文搜索
- 比如想要搜索下所有喜欢攀岩(rock climbing)的雇员
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.53484553,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 0.53484553,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "2",
"_score": 0.26742277,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
}
]
}
}
大数据视频推荐:
腾讯课堂
CSDN
ELK入门精讲
AIOps智能运维实战
ELK7 stack开发运维
大数据语音推荐:
ELK7 stack开发运维
企业级大数据技术应用
大数据机器学习案例之推荐系统
自然语言处理
大数据基础
人工智能:深度学习入门到精通