| 知乎专栏 |
curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
"query" : {
"match" : {
"tag" : "美"
}
}
}
'
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"}
}
]
}
'
Elasticsearch 提供三个布尔条件
must: AND
must_not:NOT
should:OR
查询必须满足 tags=天气 and title 包含 台风关键字
curl -XPOST http://test:123456@so.netkiller.cn/information/article/_search?pretty -d'
{
"query": {
"bool": {
"must": [
{ "match": { "tags" : "天气" }},
{ "match": { "title": "台风" }}
]
}
},
"_source":["id","title","ctime"],
"highlight" : {
"pre_tags" : ["<strong>", "<b>"],
"post_tags" : ["</strong >", "</b>"],
"fields" : {
"content" : {}
}
}
}'
查询必须满足标title or author 两个条件
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "Linux" }},
{ "match": { "author": "Neo" }}
]
}
}
}
可以嵌套使用
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "War and Peace" }},
{ "match": { "author": "Leo Tolstoy" }},
{ "bool": {
"should": [
{ "match": { "translator": "Constance Garnett" }},
{ "match": { "translator": "Louise Maude" }}
]
}}
]
}
}
}
query 相当于 SQL 中的 LIKE 匹配, filter 更像是 where 条件。下面的例子查询 site_id = 23 的数据并且 tags 包含 “头条” 关键字
curl -XGET 'http://test:123456@so.netkiller.cn/information/article/_search?pretty' -d '
{
"query": {
"bool": {
"must": {
"match": {
"tags": "头条"
}
},
"filter": {
"term": {
"site_id" : "23"
}
}
}
}
}'
curl -XGET 'http://localhost:9200/information/news/_search?pretty' -d '
{
"query" : {
"match" : {"tag" : "美"}
},
"sort": {
"ctime": {"order": "desc", "mode": "min"}
}
}
'
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"}
}
}
'