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"} } } '
curl -XPOST http://test:123456@so.netkiller.cn/information/article/_search -d' { "query" : { "match" : { "content" : "股市" }}, "highlight" : { "pre_tags" : ["<strong>", "<b>"], "post_tags" : ["</strong >", "</b>"], "fields" : { "content" : {} } } } '
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。