elasticsearch快速入門方法以及使用方法示例

elasticsearch 使用流程:

1.創建索引庫
2.設置索引字段映射
3.添加內容到索引庫索引
4.使用查詢檢索

如下操作方法

默認elasticsearch已經安裝過ik中文分詞插件

#!/bin/sh
#接入資料
http://www.cnblogs.com/xing901022/p/5910139.html
#查看Elasticsearch版本信息
curl -XGET http://localhost:9200/
#restful API 命令格式
curl -X[GET|POST|PUT] http://localhost:9200/索引庫名/類型名/ID標識 -d "POST參數(json)"


#es-ik 自定義詞庫使用方法
http://www.cnblogs.com/zlslch/p/6440891.html
#創建索引庫
curl -XPUT http://localhost:9200/samtest -d "{}"
#刪除索引庫
#curl -XDELETE http://localhost:9200/samtest
#增加映射
#http://blog.csdn.net/u010994304/article/details/50454025  映射使用方法
curl -XPUT http://localhost:9200/samtest/abc/_mapping -d '{
    "abc": {
        "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "yes"
        },
        "properties": {
            "test": {
                "type": "text",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}';
curl -XPUT http://localhost:9200/samtest/abc/_mapping -d '
{
     "abc": {
        "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        }
    }
}';
#刪除映射
#curl -XDELETE http://localhost:9200/samtest/abc/_mapping


info='{"first_name":"常","last_name":"濛濛","age" :25,"about":"I love to go rock climbing","interests":["sports","music"]}';
info='{"first_name":"aad","last_name":"dddd","age" :25,"about":"I love to go rock climbing","interests":["sports","music"]}';
#添加並索引
curl -XPUT http://localhost:9200/samtest/abc/6 -d "$info"
curl -XPUT http://localhost:9200/samtest/abc/6 -d '{"test":"美國是一箇中國的省"}'

#直接讀
curl http://localhost:9200/samtest/abc/6
#搜索1
curl http://localhost:9200/samtest/abc/_search?q=last_name:Smith
#搜索2???
curl http://localhost:9200/samtest/abc/_search -d '{"query":{"term":{"test":"美國"}}}'
#分詞測試ik
curl http://localhost:9200/_analyze?analyzer=ik_max_word -d '{"analyzer":"ik_max_word","text":"中華人民共和國國歌"}'
curl http://localhost:9200/_analyze -d '{"analyzer":"ik_max_word","text":"中華人民共和國國歌"}'

空間索引實驗

curl -XPOST http://192.168.1.16:9200/sampoi/_open 打開索引 
curl -XPOST http://192.168.1.16:9200/sampoi/_close 關閉索引 
curl -XPOST http://192.168.1.16:9200/sampoi/_flush 刷新索引 
curl -XPOST http://192.168.1.16:9200/sampoi/_stats 刷新索引 
curl -XDELETE http://192.168.1.16:9200/sampoi
curl -XPUT http://192.168.1.16:9200/attractions -d "{}"
#設置映射 
curl -XPUT http://192.168.1.16:9200/sampoi -d '
{
  "mappings": {
    "test": {
      "_all":{
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "term_vector": "no",
        "store": "yes",
        "enabled":true
      },
      "properties": {
        "name": {
          "type": "text"
        },
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}';
#添加
curl -XPUT http://192.168.1.16:9200/sampoi/test/1 -d '
{
  "name":     "馬泉營B口",
  "location": [ 116.510431,40.040612 ]
}';
curl -XPUT http://192.168.1.16:9200/sampoi/test/2 -d '
{
  "name":     "漢堡啤酒餐廳",
  "location": [ 116.511791,40.040477 ]
}';

curl -XPUT http://192.168.1.16:9200/sampoi/test/3 -d '
{
  "name":     "馬泉營C口停車場",
  "location": [ 116.510691,40.039383 ]
}';
curl -XPUT http://192.168.1.16:9200/sampoi/test/4 -d '
{
  "name":     "麗苑小區",
  "location": [ 116.516844,40.041461 ]
}';

#檢索 
curl -XPOST http://192.168.1.16:9200/sampoi/test/_search -d '{"query":{"match":{"name":"馬泉營"}}}';
curl http://192.168.1.16:9200/sampoi/test/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance":"1km", "distance_type":"plane", "location":{"lat":40.040612, "lon":116.510431}}}}}}';
curl http://192.168.1.16:9200/sampoi/test/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance":"1km", "distance_type":"plane", "location":[ 116.510431,40.040612]}}}}}';
curl http://192.168.1.16:9200/sampoi/test/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance":"1km", "location":{"lat":40.040612, "lon":116.510431}}}}}}';

#分頁查詢
curl -XPOST http://192.168.1.16:9200/sampoi/test/_search -d '{"from":2,"size":2}'
#統計總數
curl -XPOST http://192.168.1.16:9200/sampoi/test/_count -d '{"query":{"match":{"name":"馬泉營"}}}';
#排序
curl http://192.168.1.16:9200/sampoi/test/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance":"1km", "distance_type":"plane", "location":{"lat":40.040612, "lon":116.510431}}}}},"sort":[{"_geo_distance":{"location":{"lat":40.040612,"lon":116.510431},"order":"asc","unit":"m","distance_type":"plane"}}]}';

高級用法收集

#排序分頁
curl http://192.168.1.16:9200/sampoi/test/_search -d '{"from":1,"size":1,"query":{"bool":{"filter":{"geo_distance":{"distance":"1km", "distance_type":"plane", "location":{"lat":40.040612, "lon":116.510431}}}}},"sort":[{"_geo_distance":{"location":{"lat":40.040612,"lon":116.510431},"order":"asc","unit":"m","distance_type":"plane"}}]}';
#max聚合
curl -XPOST "http://192.168.1.16:9200/sampoi/test/_search?pretty" -d '{"size":0,"aggs":{"max_modifytime":{"max":{"field":"modifytime"}}}}'
#倒序1條
curl http://192.168.1.16:9200/sampoi/test/_search -d '{"size":1,"sort":[{"modifytime":"desc"}]}'


curl http://192.168.1.16:9200/wqms/WqSpot/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance":"1km", "distance_type":"plane", "Location":{"lat":39.9464127, "lon":116.3657631}}}}}}';

#nested用法,嵌套文檔mapping
curl -XPUT http://192.168.1.16:9200/wqms/WqRoute/_mapping?pretty -d '{"WqRoute":{"_all":{"analyzer":"ik_max_word","enabled":true,"search_analyzer":"ik_max_word","store":"yes","term_vector":"no"},"properties":{"CreateTime":{"format":"yyyy-MM-dd HH:mm:ss","type":"date"},"CreateTimeInt":{"type":"long"},"Difficulty":{"type":"long"},"Duration":{"type":"string"},"EmpId":{"type":"long"},"Image":{"type":"string"},"Info":{"type":"string"},"IsHot":{"type":"boolean"},"ModifyTime":{"format":"yyyy-MM-dd HH:mm:ss","type":"date"},"ModifyTimeInt":{"type":"long"},"OriginPoint":{"type":"string"},"Pageview":{"type":"long"},"PublishTime":{"format":"yyyy-MM-dd HH:mm:ss","type":"date"},"PublishTimeInt":{"type":"long"},"Route":{"properties":{"Location":{"type":"geo_point"},"Name":{"type":"string"}},"type":"nested"},"RouteId":{"type":"long"},"SpotIds":{"type":"long"},"Status":{"type":"integer"},"Time":{"type":"string"},"Title":{"type":"string"}}}}'

#nested,嵌套文檔put index
curl -XPUT http://192.168.1.16:9200/wqms/WqRoute/10 -d '
{"Author":"","CreateTime":"2017-08-14 19:23:10","CreateTimeInt":1502709790124,"Difficulty":0,"Duration":"7","EmpID":0,"Image":"ant/wqzjy/route/1502709789_4594.jpg","Info":"","IsHot":false,"ModifyTime":"2017-08-14 19:23:10","ModifyTimeInt":1502709790124,"OriginPoint":110000,"Pageview":0,"PublishTime":"1970-01-01 08:00:00","PublishTimeInt":0,"Route":[{"Location":[116.39564503788,39.92998577808],"Name":"北京"},{"Location":[114.35164211776,34.801854175837],"Name":"開封"},{"Location":[115.37524581828,31.766261672209],"Name":"商城"},{"Location":[115.77931490356,31.47909281966],"Name":"金寨"},{"Location":[116.22007036688,30.901821144678],"Name":"嶽西"},{"Location":[117.05873877211,30.537897817381],"Name":"安慶"},{"Location":[0,0],"Name":"九華山"},{"Location":[118.07754612726,30.27774589512],"Name":"黃山"},{"Location":[117.91075047481,30.014778480875],"Name":"黟縣"},{"Location":[117.60052812882,29.873705688292],"Name":"祁門"}],"RouteId":10,"SpotIds":[47476],"Status":1,"Time":"","Title":"北京—黃山—祁門7日自駕"}
'
#創建索引別名
curl -XPOST http://192.168.1.16:9200/_aliases -d '  
{
    "actions": [  
        { "add": {  
            "alias": "wqms1",  
            "index": "wqms"  
        }}  
    ]  
}  
'  
#刪除一個type
curl -XPOST http://192.168.1.16:9200/wqms/WqRoute/_delete_by_query?conflicts=proceed -d '{
  "query": {
    "match_all": {}
  }
}'

#按匹配度降序(每符合一個term,默認匹配度1.0)
curl -XPOST http://192.168.1.16:9200/wqms/WqRoute/_search?pretty -d'
{
 "query": {
    "bool": {
      "should":[
        {"term": {"OriginPoint":"110000"}},
        {"term": {"Duration":"1"}},
        {"term": {"Time":"4"}},        
        {"term": {"Crowds":"青年"}}
      ]      
    }  
  }  
}
';

#按匹配度降序,(基礎匹配度1.0+weight權重,得出實際匹配度)
curl -XPOST http://192.168.1.16:9200/wqms/WqRoute/_search?pretty -d'
{
    "query": {
        "function_score": {
          "query": {
            "bool": {
              "should":[
                {"term": {"OriginPoint":"110000"}},
                {"term": {"Duration":"1"}},
                {"term": {"Time":"4"}},        
                {"term": {"Crowds":"青年"}}
              ]      
            }  
          }  ,
          "boost": "1.0", 
          "functions": [
              {
                  "filter": { "match": { "OriginPoint": "110000" } },                  
                  "weight": 1
              },
              {
                  "filter": { "match": { "Crowds": "青年" } },
                  "weight": 4
              }
          ]
        }
    }
}';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章