查询表达式
查询表达式(Query DSL)是一种非常灵活又富有表现力的 查询语言。 Elasticsearch 使用它可以以简单的 JSON 接口来展现 Lucene 功能的绝大部分。
match
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
{
"query": {
"match": {
"tweet": "elasticsearch"
}
}
}
match_phrase
slop 参数告诉 match_phrase 查询词条相隔多远时仍然能将文档视为匹配
{
"query": {
"match_phrase": {
"content" : {
"query" : "quick fox",
"slop" : 1
}
}
}
}
multi_match
{
"multi_match": {
"query": "Quick brown fox",
"type": "best_fields", # best_fields 类型是默认值
"fields": [ "title", "body" ],
"tie_breaker": 0.3,
"minimum_should_match": "30%" # 如 minimum_should_match 或 operator 这样的参数会被传递到生成的 match 查询中。
}
}
# 使用 ^ 字符语法为单个字段提升权重
# chapter_title 这个字段的 boost 值为 2 ,而其他两个字段 book_title 和 section_title 字段的默认 boost 值为 1
{
"multi_match": {
"query": "Quick brown fox",
"fields": [ "*_title", "chapter_title^2" ]
}
}
合并查询语句
- 叶子语句(Leaf clauses);match语句。
- 复合语句 (Compound);bool语句。
{
"bool": {
"must": { "match": { "tweet": "elasticsearch" }},
"must_not": { "match": { "name": "mary" }},
"should": { "match": { "tweet": "full text" }},
"filter": { "range": { "age" : { "gt" : 30 }} }
}
}
# 以下查询是为了找出信件正文包含 business opportunity 的星标邮件,或者在收件箱正文包含 business opportunity 的非垃圾邮件:
{
"bool": {
"must": { "match": { "email": "business opportunity" }},
"should": [
{ "match": { "starred": true }},
{ "bool": {
"must": { "match": { "folder": "inbox" }},
"must_not": { "match": { "spam": true }}
}}
],
"minimum_should_match": 1
}
}
匹配评分
# 完全匹配的文档占的评分比较高,则需要使用best_fields
{
"query": {
"multi_match": {
"query": "query text string",
"type": "best_fields",
"fields": [
"tag",
"content"
],
"tie_breaker": 0.3
}
}
}
# 希望越多字段匹配的文档评分越高,就要使用most_fields
{
"query": {
"multi_match": {
"query": "query text string",
"type": "most_fields",
"fields": [
"tag",
"content"
]
}
}
}
# 希望这个词条的分词词汇是分配到不同字段中的,那么就使用cross_fields
{
"query": {
"multi_match": {
"query": "query text string",
"type": "cross_fields",
"fields": [
"tag",
"content"
]
}
}
}