Elastic_Dev_Tools

加油酱發表於2024-11-14
GET _search
{
  "query": {
    "match_all": {}
  }
}

GET /_analyze
{
  "analyzer": "ik_smart",
  "text": "我愛北京天安門"
}
# 建立表
PUT /user
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer",
        "index": true
      },
      "info": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email": {
        "type": "keyword"
      },
      "name": {
        "type": "object", 
        "properties": {
          "firstName": {
            "type": "keyword"
          },
          "lastName": {
            "type": "keyword"
          }
          
        }
      }
    }
  }
}
#檢視索引庫
GET /hotel
#刪除索引庫
DELETE /user
#修改索引庫
PUT /user/_mapping
{
  "properties": {
    "weight": {
      "type": "double"
    }
  }
}

#文件操作
#新建文件
POST /user/_doc/
{
  "age":30,
  "email":"張@qq.com",
  "info":"JAVA高階程式設計師",
  "name":{
    "firstName":"張",
    "lastName": "123"
  },
  "weight":"75"
}
#查詢索引庫所有資料
GET /user/_search
{
  "query": {
    "match_all": {}
  }
}
#查詢某一個文件資料
GET /user/_doc/1
#刪除某一個文件資料
DELETE /user/_doc/lDD-FJMBA6ani3V3HjCM
#修改文件資料
#全域性修改
PUT /user/_doc/1
{
  "age":30,
  "email":"jianghuanhuan@qq.com",
  "info":"JAVA高階程式設計師",
  "name":{
    "firstName":"蔣",
    "lastName":"歡歡"
  },
  "weight":"75"
}
#區域性修改
POST /user/_update/1
{
  "doc": {
    "age":66
  }
}

#建立hotel索引庫
PUT /hotel
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type": "keyword",
        "copy_to": "all"
      },
      "starName":{
        "type": "keyword",
         "copy_to": "all"
      },
      "business":{
        "type": "keyword"
      },
      "location":{
        "type": "geo_point"
      },
      "pic":{
        "type": "keyword",
        "index": false
      },
      "isAd":{
        "type": "text"
      },
      "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
GET /hotel
DELETE /hotel
#查詢索引庫所有資料
GET /hotel/_search
{
  "query": {
    "match_all": {}
  }
}
#全文檢索的match查詢
GET /hotel/_search
{
  "query": {
    "match": {
      "all": "如家"
    }
  }
}
#全文檢索的multi_match查詢
GET /hotel/_search
{
  "query": {
    "multi_match": {
      "query": "如家",
      "fields": ["brand","city","name"]
    }
  }
}
#精確查詢term
GET /hotel/_search
{
  "query": {
    "term": {
      "brand": {
        "value": "麗笙"
      }
    }
  }
}
#精確查詢range
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 200,
        "lte": 300
      }
    }
  }
}

#地理座標查詢 geo_distance
GET /hotel/_search
{
  "query": {
    "geo_distance":{
      "distance": "1.5km",
      "location": "31.21,121.5"
    }
  }
}

#複合型查詢:function_score---廣告置頂
GET /hotel/_search
{
  "query": {
  "function_score": {
    "query": {
      "match": {
        "all": "如家"
      }
    },
    "functions": [
      {
        "filter": {
         "range": {
           "price": {
             "gte": 300,
             "lte": 450
           }
         }
        },
        "weight": 10
      } 
    ],
    "boost_mode": "multiply"
  }   
  }
}


GET /hotel/_search
{
"sort": [
  {
    "price": {
      "order": "desc"
    }
  },
  
  {
    "_geo_distance": {
      "location": "31.25,121.5",
      "order": "asc"
    
     }
   }
  ] 
}
  
}

GET /hotel/_search---廣告置頂
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "all": "如家"
        }
      },
      "functions": [
        {
         "filter": {
           "term": {
             "city": "北京"
           }
         },
         "weight": 10
        }
       
      ]
    }
  }
}

GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "geo_distance": {
            "distance": "10km",
            "location": {
              "lat": 31.21,
              "lon": 121.5
            }
          }
        }
      ]
    }
  }
}



GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "term": {
            "city": "北京"
          }
        },
        {
          "term": {
            "brand": "如家"
          }
        }
      ]
    }
  },
  "sort": [
    {
     "_geo_distance": {
       "location": {
         "lat": 31.25,
         "lon": 121.5
       },
       "order": "asc"
     }
    },
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": "<em>",
        "post_tags": "</em>",
        "require_field_match": "false"
      }
    }
  }
}


#結果集排序
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 31.21,
          "lon": 121.5
        },
        "order": "asc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "score": {
        "order": "desc"
      },
      "price": {
        "order": "asc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "isAD": {
        "order": "desc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "from": 191,
  "size": 10,
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match": {
      "name": "如家"
    }
  },
  "highlight": {
    "fields": {
      "name": {
       "pre_tags": "<em>",
       "post_tags": "</em>"
      }
    }
  }
}

GET /hotel/_search
{
  "query": {
    "match": {
      "brand": "如家"
    }
  },
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": "<em>",
        "post_tags": "</em>",
        "require_field_match": "false"
      }
    }
  }
}

POST /hotel/_update/197837109
{
    "doc": {
        "isAD": true
    }
}
POST /hotel/_update/1557882030
{
    "doc": {
        "isAD": true
    }
}
POST /hotel/_update/197492277
{
    "doc": {
        "isAD": true
    }
}
POST /hotel/_update/200214824
{
    "doc": {
        "isAD": true
    }
}





GET /hotel/_search
{
  "query": {
   "match_all": {} 
  },
  "from": 191,
  "size": 10
}

GET /hotel/_search
{
  "query": {
    "match": {
      "all": "上海"
    }
  }
}