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的数据,不取出任何基础数据
curl -X GET 127.0.0.1:9200/articles/article/_search?_source=title,user_id
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条
# 文章标题匹配 "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
全文检索
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 编程"
}
}
}'
短语查询
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"
}
}
}'
精确查找
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" : "确实如此"
}
}
}
}'
<em>
标识, 设置了html斜体标签# 标题匹配"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的区别
要求 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" }}
]
}'
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"
}
]
}
}'
原文:https://www.cnblogs.com/oklizz/p/11448300.html