ElasticSearch系列学习

ElasticSearch第一步-环境配置

ElasticSearch第二步-CRUD之Sense

ElasticSearch第三步-中文分词

ElasticSearch第四步-查询详解

ElasticSearch第五步-.net平台下c#操作ElasticSearch详解

注意:以下命令都是使用sense测试(ElasticSearch第二步-CRUD之Sense),且数据都已经使用过IK分词。

以下测试数据来源于文档(db_test/person)

需要注意的是下面的id是文档的ID,不是elasticsearch生成的_id,删除文档需要用_id

{
"id": "0959ab1c-47bf-4417-904c-e5bc774ce730",
"name": "王军华",
"age": ,
"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":"","to":"" }
}
},
{
"term":{
"intro":"研究"
}
}]
}
}
}

多字段多分词字段并且分词或者条件

查询索引库db_test表person中age大于500,intro包含"研究"或者"方鸿渐"的数据

POST /db_test/person/_search
{
"query":{
"bool":{
"must":[
{
"range":{
"age":{
"gt":""
}
}
},
{
"terms":{
"intro":["研究","方鸿渐"]
}
}]
}
}
}

分页/排序/高亮显示

说明:size如果不写默认是10,from如果不写默认是0。取前20条数据,按照age升序,生日降序,并且北京高亮显示。

POST /db_test/person/_search
{
"query": {
"term":{"intro":"北京"
}
},
"from": ,
"size": ,
"sort":{
"age" : {"order" : "asc"},
"birthday": {"order" : "desc"} } ,
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"intro" : {}
}
}
}

还可以这样写

GET /_search?size=
GET /_search?size=&from=
GET /_search?size=&from=

 

单字段多分词或者关系查询

说明:查询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/
{
"title":"我是中国人",
"content":"我爱北京和天安门"
}
PUT /db_news/news/
{
"title":"我是中国人",
"content":"朋友在一起就是亲家"
}
PUT /db_news/news/
{
"title":"亲家的朋友",
"content":"我们明天去玩吧"
}
PUT /db_news/news/
{
"title":"朋友在一起吃饭",
"content":"我们明天去玩吧"
}
PUT /db_news/news/
{
"title":"朋友在一起吃饭",
"content":"亲家的朋友在一起吃饭"
}
PUT /db_news/news/
{
"title":"在阿萨德",
"content":"亲家的在一起"
}
PUT /db_news/news/
{
"title":"在一起",
"content":"按时到岗"
}
PUT /db_news/news/
{
"title":"在一起吃饭",
"content":"朋友啊吃饭"
}
PUT /db_news/news/
{
"title":"在一起吃饭",
"content":"朋友啊吃饭"
}
POST /db_news/news/_search
{
"query":{
"bool":{
"should":[
{
"bool": {
"must":[
{ "term":{"title":"朋友"} },
{
"term":{"title":"吃饭"}
}
]
}
}
],
"should":[
{
"bool": {
"must":[
{ "term":{"content":"朋友"} },
{
"term":{"content":"吃饭"}
}
]
}
}
]
}
}
}

第二种写法

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和intro

POST /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/
{
"title":"第一条新闻",
"content":"我爱北京天安门"
}

测试查询

GET /_mget
{
"docs" : [
{
"_index" : "db_news",
"_type" : "news",
"_id" :
},
{
"_index" : "db_test",
"_type" : "person",
"_id" : "5bd94e88-10cb-4e9f-9ba6-df8ff8b59081"
}
]
}

查询包含一组id的文档集合

GET /db_news/news/_mget
{
"ids" : [ "", "" ]
}

批量操作

说明:可以批量添加数据,详情:http://es.xiaoleilu.com/030_Data/55_Bulk.html

POST /_bulk
{ "create": { "_index": "db_news", "_type": "news", "_id": "" }}
{ "title" : "john@smith.com", "content" : "John Smith" }
{ "create": { "_index": "db_news", "_type": "news", "_id": "" }}
{ "title" : "john@smdsith.com", "content" : "John Smidth" }

ElasticSearch系列学习

ElasticSearch第一步-环境配置

ElasticSearch第二步-CRUD之Sense

ElasticSearch第三步-中文分词

ElasticSearch第四步-查询详解

ElasticSearch第五步-.net平台下c#操作ElasticSearch详解

