ES DSL搜索 - term、match和match_phrase

1 介绍

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

统一请求地址:

POST /search_demo/_doc/_search

2 term与match

2.1 term

term 是精确搜索,搜索的时候会将用户的搜索内容,比如"好的"作为一整个关键词去搜索,而不会对其进行分词后再搜索。

传递JSON数据

{
    
    
    "query": {
    
    
        "term": {
    
    
            "desc": "好的"
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    
    
    "took": 10,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.8419956,
        "hits": [
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.8419956,
                "_source": {
    
    
                    "nickname": "红帽子",
                    "id": 1004,
                    "desc": "好的系统必须拥有稳定的系统结构"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.8419956,
                "_source": {
    
    
                    "nickname": "switch游戏机",
                    "id": 1005,
                    "desc": "好的游戏,才会有人购买,比如塞尔达"
                }
            }
        ]
    }
}

2.2 match基础

term 是非精确搜索,搜索的时候会将用户的搜索内容,比如"好的"作为一整个关键词去搜索,而不会对其进行分词后再搜索。

传递JSON数据

{
    
    
    "query": {
    
    
        "match": {
    
    
            "desc": "好的"
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    
    
    "took": 85,
    "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网站解决项目中遇到的问题,学习到了很多知识"
                }
            }
        ]
    }
}

使用 term 查询 好的 命中记录 2

使用 match 查询 好的 命中记录 9

2.3 match扩展

operator

  • or:搜索内容分词后,只要存在一个词语匹配就展示结果

  • and:搜索分词后,要满足所有的词语匹配

{
    
    
  "query": {
    
    
    "match": {
    
    
      "desc": "好的"
    }
  }
}
# 等同于下方
{
    
    
    "query": {
    
    
        "match": {
    
    
            "desc": {
    
    
            "query": "好的",
            "operator": "and"
            }
        }
    }
}
# 相当于 select * from search_demo where desc = 'aaa' or|and desc='bbb'
  • minimum_should_match:最低匹配精度,至少有[分词后的词语个数] * 百分比,得出一个数据值取整。举个例子,当前属性设置为 70,如果一个用户查询检索内容分词后有10词语,那么匹配度按照 10 * 70% = 7,则desc中至少需要有7个词语匹配,就展示;若分词是8个,则8 * 70% = 5.6,则desc中至少有5个词语匹配,就展示
  • minimum_should_match 也能设置具体的数字,表示个数

传递JSON数据

{
    
    
    "query": {
    
    
        "match": {
    
    
            "desc": {
    
    
                "query": "好的",
                "minimum_should_match": "30%"
            }
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    
    
    "took": 7,
    "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网站解决项目中遇到的问题,学习到了很多知识"
                }
            }
        ]
    }
}

3 terms

terms 相当于tag标签查询比如有些数据因为业务需要,build被打上分类的标签,比如京东对商品,有零食、数码、衣服等等这样的标签,可以完全匹配做类似标签的查询。

{
    
    
    "query": {
    
    
        "terms": {
    
    
            "desc": ["好","的"]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

4 match_phrase

match:分词后只要有匹配就返回

match_phrase:分词结果必须在text字段内容中都包含而且顺序必须相同,而且必须是连续的(搜索比较严格)

  • slop:允许词语间跳过的数量

传递JSON数据

{
    
    
    "query": {
    
    
        "match_phrase": {
    
    
            "desc": {
    
    
                "query": "皮特 姓氏",
                "slop": 1
            }
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    
    
    "took": 14,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 1,
            "relation": "eq"
        },
        "max_score": 4.7110896,
        "hits": [
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 4.7110896,
                "_source": {
    
    
                    "nickname": "皮特",
                    "id": 1011,
                    "desc": "皮特的姓氏好像是彼得"
                }
            }
        ]
    }
}

5 ids

5.1 查询单个

GET /search_demo/_doc/1001
{
    
    
    "_index": "search_demo",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 2,
    "found": true,
    "_source": {
    
    
        "id": 1001,
        "age": 18,
        "username": "Tic",
        "nickname": "飞翔的荷兰号",
        "money": 88.8,
        "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识",
        "sex": 0,
        "birthday": "1992-12-24",
        "face": "http://www.p2pi.cn/static/img/1001_face.png"
    }
}

5.2 查询多个

传递JSON数据

{
    
    
    "query": {
    
    
        "ids": {
    
    
            "type": "_doc",
            "values": ["1001","1005"]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

请求结果

{
    
    
    "took": 9,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
    
    
                    "nickname": "飞翔的荷兰号",
                    "id": 1001,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
    
    
                    "nickname": "switch游戏机",
                    "id": 1005,
                    "desc": "好的游戏,才会有人购买,比如塞尔达"
                }
            }
        ]
    }
}

6 相关信息

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

猜你喜欢

转载自blog.csdn.net/qq_15769939/article/details/114571888