zoukankan      html  css  js  c++  java
  • Elastic Search 搜索的简单使用

    搜索的简单使用

    准备工作

    • 删掉nba索引
      • DELETE localhost:9200/nba
    • 新建一个索引,并且指定mapping新增document
      • PUT localhost:9200/nba
      • {
            "mappings":{
                "properties":{
                    "name":{
                        "type":"text"
                    },
                    "team_name":{
                        "type":"text"
                    },
                    "position":{
                        "type":"text"
                    },
                    "play_year":{
                        "type":"long"
                    },
                    "jerse_no":{
                        "type":"keyword"
                    }
                }
            }
        }
      • PUT localhost:9200/nba/_doc/1
      • {
            "name":"哈登",
            "team_name":"火箭",
            "position":"得分后卫",
            "play_year":10,
            "jerse_no":"13"
        }
      • PUT localhost:9200/nba/_doc/2
      • {
            "name":"库里",
            "team_name":"勇士",
            "position":"控球后卫",
            "play_year":10,
            "jerse_no":"30"
        }
      • PUT localhost:9200/nba/_doc/3
      • {
            "name":"詹姆斯",
            "team_name":"湖人",
            "position":"小前锋",
            "play_year":15,
            "jerse_no":"23"
        }

    term(词条)查询和full text(全文)查询

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

    单条term查询

    POST localhost:9200/nba/_search
    
    {
        "query":{
            "term":{
                "jerse_no":"23"
            }
        }
    }

    多条term查询

    POST localhost:9200/nba/_search
    
    参数:
    {
        "query":{
            "terms":{
                "jerse_no":[
                    "23",
                    "13"
                ]
            }
        }
    }
    
    返回值:
    {
        "took": 18,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 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

    POST localhost:9200/nba/_search
    
    参数:
    {
        "query":{
            "match_all":{
    
            }
        },
        "from":0,
        "size":10
    }
    
    返回值:
    {
        "took": 4,
        "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

    POST localhost:9200/nba/_search
    
    参数:
    {
        "query":{
            "match":{
                "position":"后卫"
            }
        }
    }
    
    返回值:
    {
        "took": 55,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 2,
                "relation": "eq"
            },
            "max_score": 0.90630186,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 0.90630186,
                    "_source": {
                        "name": "哈登",
                        "team_name": "火箭",
                        "position": "得分后卫",
                        "play_year": 10,
                        "jerse_no": "13"
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "2",
                    "_score": 0.90630186,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "控球后卫",
                        "play_year": 10,
                        "jerse_no": "30"
                    }
                }
            ]
        }
    }

    multi_match

    POST localhost:9200/nba/_update/2
    
    参数:
    {
        "doc":{
            "name":"库里",
            "team_name":"勇士",
            "position":"控球后卫",
            "play_year":10,
            "jerse_no":"30",
            "title":"the best shooter"
        }
    }
    
    POST localhost:9200/nba/_search
    
    参数:
    {
        "query":{
            "multi_match":{
                "query":"shooter",
                "fields":[
                    "title",
                    "name"
                ]
            }
        }
    }
    
    参数:
    {
        "query":{
            "multi_match":{
                "query":"shooter",
                "fields":[
                    "*title",
                    "name"
                ]
            }
        }
    }

    match_phrase

    post localhost:9200/nba/_search
    
    参数:
    {
        "query":{
            "match_phrase":{
                "position":"得分后卫"
            }
        }
    }
    
    返回值:
    {
        "took": 12,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 1,
                "relation": "eq"
            },
            "max_score": 2.797622,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 2.797622,
                    "_source": {
                        "name": "哈登",
                        "team_name": "⽕火箭",
                        "position": "得分后卫",
                        "play_year": 10,
                        "jerse_no": "13"
                    }
                }
            ]
        }
    }

    match_phrase_prefix

    POST localhost:9200/nba/_update/3
    
    参数:
    {
        "doc":{
            "name":"詹姆斯",
            "team_name":"湖人",
            "position":"小前锋",
            "play_year":15,
            "jerse_no":"23",
            "title":"the best small forward"
        }
    }
    
    POST localhost:9200/nba/_search
    
    参数:
    {
        "query":{
            "match_phrase_prefix":{
                "title":"the best s"
            }
        }
    }
    
    返回值:
    {
        "took": 4,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 2,
                "relation": "eq"
            },
            "max_score": 1.8596163,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "2",
                    "_score": 1.8596163,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "控球后卫",
                        "play_year": 10,
                        "jerse_no": "30",
                        "title": "the best shooter"
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "3",
                    "_score": 1.6542599,
                    "_source": {
                        "name": "詹姆斯",
                        "team_name": "湖人",
                        "position": "小前锋",
                        "play_year": 15,
                        "jerse_no": "23",
                        "title": "the best small forward"
                    }
                }
            ]
        }
    }

     

  • 相关阅读:
    jsp
    session
    cookie
    异常
    静态导入 可变参数与collectiions集合工具 集合嵌套
    HashMap和LinkedHashMap
    map集合
    匿名对像,,,内部类
    电脑版校网登录助手
    360舵机与180舵机
  • 原文地址:https://www.cnblogs.com/jwen1994/p/12507770.html
Copyright © 2011-2022 走看看