Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

23.3. 搜索

搜索所有内容

# curl -XGET 'http://localhost:9200/_search?pretty'	
# curl -XGET 'http://localhost:9200/_all/_search?pretty'			
		

指定 _index 搜索

# curl -XGET 'http://localhost:9200/website/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news/_search?pretty'
		

指定 _type 搜索

# curl -XGET 'http://localhost:9200/website,twitter/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news,blog/_search?pretty'
# curl -XGET 'http://localhost:9200/website,twitter/news,blog/_search?pretty'
		

所有 _index 包含指定 _type 搜索

# curl -XGET 'http://localhost:9200/_all/news,blog/_search?pretty'
		

23.3.1. 分页

该功能与SQL的LIMIT关键字结果一样,Elasticsearch接受size和from两个参数参数:

size: 返回结果集数量,默认10,用法与SQL中的 Limit相同

from: 偏移量,默认0,用法与 SQL中的 Offset相同

如果你想每页显示10个结果,那么请求如下:

			
第一页 GET /_search?size=10
第二页 GET /_search?size=10&from=10
第三页 GET /_search?size=10&from=20
			
			

23.3.2. 字符串搜索

			
# curl -XGET 'http://localhost:9200/_all/_search?q=neo&pretty'
						
			

同时满足两个条件

+name:neo +age:30
			

查找name为mary 或者 john的数据

+name:(mary john)
			

查询姓名是neo或者jam并且年龄小于30岁同时1980-09-10之后出生的

			
+name:(neo jam) +age:<30 +date:>1980-09-10
			
			

23.3.3. Query DSL

23.3.3.1. match

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query" : {
		"match" : {
			"tag" : "美"   
		}
	}
}
'
				

23.3.3.2. multi_match

multi_match 实现多字段查询

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query": {
	    "multi_match": {
		    "query":      "国际",
		    "type":       "cross_fields",
		    "fields":     [ "title", "content" ],
		    "operator":   "and"
	    }
	},
	"from": 0,
	"size": 20,
	"_source":["id","title","ctime"],
	"sort": [
	   {
	      "ctime": {"order": "desc"}
	   }
	]
	
}
'
				

23.3.3.3. sort

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query" : {
		"match" : {"tag" : "美"}
	},
	"sort": {
		"ctime": {"order": "desc", "mode":  "min"}
	}
}
	'			

23.3.3.4. _source

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"query" : {
		"match" : {
			"tag" : "美"   
		}
	},
	"_source":["id","title","ctime"]
}
'

curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
	"_source":["id","title","ctime"],
	"query" : {
		"match" : {"tag" : "美"}
	},
	"sort": {
		"ctime": {"order": "desc", "mode":  "min"}
	}
}
'	
				
comments powered by Disqus