【大数据开发】Elasticsearch实操

参考博客:ElasticSearch综合练习题
elasticsearch搜索引擎的常用方法(二) term和terms,match,range,sort等


# 1.插入数据
PUT /megacorp/employee/3
{
  "first_name":"Douglas",
  "last_name":"Fir",
  "age":35,
  "about":"I like to build cabinets",
  "interests": ["forestry"]
}

# 2.查看雇员id为1的信息
GET /megacorp/employee/1

# 3.搜索所有员工信息
GET /megacorp/employee/_search
# 和上面结果是一样的
GET /megacorp/employee/_search
{
  "query": {
    "match_all": {}
  }
}

# 4.搜索特定条件的雇员信息:通过非json形式搜索名字为Smith的雇员
GET /megacorp/employee/_search
{
  "query": {
    "multi_match":{
      "query": "Smith",
      "fields": ["last_name"]
    }
  }
}
# 和上面结果一样
GET /megacorp/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "last_name": "Smith"
          }
        }
      ]
    }
  }
}

# 6.搜索名字为 Smith 的雇员,但年龄大于 30 岁的。
GET /megacorp/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "last_name": "Smith"
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "age": {
              "lte": 30
            }
          }
        }
      ]
    }
  }
}
# 和上面结果一样
GET /megacorp/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "last_name": "Smith"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 30
            }
          }
        }
      ]
    }
  }
}

# 7.搜索下所有喜欢攀岩(rock climbing)的雇员。
# match模式下会自动分词
GET /megacorp/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "about": "climbing"
          }
        }
      ]
    }
  }
}
# 和上面结果一样
GET /megacorp/employee/_search
{
  "query": {
    "multi_match": {
      "query": "climbing",
      "fields": ["about"]
    }
  }
}

# 8.仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语“rock climbing” 的形式紧挨着的雇员记录
GET /megacorp/employee/_search
{
  "query": {
    "match_phrase": {
      "about": "rock climbing"
    }
  }
}

# 9.按照第8题的搜索要求,同时需要高亮显示搜索的内容。
GET /megacorp/employee/_search
{
  "query": {
    "match_phrase": {
      "about": "rock climbing"
    }
  },
  "highlight": {
    "fields": {
      "about":{}
    }
  }
}


vim student.json
{"create":{"_index":"stu","_type":"doc","_id":"1"}}
{"id": 1, "studentNo": "TH-CHEM-2016-C001", "name": "Jonh Smith", "major":"Chemistry", "gpa": 4.8, "yearOfBorn": 2000, "classOf": 2016,  "interest": "soccer, basketball, badminton, chess"}
{"create":{"_index":"stu","_type":"doc","_id":"2"}}
{"id": 2, "studentNo": "TH-PHY-2018-C001", "name": "Isaac Newton", "major":"Physics", "gpa": 3.6, "yearOfBorn": 2001, "classOf": 2018,  "interest": "novel, soccer, cooking"}
{"create":{"_index":"stu","_type":"doc","_id":"3"}}
{"id": 3, "studentNo": "BU-POLI-2016-C001", "name": "John Kennedy", "major":"Politics", "gpa": 4.2, "yearOfBorn": 2000, "classOf": 2016,  "interest": "talking, dating, boxing, shooting, chess"}
{"create":{"_index":"stu","_type":"doc","_id":"4"}}
{"id": 4, "studentNo": "BU-POLI-2015-C001", "name": "John Kerry",  "major":"Politics", "gpa": 4.1, "yearOfBorn": 1999, "classOf": 2015,  "interest": "money, basketball"}
{"create":{"_index":"stu","_type":"doc","_id":"5"}}
{"id": 5, "studentNo": "BU-ARTS-2016-C002", "name": "Da Vinci",  "major":"Arts", "gpa": 4.8, "yearOfBorn": 1995, "classOf": 2016,  "interest": "drawing, music, wine"}

# 导入数据
curl -XPUT 'host01:9200/_bulk' -H 'Content-Type:application/json' --data-binary @student.json

# 1)同时查询id为1,3,5
GET /stu/doc/_search
{
  "query": {
    "terms": {
      "id": [
        "1",
        "3",
        "5"
      ]
    }
  }
}

# 2)名字不叫John的文档
GET /stu/doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "John"
          }
        }
      ]
    }
  }
}

# 3)2016年以前入学的文档
GET /stu/doc/_search
{
  "query": {
    "range": {
      "classOf": {
        "lte": "2015"
      }
    }
  }
}


# 查询所有index列表
GET /stu/_mapping?pretty=true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值