## **query_string查詢**
例如:
```
POST my_index/_doc/_search
{
"query":{
"query_string":{
"default_field":"title",
"query":"寶馬"
}
}
}
```
說明:
* query_string查詢跟url query查詢類似,就是將查詢條件寫在json體中
* default_field 指的是默認查詢的字段
* query 查詢條件 ‘AND’ 或則 'OR'記的大寫
* fields 指定查詢字段集合,數組形式,例如:['title','author']指查詢title和author字段
## **Simple Query String Query**
* 類似Query String ,但會忽略錯誤的查詢表達式,并且僅支持部分查詢語法
* 其常用的邏輯服務如下,不能使用 AND OR NOT 等關鍵字,否則當普通字符串處理
+ 代表AND
| 代表OR
- 代表NOT
## **query關鍵字**
要搜索的body
例如:
```
GET /_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}
```
## **from / Size**
如果需要分頁,則使用from和size關鍵字。
例如:
```
#從0位置開始,獲取10條數據。即獲取:0-10的數據
GET /_search
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "user" : "kimchy" }
}
}
```
## **sort**
指定結果的排序方式,可以指定某個字段排序,也可用于 _score 按分數排序,以及 _doc 按索引順序排序
例如:
```
PUT /my_index
{
"mappings": {
"_doc": {
"properties": {
"post_date": { "type": "date" },
"user": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"age": { "type": "integer" }
}
}
}
}
GET /my_index/_search
{
"sort" : [
{ "post_date" : {"order" : "asc"}},
"user",
{ "name" : "desc" },
{ "age" : "desc" },
"_score"
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
```
## **_source**
指定結果返回的字段,默認情況下返回所有字段,也可不返回字段。
```
#不返回_source字段,即沒有文檔內容返回
POST my_index/_doc/_search
{
"_source":false,
"query":{
"query_string":{
"default_field":"title",
"query":"寶馬"
}
}
}
#返回結果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821
}
]
}
}
#返回指定字段開頭的字段
POST my_index/_doc/_search
{
"_source":"tit*",
"query":{
"query_string":{
"default_field":"title",
"query":"寶馬"
}
}
}
#返回結果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821,
"_source": {
"title": "我的寶馬x5有260馬力"
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"title": "我的寶馬有222馬力"
}
}
]
}
}
#多個時可以這樣用(返回以"tit"開頭和"na"的字段)
POST my_index/_doc/_search
{
"_source":["tit*","na*"],
"query":{
"query_string":{
"default_field":"title",
"query":"寶馬"
}
}
}
#指定不返回的字段
POST my_index/_doc/_search
{
"_source":{
"includes":["title"],
"excludes":["name"]
},
"query":{
"query_string":{
"default_field":"title",
"query":"寶馬"
}
}
}
```
## **highlight**
用于高亮搜索,將搜索出來的結果用標簽包裹起來,不指定標簽默認"`<em>`"標簽包裹
```
POST my_index/_doc/_search
{
"query":{
"match":{
"title":"寶馬"
}
},
"highlight":{
"fields":{
"title":{}
}
}
}
#返回結果
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "寶馬",
"title": "我的寶馬x5有260馬力"
},
"highlight": {
"title": [
"我的<em>寶馬</em>x5有260馬力"
]
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "張三",
"title": "我的寶馬有222馬力"
},
"highlight": {
"title": [
"我的<em>寶馬</em>有222馬力"
]
}
}
]
}
}
```
自定義標簽
```
POST my_index/_doc/_search
{
"query":{
"match":{
"title":"寶馬"
}
},
"highlight":{
"fields":{
"title":{
"pre_tags":["<mark>"],
"post_tags":["</mark>"]
}
}
}
}
#返回結果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "寶馬",
"title": "我的寶馬x5有260馬力"
},
"highlight": {
"title": [
"我的<mark>寶馬</mark>x5有260馬力"
]
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "張三",
"title": "我的寶馬有222馬力"
},
"highlight": {
"title": [
"我的<mark>寶馬</mark>有222馬力"
]
}
}
]
}
}
```
多字段高亮
```
POST my_index/_doc/_search
{
"query":{
"match":{
"title":"寶馬"
}
},
"highlight":{
"require_field_match":false,
"fields":{
"title":{
"pre_tags":["<mark>"],
"post_tags":["</mark>"]
},
"name":{}
}
}
}
#返回結果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "寶馬",
"title": "我的寶馬x5有260馬力"
},
"highlight": {
"name": [
"<em>寶馬</em>"
],
"title": [
"我的<mark>寶馬</mark>x5有260馬力"
]
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "張三",
"title": "我的寶馬有222馬力"
},
"highlight": {
"title": [
"我的<mark>寶馬</mark>有222馬力"
]
}
}
]
}
}
```
說明:默認情況下只高亮顯示被索引的字段,即:require_filed_match 默認為 true,當 require_filed_match 為 false時,未被索引的字段也可以被高亮顯示。