暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Elasticsearch索引基础查询-2

碧茂大数据 2022-04-20
183

更多精彩,请点击上方蓝字关注我们!

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"]
}


复制
  • 搜索结果:



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:必须同时符合所有条件

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"
]
}
}
]
}
}

复制

关注公众号:领取精彩视频课程&海量免费语音课程




文章转载自碧茂大数据,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论