ElasticSearch 7 学习(5)文档的搜索

  • term(词条)查询和full text(全文)查询
  • 词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索
  • 全文查询:ElasticSearch引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字段中包含词条的任意一个,或全部包含,就匹配查询条件,返回该文档;如果不包含任意一个分词,表示没有任何文档匹配查询条件

单条trem查询 GET/POST请求

term关键字查询,精确查询。例如Sql的where条件

  • 请求
localhost:9200/nba/_search
  • 请求体
{   
    // 语义: 查询词条球号为23
    "query":{   // 查询
        "term":{    // 词条
            "jerse_no":"23" // 字段名称
        }
    }
}
  • 响应
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.9808292,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.9808292,
                "_source": {
                    "name": "詹姆斯",
                    "team_name": "湖人",
                    "position": "小前锋",
                    "play_year": "15",
                    "jerse_no": "23"
                }
            }
        ]
    }
}

多条trem查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
    "query":{
        "terms":{ // 这里使用terms
            "jerse_no": ["23","13"]
        }
    }
}
  • 响应
{
    "took": 0, // 消耗时间
    "timed_out": false, // 是否超时
    "_shards": { // 分片
        "total": 1, // 分片总体 1 
        "successful": 1, // 成功 1
        "skipped": 0,
        "failed": 0
    },
    "hits": { // 命中
        "total": { // 查询总数
            "value": 2, // 2条
            "relation": "eq"
        },
        "max_score": 1.0, // 最大分数 返回结果按照分数最大排序
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "哈登",
                    "team_name": "火箭",
                    "position": "得分后卫",
                    "play_year": "10",
                    "jerse_no": "13"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "詹姆斯",
                    "team_name": "湖人",
                    "position": "小前锋",
                    "play_year": "15",
                    "jerse_no": "23"
                }
            }
        ]
    }
}

match_all查询 GET/POST请求

全文查询

  • 请求
localhost:9200/nba/_search
  • 请求体
{
    "query":{
        "match_all":{} // 查询全部文档,默认显示10条记录
    },
    "from": 0, // 从0开始
    "size": 100 // 查询100条
}
  • 响应
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "哈登",
                    "team_name": "火箭",
                    "position": "得分后卫",
                    "play_year": "10",
                    "jerse_no": "13"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "控球后卫",
                    "play_year": "10",
                    "jerse_no": "30"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "詹姆斯",
                    "team_name": "湖人",
                    "position": "小前锋",
                    "play_year": "15",
                    "jerse_no": "23"
                }
            }
        ]
    }
}

match查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
    "query":{
        "match":{ // 这里使用match
            "name": "库小里" //name:字段 会进行分词匹配
        }
    },
    "from": 0,
    "size": 100
}
  • 响应
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.0834165,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 2.0834165,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "控球后卫",
                    "play_year": "10",
                    "jerse_no": "30"
                }
            }
        ]
    }
}

multi_match 多个查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
    "query":{
        "multi_match":{ // 选定多个字段所以使用multi_match
            "query": "shooter",
            "fields": ["title","name"]  // 指定字段
        }
    }
}
  • 响应
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.18232156,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.18232156,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "控球后卫",
                    "play_year": 10,
                    "jerse_no": "30",
                    "title": "the best shooter"
                }
            }
        ]
    }
}

match_phrase 多个查询 GET/POST请求

准确查询,类似词条查询

  • 请求
localhost:9200/nba/_search
  • 请求体
{
    "query":{
        "match_phrase":{
            "position": "得分后卫"
        }
    }
}
  • 响应
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 3.277387,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 3.277387,
                "_source": {
                    "name": "哈登",
                    "team_name": "火箭",
                    "position": "得分后卫",
                    "play_year": "10",
                    "jerse_no": "13"
                }
            }
        ]
    }
}

match_phrase_profix 多个查询 GET/POST请求

可以增加前缀

  • 请求
localhost:9200/nba/_search
  • 请求体
{
    "query":{
        "match_phrase_prefix":{
            "title": "the" // 指定了前缀
        }
    }
}
  • 响应
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.2876821,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "控球后卫",
                    "play_year": 10,
                    "jerse_no": "30",
                    "title": "the best shooter"
                }
            }
        ]
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/12/elasticsearch-7-learning-document-search/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
ElasticSearch 7 学习(5)文档的搜索
term(词条)查询和full text(全文)查询 词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索 全文查询:ElasticSearch引擎会……
<<上一篇
下一篇>>
文章目录
关闭
目 录