基本查询
-
两种查询方法
- 查询条件设置在查询字符串中
- 查询条件设置在请求体中 GET也可以设置
-
根据文档ID
curl -X GET 127.0.0.1:9200/articles/article/1 # 查询文档id=1d的数据
curl -X GET 127.0.0.1:9200/articles/article/1?_source=title,user_id # 查询文档id=1d的数据, 只取出标题和用户id
curl -X GET 127.0.0.1:9200/articles/article/1?_source=false # 查询文档id=1d的数据,不取出任何基础数据
- 查询所有
- 默认分页10条
curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id
- 分页查询
- from 起始
- size 每页数量
curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id&size=3 # 每页3条
curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id&size=3&from=10 # 从第10条开始取, 取3条
- 全文检索
- %20表示空格
# 文章标题匹配 "python web"
curl -X GET 127.0.0.1:9200/articles/article/_search?q=title:python%20web&_source=title,article_id&pretty # 查询内容匹配"python web"的数据
# 文章标题和内容匹配 "python web"
curl -X GET 127.0.0.1:9200/articles/article/_search?q=title:python%20web,content:python%20web&_source=title,article_id&pretty
# 所有字段匹配"python web"
curl -X GET 127.0.0.1:9200/articles/article/_search?q=_all:python%20web&_source=title,article_id&pretty
高级查询
全文检索
- 根据分词后的结果进行查询, 按照得分排序
- match
curl -X GET 127.0.0.1:9200/articles/article/_search -d'
{
"query" : {
"match" : { # 表示全文检索
"title" : "python web" # 指定检索的字段
}
}
}'
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
{
"from": 0, # 指定分页
"size": 5,
"_source": ["article_id","title"], # 指定返回的数据
"query" : {
"match" : {
"title" : "python web"
}
}
}'
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
{
"from": 0,
"size": 5,
"_source": ["article_id","title"],
"query" : {
"match" : {
"_all" : "python web 编程"
}
}
}'
短语查询
- 要求包含所有的词条, 不需要直接相连, 相对位置不能改变
- match_phrase
curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '{
"article_id": 150000,
"title": "python is good",
"status": 2
}'
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
{
"from": 0,
"size": 5,
"_source": ["article_id","title"],
"query" : {
"match_phrase" : {
"title" : "python good"
}
}
}'
精确查找
- 不会分词, 必须能够匹配到词条(索引库中必须有该词条)
- term
curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '
{
"article_id": 150000,
"user_id": 1,
"title": "确实如此", # 确实 实如 如此
"content": "python is good",
"status": 2,
"create_time": "2019-04-03"
}'
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
{
"size": 5,
"_source": ["title"],
"query" : {
"term" : {
"title" : "确实如此"
}
}
}
}'
- 范围查找 range
- gt greater than 大于
- gte greater than equel 大于等于
- lt 小于
- lte 小于等于
- 高亮显示
- highlight
- 结果中对指定的字段匹配到的位置进行
<em>
标识, 设置了html斜体标签
- 组合查询 bool
- 逻辑运算
- must
- must_not
- should or / 如果和must配合使用, 则只会变为加分项,并不强制要求
- filter 直接过滤掉数据, 不进行评分
# 标题匹配"python web" 并且 内容匹配"python c"
curl -X GET 127.0.0.1:9200/articles/article/search?pretty -d '
{
"source": ["title", "user_id"],
"query": {
"bool": {
"must": [
{"match": {"title": "python web"}},
{"match": {"content": "python c"}}]
}
}
}
'
# 标题匹配"python web" 或者 内容匹配"python c"
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d '
{
"_source": ["title", "user_id"],
"query": {
"bool": {
"should": [{"match": {"title": "python web"}}, {"match": {"content": "python c"}}],
}
}
}
'
# (标题匹配"python web" 并且 内容匹配"python c") 并且 (状态 匹配2 或者 user_id 匹配1) -> (A and B) and (C or D)
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d '
{
"_source": ["title", "user_id"],
"query": {
"bool": {
"filter": {
"bool": {
"must": [{"match": {"title": "python web"}}, {"match": {"content": "python"}}],
"should": [
{"match": {"status": 2}}, {"match": {"user_id": 1}}
]
}
}
}
}
}
'
filter和query的区别
- query匹配完会进行排序
- filter只判断是否满足要求, 不进行排序, 而且对于不满足要求的结果会进行缓存
要求 status=2 并且 title 匹配 “python web”
# 只使用query
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d '
{
"_source": ["title", "user_id"],
"query": {
"bool": {
"must": [
{"term":
{"status": 2}
},
{"match":
{"title": "python web"}
}
]
}
}
}
'
# 先使用filter过滤, 再使用query排序
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d '
{
"_source": ["title", "user_id"],
"query": {
"bool": {
"must": [
{"match":
{"title": "python web"}
}
],
"filter": {
"term": {"status": 2}
}
}
}
}
'
- 排序
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
{
"size": 5,
"_source": ["article_id","title"],
"query" : {
"match" : {
"_all" : "python web"
}
},
"sort": [
{ "create_time": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}'
- 提升权重boost
curl -X GET 127.0.0.1:9200/articles/article/_search?pretty -d'
{
"size": 5,
"_source": ["article_id","title"],
"query" : {
"must": [
"match" : {
"title" : {
"query": "python web",
"boost": 4
}
},
"match": {
"content": "python web"
}
]
}
}'