常规操作elasticSearch -分词检索
借助REST API
GET bank/_search?q=*&sort=account_number:asc
检索条件全部 按照account_number 升序
借助Kibana
GET bank/_search
{
"query":
{ "match_all": {}},
"sort": [
{ "account_number": { "order": "desc"}}
]
}
匹配全部 按照account_number 倒序
指定字段分页排序查找
GET bank/_search
{
"query":{
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
],
"from":0,
"size":2,
"_source": ["balance","firstname"]
}
匹配全部
balance字段倒序
from :0 ,size:2 相当于mysql 的 limit 0,2
“_source”: [“balance”,“firstname”] 相当于 select balance,firstname from …
结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "248",
"_score" : null,
"_source" : {
"firstname" : "West",
"balance" : 49989
},
"sort" : [
49989
]
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "854",
"_score" : null,
"_source" : {
"firstname" : "Jimenez",
"balance" : 49795
},
"sort" : [
49795
]
}
]
}
}
分词匹配查找 match
GET bank/_search
{
"query": {
"match": {
"address": "Mill lane"
}
}
}
分词: Mill lane -> Mill , lane
查找 address包含 Mill 或者lane的检索。匹配度越高,得分越高,排序越靠前
短语匹配查找 match_phrase
GET bank/_search
{
"query": {
"match_phrase": {
"address": "Mill lane"
}
}
}
查找 address包含 Mill lane的数据
多列字段分词查找multi_match
GET bank/_search
{
"query": {
"multi_match": {
"query": "mill Movico",
"fields": ["address","city"]
}
}
}
分词:“mill Movico” mill ,Movico
查找字段属性address ,city对分词的查找。匹配度越高 得分越高越靠前
综合查找
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 应当不影响查询结果,但影响得分,从而影响排序
匹配属性gender必须为m,age必须不是18,lastname为Wallace更好(非Wallace的也能查到,但是Wallace匹配的会提升得分,会靠前)
节段查询
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
]
}
}
}
检索 18<=age<=30 的数据
附:
gt: greater than 大于
gte: greater than or equal 大于等于
lt: less than 小于
lte: less than or equal 小于等于
区分精确检索和文本匹配
GET bank/_search
{
"query": {
"match": {
"age": 28
}
}
}
GET bank/_search
{
"query": {
"term": {
"age": 28
}
}
}
age为精确数字,结果集一样。
但是,match通常用于文本匹配检索。
term通常用于精确数字匹配。
无效检索:
GET bank/_search
{
"query": {
"term": {
"address": "789 Madison Street"
}
}
}
如果非要用term匹配文本,查不到结果
精确文本检索
#精确匹配
GET bank/_search
{
"query": {
"match": {
"address.keyword": "789 Madison "
}
}
}
这个检索,即使: "address" : "789 Madison Street" 也查不到它
只要你的存储内容与address.keyword完全匹配才可以!
对比精确文本检索的短语检测
GET bank/_search
{
"query": {
"match_phrase": {
"address": "789 Madison "
}
}
}
这个检索,可以查到: "address" : "789 Madison Street"