ElasticSearch第四步-查询详解的更多相关文章

  1. elasticsearch系列四:搜索详解(搜索API、Query DSL)

    一.搜索API 1. 搜索API 端点地址 从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy 从索引tweet,user里 ...

  2. Lucene系列六:Lucene搜索详解(Lucene搜索流程详解、搜索核心API详解、基本查询详解、QueryParser详解)

    一.搜索流程详解 1. 先看一下Lucene的架构图 由图可知搜索的过程如下: 用户输入搜索的关键字.对关键字进行分词.根据分词结果去索引库里面找到对应的文章id.根据文章id找到对应的文章 2. L ...

  3. Solr安装入门、查询详解

    Solr安装入门:http://www.importnew.com/12607.html 查询详解:http://www.360doc.com/content/14/0306/18/203871_35 ...

  4. Centos7 配置网络步奏详解

    Centos7 配置网络步奏详解 编辑网卡配置文件 vi /etc/sysconfig/network-script/ifcfg-ens01 备注:这里的ens01不是所有系统都叫这个,有的可能叫其他 ...

  5. ThinkPHP视图查询详解

    ThinkPHP视图查询详解 参考http://www.jb51.net/article/51674.htm   这篇文章主要介绍了ThinkPHP视图查询,需要的朋友可以参考下     ThinkP ...

  6. J2EE进阶(四)Spring配置文件详解

    J2EE进阶(四)Spring配置文件详解 前言 Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程 ...

  7. elasticSearch+spring 整合 maven依赖详解

    摘自:http://www.mayou18.com/detail/nTxPQSyu.html [Elasticsearch基础]elasticSearch+spring 整合 maven依赖详解 Ma ...

  8. MySQL简单查询详解-单表查询

    MySQL简单查询详解-单表查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询的执行路径 一条SQL查询语句的执行过程大致如下图所示: 1>.客户端和服务端通过my ...

  9. [转]Linux服务器上11种网络连接状态 和 TCP三次握手/四次挥手详解

    一.Linux服务器上11种网络连接状态: 图:TCP的状态机 通常情况下:一个正常的TCP连接,都会有三个阶段:1.TCP三次握手;2.数据传送;3.TCP四次挥手. 注:以下说明最好能结合”图:T ...

随机推荐

  1. 基于傅里叶变换和PyQt4开发一个简单的频率计数器

    小学期的<信号与系统>课,要求写一个频率计数器,下面是我个人理解的频率计数 傅里叶变换的代码: # coding=utf-8 import numpy as np from scipy.i ...

  2. Scala快速概览

    IDEA工具安装及scala基本操作 目录 一. 1. 2. 3. 4. 二. 1. 2. 3. 三. 1. 2. 3. 4. 5. 6. 7. 四. 1. (1) (2) (3) (4) (5) ( ...

  3. java动态代理的2种实现方式

    java的动态代理在接java的api上有说明,这里就不写了.我理解的代理: 对特定接口中特定方法的功能进行扩展,这就是代理.代理是通过代理实例关联的调用处理程序对象调用方法. 下面通过一个例子看一下 ...

  4. spring源码:Aware接口(li)

    一.spring容器中的aware接口介绍 Spring中提供了各种Aware接口,比较常见的如BeanFactoryAware,BeanNameAware,ApplicationContextAwa ...

  5. MySQL引擎、索引和优化(li)

    一.存储引擎 存储引擎,MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术 ...

  6. Java Business Process Management(业务流程管理) 初识环境搭建

    一.简介 (一)什么是jbpm JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易 ...

  7. springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】

    项目结构:   1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  8. python语言中的编码问题(续)

    上文提到了python开发中非常重要的两处设置. 一个是编解码器的默认设置defaultencoding >>> import sys >>> sys.getdef ...

  9. canvas 制作flappy bird(像素小鸟)全流程

    flappy bird制作全流程: 一.前言 像素小鸟这个简单的游戏于2014年在网络上爆红,游戏上线一段时间内appleStore上的下载量一度达到5000万次,风靡一时, 近年来移动web的普及为 ...

  10. SharePoint2016如何使用策略进行文档归档

    前言 最近项目用户需要提供文档按照日期或标题关键字进行对应的文档归档操作,为了实施这个操作,需要准备2个文档库,我这里准备了如下文档库: 1. 测试文档库:在测试文档中上传几篇文档,如下图: 2. 我 ...