POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"title" : "this is java and elasticsearch blog"} }
{ "update": { "_id": "2"} }
{ "doc" : {"title" : "this is java blog"} }
{ "update": { "_id": "3"} }
{ "doc" : {"title" : "this is elasticsearch blog"} }
{ "update": { "_id": "4"} }
{ "doc" : {"title" : "this is java, elasticsearch, hadoop blog"} }
GET /forum/article/_search
{"query": {"match_all": {}}}
/java或elasticsearch
GET /forum/article/_search
{"query": {"match": {
"title": "java elasticsearch"
}}
}
/java和elasticsearch
GET /forum/article/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"title": {
"query": "java elasticsearch",
"operator": "and"
}
}
}
}
}
}
/java 和elasticsearch
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": " elasticsearch java",
"operator": "and"
}
}
}
}
/至少包含三个
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch spark hadoop",
"minimum_should_match":"75%"
}
}
}
}
/bool查询时,如果有must,should中可以不包含任何单词,但是should匹配会提高score
GET /forum/article/_search
{
"query": {
"bool": {
"must": { "match": { "title": "java" }},
"must_not": { "match": { "title": "spark" }},
"should": [
{ "match": { "title": "hadoop" }},
{ "match": { "title": "elasticsearch" }}
]
}
}
}
/精确到分词命中具体个数
GET /forum/article/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "java"
}
},
{
"match": {
"title": "elasticsearch"
}
},
{
"match": {
"title": "hadoop"
}
},
{
"match": {
"title": "spark"
}
}
],
"minimum_should_match":3
}
}
}
多个条件如何转为bool查询
1、普通match如何转换为term+should { "match": { "title": "java elasticsearch"} } 使用诸如上面的match query进行多值搜索的时候,es会在底层自动将这个match query转换为bool的语法 bool should,指定多个搜索词,同时使用term query { "bool": { "should": [ { "term": { "title": "java" }}, { "term": { "title": "elasticsearch" }} ] } } 2、and match如何转换为term+must { "match": { "title": { "query": "java elasticsearch", "operator": "and" } } } { "bool": { "must": [ { "term": { "title": "java" }}, { "term": { "title": "elasticsearch" }} ] } } 3、minimum_should_match如何转换 { "match": { "title": { "query": "java elasticsearch hadoop spark", "minimum_should_match": "75%" } } } { "bool": { "should": [ { "term": { "title": "java" }}, { "term": { "title": "elasticsearch" }}, { "term": { "title": "hadoop" }}, { "term": { "title": "spark" }} ], "minimum_should_match": 3 } }
boost,可以将某个搜索条件的权重加大,下面的搜索可以看出elasticsearch 的score比较大。
GET /forum/article/_search { "query": { "bool": { "must": [ { "match": { "title": "blog" } } ], "should": [ { "match": { "title": { "query": "java" } } }, { "match": { "title": { "query": "hadoop" } } }, { "match": { "title": { "query": "elasticsearch", "boost":5 } } } ] } } }