PUT /lib3
{
"settings":{
"number_of_shards" : 3,
"number_of_replicas" : 0
},
"mappings":{
"user":{
"properties":{
"name": {"type":"text"},
"address": {"type":"text"},
"age": {"type":"integer"},
"interests": {"type":"text"},
"birthday": {"type":"date"}
}
}
}
}
GET /lib3/user/_search?q=name:lisi
GET /lib3/user/_search?q=name:zhaoliu&sort=age:desc
PUT /lib3/user/1
{
"name":"zhaoliu",
"address":"zhe jiang shen hang zhou shi",
"age":50,
"birthday":"1999-06-06",
"interests":"he jiu,xiedaima"
}
PUT /lib3/user/2
{
"name":"laogao",
"address":"shan dong shen ji nan shi",
"age":50,
"birthday":"1992-08-06",
"interests":"paoniu,xiedaima"
}
PUT /lib3/user/3
{
"name":"laowang",
"address":"shan xi shen xi an shi",
"age":22,
"birthday":"1989-06-06",
"interests":"xiedaima,da you xi"
}
PUT /lib3/user/4
{
"name":"jiajia",
"address":"jiang su shen nan jing shi",
"age":22,
"birthday":"1996-02-06",
"interests":"xue xi,zuo fan"
}
PUT /lib3/user/5
{
"name":"xiao ren",
"address":"gan su shen bai yin shi",
"age":22,
"birthday":"1998-08-22",
"interests":"kan dian ying,yue mei"
}
# max_score:和当前搜索相关度的匹配分数
GET /lib3/user/_search?q=name:laowang
# 查询兴趣为写代码,并根据年龄降序排序
GET /lib3/user/_search?q=interests:xie dai ma&sort=age:desc
term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword 、numeric、date。
term:查询某个字段里含有某个关键词的文档
GET /lib3/user/_search/
{
"query":{
"term": {
"name": {
"value": "jiajia"
}
}
}
}
terms:查询某个字段里含有多个关键词的文档
GET /lib3/user/_search
{
"query":{
"terms":{
"interests": ["paoniu","xiedaima"]
}
}
}
from:从哪一个文档开始
size:需要的个数
GET /lib3/user/_search
{
"from":0,
"size":2,
"query":{
"terms":{
"interests": ["paoniu","xiedaima"]
}
}
}
GET /lib3/user/_search
{
"version":true,
"query":{
"terms":{
"interests": ["hejiu","changge"]
}
}
}
match query知道分词器的存在,会对filed进行分词操作,然后再查询
GET /lib3/user/_search
{
"query":{
"match": {
"name": "jiajia,laogao"
}
}
}
GET /lib3/user/_search/
{
"query": {
"match": {
"age": "22"
}
}
}
match_all:查询所有文档
GET /lib3/user/_search/
{
"query": {
"match_all": {}
}
}
multi_match:可以指定多个字段
GET /lib3/user/_search/
{
"query": {
"multi_match": {
"query": "xie dai ma",
"fields": ["interests","name"]
}
}
}
match_phrase:短语匹配查询
ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变:
# 短语匹配查询
GET /lib3/user/_search/
{
"query": {
"match_phrase": {
"interests": "he jiu,xie dai ma"
}
}
}
GET /lib3/user/_search/
{
"_source":["address","name"],
"query": {
"match": {
"interests": "he jiu"
}
}
}
GET /lib3/user/_search/
{
"query": {
"match_all": {}
},
"_source": {
"includes": ["name","address"],
"excludes": ["age","birthday"]
}
}
使用通配符*
GET /lib3/user/_search/
{
"_source": {
"includes": "addr*",
"excludes": ["age","bir*"]
},
"query": {
"match_all": {}
}
}
使用sort实现排序:
desc:降序,asc升序
# 排序(降序)
GET /lib3/user/_search/
{
"query": {
"match_all": {}
},
"sort":[
{
"age": {
"order": "desc"
}
}]
}
GET /lib3/user/_search/
{
"query": {
"match_phrase_prefix": {
"name": {
"query":"lao"
}
}
}
}
range:实现范围查询
参数:from,to,include_lower,include_upper,boost
include_lower:是否包含范围的左边界,默认是true
include_upper:是否包含范围的右边界,默认是true
# 范围查询
GET /lib3/user/_search/
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2019-05-01"
}
}
}
}
# 区间内查询(包含与不包含)
GET /lib3/user/_search/
{
"query": {
"range": {
"age": {
"from": 20,
"to": 28,
"include_lower":true,
"include_upper":false
}
}
}
}
允许使用通配符* 和 ?来进行查询
*代表0个或多个字符
?代表任意一个字符
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
}
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "jia?ia"
}
}
}
value:查询的关键字
boost:查询的权值,默认值是1.0
min_similarity:设置匹配的最小相似度,默认值为0.5,对于字符串,取值为0-1(包括0和1);对于数值,取值可能大于1;对于日期型取值为1d,1m等,1d就代表1天
prefix_length:指明区分词项的共同前缀长度,默认是0
max_expansions:查询中的词项可以扩展的数目,默认可以无限大
# 模糊查询
GET /lib3/user/_search/
{
"query":{
"fuzzy":{
"name":"jiaji"
}
}
}
GET /lib3/user/_search/
{
"query":{
"fuzzy":{
"interests": {
"value":"paoni"
}
}
}
}
# 查询信息高亮显示
GET /lib3/user/_search
{
"query":{
"match": {
"interests": "paoniu"
}
},
"highlight":{
"fields": {
"interests": {}
}
}
}