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
              }
          ]
        }
    }
}';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值