先前条件1、elasticsearch测试数据集
https://blog.csdn.net/qq_35843514/article/details/120205873
先前条件2、批量插入
POST bank/account/_bulk
批量插入如图所示
1、Query DSL基本查询使用
GET bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"_source": ["firstname","lastname","balance"]
}
-- 其中"match_all"表示匹配所有
-- “sort”表示查询条件 是按"balance"降序排序
-- "from"查询起始位置
-- "size"查询数据的量(这里是从第0号开始,显示5条数据,相当于sql语句的limit)
-- "_source"查询出需要显示的字段
2、match既可以作为模糊查询,全文检索
GET bank/_search
{
"query":{
"match": {
"address": "Mill Lane"
}
}
}
-- "address"中包含"Mill“、”Lane"、“Mill Lane”都会被查询出来(相当于分词)
-- 全文检索最后会按评分排序
3、短语匹配之"match_phrase"
GET bank/_search
{
"query":{
"match_phrase": {
"address": "Mill Lane"
}
}
}
-- "match_phrase"表示只会匹配"address"中包含"Mill Lane"的记录
4、多字段匹配之 "multi_match"
GET bank/_search
{
"query":{
"multi_match": {
"query": "mill",
"fields": ["address","city"]
}
}
}
-- 只要字段"address"或"city"中包含"mill"的记录就会被查询出来(如果查询的是多字段也会进行分词)
5、复合查询之"bool"
GET bank/_search
{
"query":{
"bool": {
"must": [
{"match": {
"gender": "M"
}},
{"match": {
"address": "mill"
}}
],
"must_not": [
{"match": {
"age": "18"
}}
],
"should": [
{"match": {
"lastname": "Wallace"
}}
]
}
}
}
-- "must"必须匹配
-- "must_not"必须不匹配
-- "should"可以匹配,可以不配配,匹配的话相关性评分会高
6、结果过滤之"filter"
GET bank/_search
{
"query":{
"bool": {
"must": [
{"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
]
}
}
}
GET bank/_search
{
"query":{
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
must查询出来有相关性评分
filter查询出来没有相关性评分
7、非text文本字段之"term",全文用"match"
GET bank/_search
{
"query":{
"term": {
"balance":"32838"
}
}
}
网友评论