Elasticsearch的简单使用案例

  • 1.添加一个学生的成绩(ip: port/库/表/id)
curl -XPUT 'http://192.168.80.123:9200/school/jsj/1' -d '{
  "class": "软件工程",
  "subject": "math",
  "name": {
    "first": "li",
    "last": "jie"
  },
  "create_time": "2017-08-10",
  "score": "98"
}'
  • 2.通过浏览器用id查询学生成绩
http://192.168.80.123:9200/school/jsj/1

结果:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "class": "软件工程",
    "subject": "math",
    "name": {
      "first": "li",
      "last": "jie"
    },
    "create_time": "2017-08-10",
    "score": "98"
  }
}
  • 3.在linux中通过curl的方式用id查询学生成绩
curl -XGET 'http://192.168.80.123:9200/school/jsj/1'

返回:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "class": "软件工程",
    "subject": "math",
    "name": {
      "first": "li",
      "last": "jie"
    },
    "create_time": "2017-08-10",
    "score": "98"
  }
}
  • 4.添加另外一个学生的成绩
curl -XPUT 'http://192.168.80.123:9200/school/jsj/2' -d '{
  "subject": "math",
  "name": {
    "first": "zhang",
    "last": "san"
  },
  "create_time": "2017-07-01",
  "score": "59"
}'
  • 5.通过_source获取指定的字段
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source=subject'

结果:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "subject": "math"
  }
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source=subject,score'

结果:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "score": "98",
    "subject": "math"
  }
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source'

结果:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "class": "软件工程",
    "subject": "math",
    "name": {
      "first": "li",
      "last": "jie"
    },
    "create_time": "2017-08-10",
    "score": "98"
  }
}
  • 6.可以通过覆盖的方式更新
curl -XPUT 'http://192.168.80.123:9200/school/jsj/1' -d '{
  "subject": "math",
  "name": {
    "first": "li",
    "last": "jie"
  },
  "create_time": "2017-08-11",
  "score": "100"
}'

返回:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

再查看就变成100分了.

  • 7.通过_updateAPI的方式单独更新你想要更新的
curl -XPOST 'http://192.168.80.123:9200/school/jsj/1/_update' -d '{
  "doc": {
    "score": "666"
  }
}'

返回:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 4,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

再查看就变成666分了.

  • 8.删除一个文档
curl -XDELETE 'http://192.168.80.123:9200/school/jsj/1'

返回:

{
  "found": true,
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "_version": 5,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

再查看id为1的返回数据为:

{
  "_index": "school",
  "_type": "jsj",
  "_id": "1",
  "found": false
}
  • 9.查询所有
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{  
    "query": { "match_all": {} }  
}'

返回:

{
    "took": 10,                //执行搜索的时间(以毫秒为单位)
    "timed_out": false,        //是否超时
    "_shards": {               //搜索分片,成功和失败的分片
    "total": 5,                //总搜索分片
    "successful": 5,           //成功搜索分片
    "failed": 0                //失败搜索分片 
    },
    "hits": {
        "total": 2,               //符合我们的搜索条件的文档总数
        "max_score": 1.0,         //最高分数
        "hits": [{                //搜索结果的实际数组(默认为前10个文档)
            "_index": "school",
            "_type": "jsj",
            "_id": "2",
            "_score": 1.0,           //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
            "_source": {
                "subject": "math",
                "name": {
                    "first": "zhang",
                    "last": "san"
                },
                "create_time": "2017-07-01",
                "score": "59"
            }
        },
        {
        "_index": "school",
        "_type": "jsj",
        "_id": "1",
        "_score": 1.0,            //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
        "_source": {
            "class": "软件工程",
            "subject": "math",
            "name": {
                "first": "li",
                "last": "jie"
            },
            "create_time": "2017-08-10",
            "score": "98"
        }
    }]
 }
}
  • 10.查询第一条
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
    "query": {
        "match_all": {

        }
    },
    "size": 1
}'

返回:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.0,
        "hits": [{
            "_index": "school",
            "_type": "jsj",
            "_id": "2",
            "_score": 1.0,
            "_source": {
                "subject": "math",
                "name": {
                    "first": "zhang",
                    "last": "san" },
                "create_time": "2017-07-01",
                "score": "59"
            }
        }]
    }
}
  • 11.分页查询 from表示从几条开始查询, size表示查询的条数 和limit一样,其中第一条是0条
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
    "query": {
        "match_all": {

        }
    },
    "from": 1,
    "size": 10
}'

