注意:以下命令都是使用sense测试(ElasticSearch第二步-CRUD之Sense),且数据都已经使用过IK分词。
以下测试数据来源于文档(db_test/person)
需要注意的是下面的id是文档的ID,不是elasticsearch生成的_id,删除文档需要用_id
{ "id": "0959ab1c-47bf-4417-904c-e5bc774ce730", "name": "王军华", "age": 265, "sex": true, "birthday": "2015-04-07T18:11:35.2655732+08:00", "intro":"介绍" }
注意:must和should并列,should无效
查询所有索引库的数据
POST /_search
查询索引库db_test中的所有数据
说明:db_test代表所有库(db)
POST /db_test/_search
查询索引库db_test表person中的所有的数据
说明:db_test代表所有库(db),person代表文档
POST /db_test/person/_search
查询索引库db_test表person中age=3的一条数据
说明:db_test代表所有库(db),person代表文档
POST /db_test/person/_search?q=age:3
//或者这样写(DSL写法)
POST /db_test/person/_search
{
"query": {
"query_string": {
"fields": ["age"],
"query": 3
}
}
}
多字段单分词字段并且条件查询
查询索引库db_test表person中age从500到800,intro包含"研究"的数据
POST /db_test/person/_search { "query":{ "bool":{ "must":[ { "range":{ "age":{ "from":"500","to":"800" } } }, { "term":{ "intro":"研究" } }] } } }
多字段多分词字段并且分词或者条件
查询索引库db_test表person中age大于500,intro包含"研究"或者"方鸿渐"的数据
POST /db_test/person/_search { "query":{ "bool":{ "must":[ { "range":{ "age":{ "gt":"500" } } }, { "terms":{ "intro":["研究","方鸿渐"] } }] } } }
分页/排序/高亮显示
说明:size如果不写默认是10,from如果不写默认是0。取前20条数据,按照age升序,生日降序,并且北京高亮显示。
POST /db_test/person/_search { "query": { "term":{"intro":"北京" } }, "from": 0, "size": 20, "sort":{ "age" : {"order" : "asc"}, "birthday": {"order" : "desc"} } , "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "intro" : {} } } }
还可以这样写
GET /_search?size=5 GET /_search?size=5&from=5 GET /_search?size=5&from=10
单字段多分词或者关系查询
说明:查询intro包含"研究"或者"方鸿渐"。
(这在全文检索时非常有用)
第一种写法:

第二种写法:

第三种写法:

单字段多分词并且关系查询
intro包含"研究"和"方鸿渐"的写法如下:

多字段多分词 字段或者分词并且关系查询
说明:查询 title中包含"朋友"并且包含"吃饭" 或者 content中包含"朋友"并且包含"吃饭" title和content中只要有一个中包含"朋友"并且"吃饭"两个关键字就行
复杂条件的范例-----------------------
第一种写法

第二种写法

多字段多分词 字段或者分词或者关系查询
查询 title中包含"朋友"或者包含"吃饭" 或者 content中包含"朋友"或者包含"吃饭" 也就是说只有title和content中包含"朋友"或者"吃饭"两个关键字就行
第一种写法:

第二种写法:

第三种写法:(对于全文检索比较简单:比如招聘网站,搜索公司名或者职位名包含关键字asp.net 或者mvc这两个关键字任意一个)

查询部分字段数据
说明:只查询age和intro
POST /db_test/person/_search?_source=age,intro
同时查询多个不同的文档
插入测试数据

测试查询
GET /_mget { "docs" : [ { "_index" : "db_news", "_type" : "news", "_id" : 1 }, { "_index" : "db_test", "_type" : "person", "_id" : "5bd94e88-10cb-4e9f-9ba6-df8ff8b59081" } ] }
查询包含一组id的文档集合
GET /db_news/news/_mget { "ids" : [ "2", "1" ] }
批量操作
说明:可以批量添加数据,详情:http://es.xiaoleilu.com/030_Data/55_Bulk.html
POST /_bulk { "create": { "_index": "db_news", "_type": "news", "_id": "3" }} { "title" : "john@smith.com", "content" : "John Smith" } { "create": { "_index": "db_news", "_type": "news", "_id": "4" }} { "title" : "john@smdsith.com", "content" : "John Smidth" }
ElasticSearch系列学习