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 等特性

 

posted @ 2021-04-28 09:45  富坚老贼  阅读(89)  评论(0编辑  收藏  举报