curl -X' :// : / ? ' -d ''
# pretty 返回格式化的结果 curl -XGET http://localhost:9200/_cluster/health?pretty
status 字段指示着当前集群在总体上是否工作正常。它的三种颜色含义如下:
-
green
所有的主分片和副本分片都正常运行。
-
yellow
所有的主分片都正常运行,但不是所有的副本分片都正常运行。
-
red
有主分片没能正常运行。
curl -XGET 'http://localhost:9200/_count?pretty' -H 'Content-Type:application/json' -d ' { "query": { "match_all": {} } } '3、保存文档
# 如果索引不存在会自动创建 curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d' { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music1" ] } ' curl -X PUT "localhost:9200/megacorp/employee/2?pretty" -H 'Content-Type: application/json' -d' { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } ' curl -X PUT "localhost:9200/megacorp/employee/3?pretty" -H 'Content-Type: application/json' -d' { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] } '
路径 /megacorp/employee/1 包含了三部分的信息:
-
megacorp
索引名称,相当于数据库中的表
-
employee
类型名称,新版本没有了
-
1
数据的ID
请求体 —— JSON 文档 —— 包含了这位员工的所有详细信息,他的名字叫 John Smith ,今年 25 岁,喜欢攀岩。
4、检索文档curl -X GET "localhost:9200/megacorp/employee/1?pretty"5、获取所有
curl -X GET "localhost:9200/megacorp/employee/_search?pretty"6、按字段查询
curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query" : { "match" : { "about" : "cabinets" } } } '
两种方式返回结果一直,第二种使用的是请求体的方式,match:用于匹配
7、复杂查询返回last_name为smith,age大于30的数据
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query" : { "bool": { "must": { "match" : { "last_name" : "smith" } }, "filter": { "range" : { "age" : { "gt" : 30 } } } } } } '
搜索下所有喜欢攀岩(rock climbing)的员工:
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query" : { "match" : { "about" : "rock climbing" } } } '
“_score” : 1.4167401, 相关性得分
Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 about 属性清楚地写着 “rock climbing” 。
但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里提到了 “rock” 。因为只有 “rock” 而没有 “climbing” ,所以她的相关性得分低于 John 的。
短语查询 match_phrase
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query" : { "match_phrase" : { "about" : "rock climbing" } } } '8、高亮搜索
许多应用都倾向于在每个搜索结果中 高亮 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。
再次执行前面的查询,并增加一个新的 highlight 参数:
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } } } '
当执行该查询时,返回结果与之前一样,与此同时结果中还多了一个叫做 highlight 的部分。这个部分包含了 about 属性匹配的文本片段,并以 HTML 标签 封装:
SQL Api 1、批量插入数据curl -X PUT "localhost:9200/library/_bulk?refresh&pretty" -H 'Content-Type: application/json' -d' {"index":{"_id": "Leviathan Wakes"}} {"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561} {"index":{"_id": "Hyperion"}} {"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482} {"index":{"_id": "Dune"}} {"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604} '2、执行sql查询
curl -X POST "localhost:9200/_sql?format=txt&pretty" -H 'Content-Type: application/json' -d' { "query": "SELECT * FROM library WHERe release_date < 2000-01-01" } '3、SQL CLI
# 进入sql命令行 elasticsearch-sql-cli
# 执行sql查询 sql> SELECt * FROM library WHERe release_date < '2000-01-01'; author | name | page_count | release_date ---------------+---------------+---------------+------------------------ Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
curl -X POST "localhost:9200/_sql?format=txt&pretty" -H 'Content-Type: application/json' -d' { "query": "SELECt * FROM library ORDER BY page_count DESC LIMIT 5" } ' author | name | page_count | release_date ----------------+---------------+---------------+------------------------ Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z James S.A. Corey|Leviathan Wakes|561 |2011-06-02T00:00:00.000Z Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z4、响应数据格式
返回为json格式
curl -X POST "localhost:9200/_sql?format=json&pretty" -H 'Content-Type: application/json' -d' { "query": "SELECt * FROM library ORDER BY page_count DESC", "fetch_size": 5 } '5、SQL 查询语句
SELECt [TOP [ count ] ] select_expr [, ...] [ FROM table_name ] [ WHERe condition ] [ GROUP BY grouping_element [, ...] ] [ HAVINg condition] [ ORDER BY expression [ ASC | DESC ] [, ...] ] [ LIMIT [ count ] ] [ PIVOT ( aggregation_expr FOR column IN ( value [ [ AS ] alias ] [, ...] ) ) ]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)