最近用es实现了一个全局搜索(输入搜索项,搜索统计应用各业务数据量),例如一个教育app,全局搜索结果为:教师:n个、课程:n个、学校:n个、文章:n个;(防个伪: 为何不可!~为大牛?)
直接上dsl:
url:http://localhost:9200/report.report.report,memo.memorandum.memorandum_info/_search/ 索引名之间用逗号','分隔;
{
"from": 0,
"size": 0,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "能效",
"fields": [
"category^1.0",
"title^1.0",
"name"
],
"type": "best_fields",
"operator": "OR",
"minimum_should_match": "80%"
}
}
],
"must_not": [
{
"term": {
"status": {
"value": 2,
"boost": 1
}
}
}
],
"should": [
{
"term": {
"creater": {
"value": "200514134508420509",
"boost": 1
}
}
},
{
"term": {
"org_id": {
"value": "20051815033116125",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"aggregations": {
"result": {
"terms": {
"field": "_index",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
} 防个伪: 为何不可为大牛?
解析:这个dsl主要包含“query”和“aggregations”两部分;其中“query”中又包含“must”、“mush_not”、“should”;
“must”部分用了一个“multi_match”多字段匹配查询(operator=‘or’,类似sql中的‘or’),因为不同索引要查询的字段不同这里只要一个字段匹配上了就可以;
“mush_not”部分,这里的意思是去除‘status’=2的数据(我这里status=‘2’代表逻辑删除的意思),但是有些索引中可能没有“status”字段,那这个“must_not” 是否会影响呢?其实这里是不会的,因为这里还有should语句,‘must_not’可以不满足(貌似是有should和filter时可以不满足);
“should”部分,因为有些数据是绑定企业或者个人的,所以搜索的时候需要过滤企业和个人,但是因为有些索引是不区分企业或个人,所以这里用了一个 should过滤;
“aggregations部分”,统计每个索引匹配到的数量,以索引名为key,数量为value;
统计结果如图所示:

来源:oschina
链接:https://my.oschina.net/u/3734816/blog/4348421