ElasticSearch查询过滤

请求体查询

由json格式参数进行查询
Http不允许get请求中携带交互数据。可用POST携带参数

结构化查询

结构化查询要使用query参数。

示例:
1.匹配所有

GET /_search
{
    "query": {
     "match_all": {}
    }
}

2.work字段查找包含elasticsearch的记录

GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}

3.复合子句(通过bool合并其他查询子句,条件为must,must_not,should)
示例:查询的是邮件正文中含有“business opportunity”字样的星标邮件或收件箱中正文中含有“business opportunity”字样的非垃圾邮件。

{
"bool": {
    "must": { "match": { "email": "business opportunity" }},
    "should": [
        { "match": { "starred": true }},
        { "bool": {
            "must": { "folder": "inbox" },
            "must_not": { "spam": true }}
            }
        }
    ],
    "minimum_should_match": 1
    }
}

ElasticSearch查询与过滤

查询与过滤很相似但目的不同。过滤会查找es中的文档字段值是否包含特定值,二查询会查找文档中每个字段值跟特定值的匹配程度用相关性评分(_score)表示。

过滤关键词

terms , term
term用于精确匹配,terms允许多个条件

{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}
{
    "terms": {
        "tag": [ "search", "full_text", "nosql" ]
    }
}

range
range 指定范围过滤

{
    "range": {
        "age": {
        "gte": 20,
        "lt": 30
        }
    }
}

范围操作符包含:

gt :: 大于
gte :: 大于等于
lt :: 小于
lte :: 小于等于

exists和missing

这两个关键词都用于查找文档中存在某个字段或者不存在某个字段。

扫描二维码关注公众号,回复: 8712764 查看本文章
{
    "exists": {
        "field": "title"
    }
}

match 和 match_all multi_match

match_all可以查询所有文档。

{
    "match_all": {}
}

match可以精准查询以及全文查询

{
    "match": {
       "tweet": "About Search"
    }
}

multi_match

{
    "multi_match": {
      "query": "full text search",
      "fields": [ "title", "body" ]
    }
}

查询过滤

对于需要查询同时过滤的情况,即复合查询。
加入使用filtered。
如下:

{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "folder": "inbox"
                }
            },
            "query": {
                "match": {
                    "email": "business opportunity"
                }
            }
        }
    }
}

示例:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": {
                        "term": {
                            "folder": "inbox"
                        }
                    },
                    "must_not": {
                        "query": {<1>
                            "match": {
                                "email": "urgent business proposal"
                            }
                        }
                    }
                }
            }
        }
    }
}

<1>的位置,说明过滤语句中也可以包含查询语句代替bool。
但是很少用到的过滤语句中包含查询,保留这种用法只是为了语法的完整性。 只
有在过滤中用到全文本匹配的时候才会使用这种结构

validate关键词

使用validate可以检测自己写的查询语句是否有误。

{
    "query": {
        "tweet": {
            "match": "really powerful"
        }
    }
}

非法提示:

{
    "_shards": {
        "failed": 0,
        "successful": 1,
        "total": 1
    },
    "valid": false
}

explain关键词

explain可以查询索引的具体信息包括其使用的分析器和映射关系
GET /_validate/query?explain

{
    "query": {
        "match": {
            "tweet": "really powerful"
        }
    }
}

结果:

{
    "_shards": "",
    "explanations": [
        {
            "explanation": "tweet:really tweet:powerful",
            "index": "us",
            "valid": true
        },
        {
            "explanation": "tweet:really tweet:power",
            "index": "gb",
            "valid": true
        }
    ],
    "valid": true
}
发布了79 篇原创文章 · 获赞 3 · 访问量 5257

猜你喜欢

转载自blog.csdn.net/SW_LCC/article/details/102877925