# 复杂条件搜索
GET tindex/_doc/_search
{
"query":{
"match":{
"name": "wells"
}
}
}
# 指定输出字段
GET /tindex/_doc/_search
{
"query": {
"match":{
"name": "wells"
}
},
"_source": ["name", "age"]
}
# 排序
GET /tindex/_doc/_search
{
"query": {
"match": {
"name": "wells"
}
},
"_source": ["name", "age"],
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
# 分页查询
GET /tindex/_doc/_search
{
"query": {
"match": {
"name": "wells"
}
},
"from": 0,
"size": 1
}
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wells"
}
},
{
"match": {
"age": 17
}
}
]
}
}
}
# bool: should 查询 where name=wells or age = 17
GET /tindex/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "wells"
}
},
{
"match": {
"age": 17
}
}
]
}
}
}
# bool: must_not 查询 where name != wells
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "wells"
}
},
{
"match": {
"name": "tom"
}
}
]
}
}
}
# 或者 bool: must_not 查询 where name != wells
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
# 通过空格隔开,设置多个值
"name": "wells tom"
}
}
]
}
}
}
# filter
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wells"
}
}
],
"filter": {
"range": {
"age": {
"lt": 30,
"gt": 18
}
}
}
}
}
}
# 匹配多个值
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "man 技术"
}
}
}
两个类型:
通过 _analyze 对词进行分析查看,text与keyword都是fields的类型,通过类型可以区分是否精确查询
# text 与 keyword
GET _analyze
{
"analyzer": "standard",
"text": "wells学es"
}
# text 与 keyword
GET _analyze
{
"analyzer": "keyword",
"text": "wells学es"
}
# term 与 match
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "技术宅男"
}
}
}
# term 与 match
GET /tindex/_doc/_search
{
"query": {
"term": {
"tags": "技术宅男"
}
}
}
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "技术"
}
},
"highlight":{
"fields":{
"name":{}
}
}
}
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "技术"
}
},
"highlight":{
"pre_tags": ""
,
"post_tags": "",
"fields":{
"name":{}
}
}
}