zoukankan      html  css  js  c++  java
  • Elasticsearch基本使用

      

    定义

    一个 Elasticsearch 集群可以 包含多个 索引 ,相应的每个索引可以包含多个 类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个 属性 。

    创建一个索引

    http://192.168.1.10:9200/test/student/1 PUT

    {
        "name" :  "liutao",
        "age" :   12,
        "gender" : "",
        "hobby":["足球","游泳"]
    }

    http://192.168.1.10:9200/test/student/2 PUT

    {
        "name" :  "meimei",
        "age" :   11,
        "gender" : "",
        "hobby":["舞蹈","游泳"]
    }

    查询单个

    http://192.168.1.10:9200/test/student/1

    {
        "_index": "test",
        "_type": "student",
        "_id": "1",
        "_version": 3,
        "_seq_no": 2,
        "_primary_term": 1,
        "found": true,
        "_source": {
            "name": "liutao",
            "age": 12,
            "gender": "",
            "hobby": [
                "足球",
                "游泳"
            ]
        }
    }

    查询全部

    http://192.168.1.10:9200/test/student/_search Get

     结果

    {
        "took": 303,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 2,
                "relation": "eq"
            },
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "test",
                    "_type": "student",
                    "_id": "1",
                    "_score": 1.0,
                    "_source": {
                        "name": "liutao",
                        "age": 12,
                        "gender": "",
                        "hobby": [
                            "足球",
                            "游泳"
                        ]
                    }
                },
                {
                    "_index": "test",
                    "_type": "student",
                    "_id": "2",
                    "_score": 1.0,
                    "_source": {
                        "name": "meimei",
                        "age": 11,
                        "gender": "",
                        "hobby": [
                            "舞蹈",
                            "游泳"
                        ]
                    }
                }
            ]
        }
    }

    按条件查询

    http://192.168.1.10:9200/test/student/_search?q=name:meimei  Get

    {
        "took": 6,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 1,
                "relation": "eq"
            },
            "max_score": 0.6931471,
            "hits": [
                {
                    "_index": "test",
                    "_type": "student",
                    "_id": "2",
                    "_score": 0.6931471,
                    "_source": {
                        "name": "meimei",
                        "age": 11,
                        "gender": "",
                        "hobby": [
                            "舞蹈",
                            "游泳"
                        ]
                    }
                }
            ]
        }
    }

    Elasticsearch 提供一个丰富灵活的查询语言叫做 查询表达式 , 它支持构建更加复杂和健壮的查询

    领域特定语言 (DSL), 使用 JSON 构造了一个请求

    http://192.168.1.10:9200/test/student/_search Get

    {
        "query" : {
            "match" : {
                "name" : "meimei"
            }
        }
    }

     结果一样

     这个请求使用 JSON 构造,并使用了一个 match 查询

    更复杂搜索

    过滤器 filter查询  名字是并且年龄大于12的

    http://192.168.1.10:9200/test/student/_search Get

    {
        "query" : {
            "bool": {
                "must": {
                    "match" : {
                        "name" : "liutao" 
                    }
                },
                "filter": {
                    "range" : {
                        "age" : { "gt" : 11 } 
                    }
                }
            }
        }
    }

    全文搜索

    Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度

    精确匹配一系列单词或者_短语_

    {
        "query" : {
            "match_phrase" : {
                
            }
        }
    }

    高亮搜索

    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        },
        "highlight": {
            "fields" : {
                "about" : {}
            }
        }
    }

    聚合

    其他

    suggestions、geolocation、percolation、fuzzy 与 partial matching 等特性

  • 相关阅读:
    CF 633 E. Binary Table
    BZOJ 4589 Hard Nim
    不走弯路,微信小程序的快速入门?
    如果通过cookies和localStorage取值?
    Airbub 弃用React Native
    如何在登陆注册的时候,实现密码框的小眼睛的显示与与隐藏?
    js 实用封装 点击按钮复制到剪贴板
    css渐变写法 从左到右渐变三种颜色示例;
    vue-router 使用二级路由去实现子组件的显示和隐藏
    vue 路由传参中刷新页面参数丢失 及传参的几种方式?
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/14711920.html
Copyright © 2011-2022 走看看