elasticsearch rest api demo

搜索推荐

2017-08-10

270

0

技术:elasticsearch5.5

运行环境:mac10.12 + postman

demo功能:提供基本的rest 操作, 比如网上都说curl 啥的, 对比mysql查询

文档参考:http://demoworld.tech/books

安装es和初始化es数据

1. 安装过程google,

2. 初始化数据工具:http://git.oschina.net/youlixishi/demo-world/tree/master/src/elasticsearch.data.builder

3. 如果想可视化管理es,请google安装elasticsearch-head

4. 索引名字=demo_index_like_db, 索引type=demo_type_like_table

分页查询全部

sql=select * from tablename order by id asc limit 10 offset 0;

method=post

url=http://localhost:9200/demo_index_like_db/demo_type_like_table/_search

body=

{
     "query": {
        "match_all" : {//查询全部数据
        }
    },
    "size": 10,//每页10条
    "from":0,//offset
    "sort": { "id": { "order": "asc" } }//id从小到大排序
}

单个条件查询

精确查询

sql=select * from tablename where userName="鞠雨泽" order by id asc limit 10 offset 0;

method=post

url=http://localhost:9200/demo_index_like_db/demo_type_like_table/_search

body=

{
     "query": {
        "match" : {
            "id":190053
        }
    },
    "size": 10,
    "from":0,
    "sort": { "id": { "order": "asc" } }
}

前缀模糊查询

{
     "query": {
        "match_phrase_prefix" : {
            "content":"利哲"
        }
    },
    "size": 10,
    "from":0,
    "sort": { "id": { "order": "asc" } }
}

全文模糊查询

sql=

method=get

url=localhost:9200/demo_index_like_db/demo_type_like_table/_search?q="蓬昊"&size=2&from=0

 

多条件查询

sql=select * from tablename where shopId=1019 and userId=253 order by id asc limit 10 offset 0

method=post

url=http://localhost:9200/demo_index_like_db/demo_type_like_table/_search

body=

{
    "query": {
        "bool": {
            "must": [//must=&&,should=||,must_not=!must
                {
                    "match": {
                        "shopId": 1019
                    }
                },
                {
                    "match": {
                        "userId": 253
                    }
                }
            ]
        }
    },
    "size": 10,
    "from": 0,
    "sort": {
        "id": {
            "order": "asc"
        }
    }
}

数字范围查询

sql=select * from tablename where id>=1 and id<=9999999 order by id asc limit 10 offset 0

method=post

url=http://localhost:9200/demo_index_like_db/demo_type_like_table/_search

body=

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}//这里可以设各种查询条件
            },
            "filter": {
                "range": {
                    "id": {
                        "gte": 1,
                        "lte": 9999999
                    }
                }
            }
        }
    },
    "size": 10,
    "from": 0,
    "sort": {
        "id": {
            "order": "asc"
        }
    }
}

分组查询

sql=select * from tablename group by userId order by id asc limit 10 offset 0

method=post

url=http://localhost:9200/demo_index_like_db/demo_type_like_table/_search

body=

{
    "aggs": {
    "group_by_userId": {//这里
      "terms": {
        "field": "userId"//这里
      }
    }
  },
    "size": 2,
    "from":0,
    "sort": { "id": { "order": "asc" } }
}

 

分片计算=

shard = hash(routing) % number_of_primary_shards

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think