例如:
POST my_index/_doc/_search
{
"query":{
"query_string":{
"default_field":"title",
"query":"宝马"
}
}
}
说明:
要搜索的body
例如:
GET /_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}
如果需要分页,则使用from和size关键字。
例如:
#从0位置开始,获取10条数据。即获取:0-10的数据
GET /_search
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "user" : "kimchy" }
}
}
指定结果的排序方式,可以指定某个字段排序,也可用于 _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字段,即没有文档内容返回
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":"宝马"
}
}
}
用于高亮搜索,将搜索出来的结果用标签包裹起来,不指定标签默认"<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时,未被索引的字段也可以被高亮显示。
内容来源于网络,如有侵权,请联系作者删除!