elasticsearch 使用流程:
1.创建索引库
2.设置索引字段映射
3.添加内容到索引库索引
4.使用查询检索
如下操作方法
默认elasticsearch已经安装过ik中文分词插件
http:
curl -XGET http:
curl -X[GET|POST|PUT] http:
http:
curl -XPUT http:
curl -XPUT http:
"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:
curl -XPOST http:
curl -XPOST http:
curl -XPOST http:
curl -XDELETE http:
curl -XPUT http:
curl -XPUT http:
{
"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"}}]}';
curl -XPOST "http://192.168.1.16:9200/sampoi/test/_search?pretty" -d '{"size":0,"aggs":{"max_modifytime":{"max":{"field":"modifytime"}}}}'
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}}}}}}';
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"}}}}'
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"
}}
]
}
'
curl -XPOST http://192.168.1.16:9200/wqms/WqRoute/_delete_by_query?conflicts=proceed -d '{
"query": {
"match_all": {}
}
}'
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":"青年"}}
]
}
}
}
';
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
}
]
}
}
}';