ElasticSearch实战使用
2016-05-30 14:31
141 查看
注意:以下命令都是使用sense测试(ElasticSearch第二步-CRUD之Sense),且数据都已经使用过IK分词。
以下测试数据来源于文档(db_test/person)
需要注意的是下面的id是文档的ID,不是elasticsearch生成的_id,删除文档需要用_id
注意:must和should并列,should无效
还可以这样写
(这在全文检索时非常有用)
第一种写法:
第二种写法:
第三种写法:
复杂条件的范例-----------------------
插入测试数据
第一种写法
第二种写法(参考http://www.elasticsearch.cn/guide/reference/query-dsl/query-string-query.html)
第一种写法:
第二种写法:
第三种写法:(对于全文检索比较简单:比如招聘网站,搜索公司名或者职位名包含关键字asp.net 或者mvc这两个关键字任意一个)
同时查询多个不同的文档
插入测试数据
测试查询
以下测试数据来源于文档(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包含"研究"或者"方鸿渐"。(这在全文检索时非常有用)
第一种写法:
POST /db_test/person/_search { "query":{ "bool":{ "must":[ { "query_string":{ "default_field":"intro", "query":"研究方鸿渐" } } ] } }, "sort": [ { "birthday": { "order": "desc" } } ]}
第二种写法:
POST /db_test/person/_search { "query": { "query_string": { "fields": ["intro"], "query": "研究方鸿渐" } }, "sort": [ { "birthday": { "order": "asc" } } ] }
第三种写法:
POST /db_test/person/_search { "query":{ "bool":{ "should":[ { "term":{"intro":"研究"} }, { "term":{"intro":"方鸿渐"} } ] } }, "sort": [ { "birthday": { "order": "desc" } } ] }
单字段多分词并且关系查询
intro包含"研究"和"方鸿渐"的写法如下:POST /db_test/person/_search { "query":{ "bool":{ "must":[ { "term":{"intro":"研究"} }, { "term":{"intro":"方鸿渐"} } ] } } }
多字段多分词 字段或者分词并且关系查询
说明:查询 title中包含"朋友"并且包含"吃饭" 或者 content中包含"朋友"并且包含"吃饭" title和content中只要有一个中包含"朋友"并且"吃饭"两个关键字就行复杂条件的范例-----------------------
插入测试数据
PUT /db_news/news/1 { "title":"我是中国人", "content":"我爱北京和天安门" } PUT /db_news/news/2 { "title":"我是中国人", "content":"朋友在一起就是亲家" } PUT /db_news/news/3 { "title":"亲家的朋友", "content":"我们明天去玩吧" } PUT /db_news/news/4 { "title":"朋友在一起吃饭", "content":"我们明天去玩吧" } PUT /db_news/news/5 { "title":"朋友在一起吃饭", "content":"亲家的朋友在一起吃饭" } PUT /db_news/news/6 { "title":"在阿萨德", "content":"亲家的在一起" } PUT /db_news/news/7 { "title":"在一起", "content":"按时到岗" } PUT /db_news/news/8 { "title":"在一起吃饭", "content":"朋友啊吃饭" } PUT /db_news/news/9 { "title":"在一起吃饭", "content":"朋友啊吃饭" }
第一种写法
POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "bool": { "must":[ { "term":{"title":"朋友"} }, { "term":{"title":"吃饭"} } ] } } ], "should":[ { "bool": { "must":[ { "term":{"content":"朋友"} }, { "term":{"content":"吃饭"} } ] } } ] } } }
第二种写法(参考http://www.elasticsearch.cn/guide/reference/query-dsl/query-string-query.html)
POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "query_string":{ "default_field":"title", "query":"朋友吃饭", "default_operator":"AND", "analyzer":"ik" } }, { "query_string":{ "default_field":"content", "query":"朋友吃饭", "default_operator":"AND", "analyzer":"ik" } } ] } } }
多字段多分词 字段或者分词或者关系查询
查询 title中包含"朋友"或者包含"吃饭" 或者 content中包含"朋友"或者包含"吃饭" 也就是说只有title和content中包含"朋友"或者"吃饭"两个关键字就行第一种写法:
POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "term":{"title":"朋友"} }, { "term":{"title":"吃饭"} }, { "term":{"content":"朋友"} }, { "term":{"content":"吃饭"} } ] } } }
第二种写法:
POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "terms":{"title":["朋友","吃饭"]} }, { "terms":{"content":["朋友","吃饭"]} } ] } } }
第三种写法:(对于全文检索比较简单:比如招聘网站,搜索公司名或者职位名包含关键字asp.net 或者mvc这两个关键字任意一个)
POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "query_string":{ "default_field":"title", "query":"朋友吃饭" } }, { "query_string":{ "default_field":"content", "query":"朋友吃饭" } } ] } } }
查询部分字段数据
说明:只查询age和introPOST /db_test/person/_search?_source=age,intro
同时查询多个不同的文档
插入测试数据
PUT /db_news { "settings" : { "analysis" : { "analyzer" : { "stem" : { "tokenizer" : "standard", "filter" : ["standard", "lowercase", "stop", "porter_stem"] } } } }, "mappings" : { "news" : { "dynamic" : true, "properties" : { "title" : { "type" : "string", "indexAnalyzer" : "ik", "searchAnalyzer":"ik" }, "content" : { "type" : "string", "indexAnalyzer" : "ik", "searchAnalyzer":"ik" } } } } } PUT /db_news/news/1 { "title":"第一条新闻", "content":"我爱北京天安门" }
测试查询
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.htmlPOST /_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" }
相关文章推荐
- iOS 技术提高方法
- Android 读取OTG中配置文件完成自动测试
- Android Studio使用SVN问题解决方案
- [C/C++11]_[初级]_[实用时间库chrono]
- Hibernate中的Entity类之间的OneToOne关联
- java设计模式:单例模式
- NSURLSession 同步请求(使用信号量)
- 242. Valid Anagram
- [C/C++11]_[初级]_[实用时间库chrono]
- 基于webpack使用ES6新特性(转载)
- js取整、取余等常用方法
- WebView内存泄漏记录
- 栈、队列和链表
- Android常用控件之ListView(二)
- 用setAnimationStyle来设置popwindow显示消失的动画效果
- Bootstrap+jfinal退出系统弹出确认框的实现方法
- return和finally的执行和联系
- iOS CGColorGetComponents
- nginx 平滑重启
- 在SublimeText上搭建ReactJS开发环境(转载)