Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

26.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'
		

26.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
			
			

26.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
			
			

26.3.3. Query DSL

26.3.3.1. match

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

26.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"}
	   }
	]
	
}
'
				

26.3.3.3. sort

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

26.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"}
	}
}
'