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"} } } '