返回:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.0,
        "hits": [{
            "_index": "school",
            "_type": "jsj",
            "_id": "1",
            "_score": 1.0,
            "_source": {
                "class": "软件工程",
                "subject": "math",
                "name": {
                    "first": "li",
                    "last": "jie" },
                "create_time": "2017-08-10",
                "score": "98"
            }
        }]
    }
}
  • 12.排序

先执行(不然报错,报错如下):
{“error”:{“root_cause”:[{“type”:”illegal_argument_exception”,”reason”:”Fielddata is disabled on text fields by default. Set fielddata=true

curl -XPUT 'http://192.168.80.123:9200/school/_mapping/jsj/' -d '{
  "properties": {
    "score": { 
      "type":     "text",
      "fielddata": true
    }
  }
}'

返回:

{"acknowledged":true}

然后:

curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
    "query": {
        "match_all": {

        }
    },
    "sort": {
        "score": "desc"
    }
}'

返回:

{
    "took": 140,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": null,
        "hits": [{
            "_index": "school",
            "_type": "jsj",
            "_id": "1",
            "_score": null,
            "_source": {
                "class": "软件工程",
                "subject": "math",
                "name": {
                    "first": "li",
                    "last": "jie" },
                "create_time": "2017-08-10",
                "score": "98"
            },
            "sort": ["98"]
        },
        {
            "_index": "school",
            "_type": "jsj",
            "_id": "2",
            "_score": null,
            "_source": {
                "subject": "math",
                "name": {
                    "first": "zhang",
                    "last": "san" },
                "create_time": "2017-07-01",
                "score": "59"
            },
            "sort": ["59"]
        }]
    }
}
  • 13.返回部分制字段
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
    "query": {
        "match_all": {

        }
    },
    "_source": ["subject",
    "name.last"]
}'

返回:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.0,
        "hits": [{
            "_index": "school",
            "_type": "jsj",
            "_id": "2",
            "_score": 1.0,
            "_source": {
                "subject": "math",
                "name": {
                    "last": "san" }
            }
        },
        {
            "_index": "school",
            "_type": "jsj",
            "_id": "1",
            "_score": 1.0,
            "_source": {
                "subject": "math",
                "name": {
                    "last": "jie" }
            }
        }]
    }
}
  • 14.匹配查询
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
    "query": {
        "match": {
            "name.last": "san"
        }
    },
    "_source": ["subject",
    "name.first"]
}'

返回:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [{
            "_index": "school",
            "_type": "jsj",
            "_id": "2",
            "_score": 0.2876821,
            "_source": {
                "subject": "math",
                "name": {
                    "first": "zhang" }
            }
        }]
    }
}
  • 15.同时包含查询match_phrase
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "query": { "match_phrase": { "name.last": "san" } }
}'
  • 16.bool查询must 需要全部满足
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "query": {
    "bool": {
      "must": [
        { "match": { "subject": "math" } },
        { "match": { "name.last": "jie" } }
      ]
    }
  }
}'
  • 17.bool查询should 只要满足一个就行
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "query": {
    "bool": {
      "should": [
        { "match": { "subject": "aaa" } },
        { "match": { "name.last": "jie" } }
      ]
    }
  }
}'
  • 18.bool查询must_not 全部都不满足
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "subject": "math" } },
        { "match": { "name.last": "jie" } }
      ]
    }
  }
}'
  • 19.bool查询must, should, must_not
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "query": {
    "bool": {
      "must": [
        { "match": { "subject": "math" } }
      ],
      "must_not": [
        { "match": { "name.last": "jie" } }
      ]
    }
  }
}'
  • 20.过滤查询(它不会去计算分值,因此效率也就更高一些) 这里score是string类型 从左到右做比较 非数字大小
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "score": {
            "gt": "8",
            "lt": "99999"
          }
        }
      }
    }
  }
}'
  • 21.聚合查询

首先:

curl -XPUT 'http://192.168.80.123:9200/school/_mapping/jsj/' -d '{
  "properties": {
    "name.last": { 
      "type":     "text",
      "fielddata": true
    }
  }
}'

然后:

curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "name.last"
      }
    }
  }
}'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值