ES DSL搜索 - multi_match、boost和布尔查询

1 介绍

主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticsearch 部署IP地址+端口号(例如 http://192.168.51.4:9200 。

统一请求地址:

POST /search_demo/_doc/_search

2 multi_match

multi_match 满足使用 match 在多个字段中进行查询的需求

传递JSON数据

{
    "query": {
        "multi_match": {
            "query": "组合",
            "fields": [
                "desc","nickname"
            ]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.2874916,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 2.2874916,
                "_source": {
                    "nickname": "老男孩",
                    "id": 1007,
                    "desc": "确实是个很好的组合,筷子 筷子"
                }
            }
        ]
    }
}

3 boost

boost 权重,为某个字段设置权重,权重越高,文档相关性得分就越高。通常来说,搜索商品名称要比商品简介的权重要高。

传递JSON数据

{
    "query": {
        "multi_match": {
            "query": "好的",
            "fields": [
                "desc","nickname*10"
            ]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 3.1980762,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 3.1980762,
                "_source": {
                    "nickname": "红帽子",
                    "id": 1004,
                    "desc": "好的系统必须拥有稳定的系统结构"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 3.0979095,
                "_source": {
                    "nickname": "switch游戏机",
                    "id": 1005,
                    "desc": "好的游戏,才会有人购买,比如塞尔达"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.37556386,
                "_source": {
                    "nickname": "涡轮增压",
                    "id": 1003,
                    "desc": "极限的速度是需要涡轮增压的"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1012",
                "_score": 0.36424035,
                "_source": {
                    "nickname": "youzi",
                    "id": 1012,
                    "desc": "永远的神"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.35254776,
                "_source": {
                    "nickname": "进击的巨人",
                    "id": 1002,
                    "desc": "艾伦是会变成真正的巨人的"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 0.27665582,
                "_source": {
                    "nickname": "皮特",
                    "id": 1011,
                    "desc": "皮特的姓氏好像是彼得"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 0.2639615,
                "_source": {
                    "nickname": "老男孩",
                    "id": 1007,
                    "desc": "确实是个很好的组合,筷子 筷子"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1009",
                "_score": 0.252381,
                "_source": {
                    "nickname": "露西",
                    "id": 1009,
                    "desc": "露西是一只很聪明的cat"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.18093815,
                "_source": {
                    "nickname": "飞翔的荷兰号",
                    "id": 1001,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识"
                }
            }
        ]
    }
}

nickname^10 代表搜索提升10倍的相关性,也就是说用户搜索的时候,其实以这个nickname为主,desc为辅,nickname的匹配相关度高只要提升权重比例就可以。

4 布尔查询

可以实现多重组合查询

  • must:查询必须匹配搜索条件,譬如 and
  • should:查询匹配满足一个条件,譬如 or
  • must_not:不匹配搜索条件,一个都不满足,譬如 not in

单个查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "好的",
                        "fields": ["desc","nickname"]
                    }
                },
                {
                    "term": {
                        "sex": 0
                    }
                },
                {
                    "term": {
                        "birthday": "1992-12-24"
                    }
                }
            
            ]
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc"
        ]
}

组合查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "desc": "好的"
                    }
                },
                {
                    "match": {
                        "nickname": "好的"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "sex": 1
                    }
                }
            ],
            "must_not":[
                {
                    "term": {
                        "birthday": "1993-01-24"
                    }
                }
            ]
            
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc",
            "birthday"
        ]
}

为指定词语加权

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "desc": {
                            "query": "好的",
                            "boost": 2
                        }
                    }
                },
                {
                    "match": {
                        "desc": {
                            "query": "男孩",
                             "boost": 20
                        }
                    }
                }
            ]
            
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc",
            "birthday"
        ]
}

5 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢

你可能感兴趣的:(ES DSL搜索 - multi_match、boost和布尔查询)