您的位置:首页 > 其它

ElasticSearch39:初识搜索引擎_上机动手实战多搜索条件组合查询

2018-01-04 11:38 726 查看
1.组合查询query

bool

 在bool中可以放must,must_not,should,filter

 例子:
GET /website/article/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "elasticsearch"
}}
],
"should": [
{"match": {
"title": "good"
}}
],
"must_not": [
{"match": {
"content": "bad"
}}
],
"filter": {
"range": {
"author_id": {
"gte": 100
}
}
}
}
}
}


执行结果:
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25316024,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 0.25316024,
"_source": {
"title": "my elasticsearch article1",
"content": "elasticsearch is good",
"author_id": 10010
}
}
]
}
}


2.我们来看一下执行结果中的_score是如何计算出来的?

每个子查询都会计算一个document针对它的相关度分数,然后bool综合所有分数,合并为一个分数,当然filter是不会计算分数的。

也可以只需要使用filter,但是必须加上constant_score,否则会报语法错误
GET /company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}


执行结果:
{
"took": 83,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"address": {
"province": "zhejiang",
"city": "wenzhou",
"country": "china"
},
"name": "tom",
"age": 31,
"join_date": "2015-01-02"
}
}
]
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch