ElasticSerch查詢語句

#新增索引庫
PUT /heima
#查詢索引庫
GET /heima
#刪除索引庫
DELETE /heima

#創建映射
PUT /heima/_mapping
{
“properties”:{
“title”:{
“type”:“text”,
“analyzer”:“ik_smart”
},
“images”:{
“type”:“keyword”,
“index”:“false”
},
“price”:{
“type”:“float”
}
}
}

#創建索引庫和映射
PUT /heima2
{
“mappings”: {
“properties”: {
“title”:{
“type”: “text”,
“analyzer”: “ik_max_word”
},
“images”:{
“type”: “keyword”,
“index”:“false”
},
“price”:{
“type”: “float”
}
}
}
}

#查看索引庫的名稱
GET /heima/_mapping
GET /heima2/_mapping

#新增文檔
POST /heima/_doc
{
“title”:“小米手機”,
“images”:“http://image.leyou.con/11222.jpg”,
“price”:2699.00
}

#新增文檔並指定id
POST /heima/_doc/2
{
“title”:“小米手機”,
“images”:“http://image.leyou.con/11222.jpg”,
“price”:2699.00
}

#查看文檔
GET /heima/_doc/3

#修改文檔
PUT /heima/_doc/1
{
“title”:“華爲手機”,
“images”:“http://image.leyou.con/11222.jpg”,
“price”:2899.00
}

PUT /heima/_doc/3
{
“title”:“大米手機pro”,
“images”:“http://image.leuoi.jpg”,
“price”:3099.0
}

#刪除文檔
DELETE /heima/_doc/3

#新增未映射的字段
POST /heima/_doc/4
{
“title”:“超大冪手機”,
“images”:“http://leyou.com/12334”,
“price”:3999.00,
“stock”:200,
“saleable”:true,
“subtitle”:“超級雙攝,以萬像素”
}

#索引庫的映射關係:
GET /heima

#動態模板
PUT heima03
{
“mappings”: {
“properties”: {
“title”: {
“type”: “text”,
“analyzer”: “ik_max_word”
}
},
“dynamic_templates”: [
{
“string”:{
“match_mapping_type”:“string”,
“mapping”:{
“type”:“keyworld”
}
}
}
]
}
}

#新增一條數據
POST /heima3/_doc/1
{
“title”:“超大米手機”,
“images”:“http://image.leyou.com/12479122.jpg”,
“price”:3299.00
}

#查看映射
GET /heima/_mapping

#插入兩條數據
PUT /heima/_doc/5
{
“title”:“小米電視4A”,
“images”:“http://images.leyou.com/1111”,
“price”:3899.00
}

PUT /heima/_doc/6
{
“title”:“樂視電視4X”,
“images”:“http://images.leyou/1222”,
“price”:2499.00
}

#查詢所有
GET /heima/_search
{
“query”:{
“match_all”: {}
}
}

#分詞查詢match
GET /heima/_search
{
“query”:{
“match”:{
“title”:“小米電視”
}
}
}

#精確查找,小米和電視
GET /heima/_search
{
“query”:{
“match”: {
“title”: {“query”:“小米電視”,“operator”: “and”}
}
}
}

GET /heima/_mapping
#詞條匹配,查詢的是一個詞條,不會被分詞
GET /heima/_search
{
“query”:{
“term”:{
“price”:2699.00
}
}
}

#fluzzy模糊查詢

模糊查詢 fuzzy

GET /heima/_search
{
“query”: {
“fuzzy”: {
“title”: {“value”: “小米”, “fuzziness”: 1}
}
}
}

#範圍查詢
GET /heima/_search
{
“query”: {
“range”: {
“price”: {
“gte”: 1000,
“lte”: 3000
}
}
}
}

#布爾查詢
GET /heima/_search
{
“query”: {
“bool”: {
“must”: {
“match”: {
“title”: “小米”
}
},

"must_not": {
  "match": {
    "title": "電視"
  }
  }
}

}
}

#排序由
GET /heima/_search
{
“query”: {
“match”: {
“title”: “小米電視”
}
},
“sort”: [
{
“price”: {
“order”: “desc”
}
}
]
}

GET /heima/_search
{
“query”:{
“match”:{
“title”:“手機”
}
},
“highlight”: {
“pre_tags”: “”,
“post_tags”: “
”,
“fields”: {
“title”: {}
}
}
}

GET /heima/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“price”: {
“order”: “asc”
}
}
],
“from”:0,
“size”: 20
}

#filter過濾

GET /heima/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“title”: “小米手機”
}
},
{
“range”: {
“price”: {
“gte”: 2000,
“lte”: 3200
}
}
}
]
}
}
}

GET /heima/_search
{
“query”: {
“bool”: {
“must”: {
“match”: {
“title”: “小米手機”
}
},
“filter”: [
{
“range”:{
“price”:{
“gt”:2000,
“lt”:3200
}
}
}
]
}
}
}

#source 篩選
GET /heima/_search
{
“_source”: [“title”,“price”],
“query”: {
“term”: {
“price”: 2699
}
}
}

#指定includes和excludes

GET /heima/_search
{
“_source”: {
“includes”: [
“title”,
“price”
]
},
“query”: {
“term”: {
“price”: 2699
}
}
}

GET /heima/_search
{
“_source”: {
“excludes”: [“images”]
},
“query”: {
“term”: {
“price”: {
“value”: “2699”
}
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章