美文网首页
elasticsearch 创建索引

elasticsearch 创建索引

作者: 落单的候鸟 | 来源:发表于2018-04-24 11:20 被阅读0次
# 需要注意,新增mapping字段是允许的
# 但是如果是修改,必须删除索引,重新索引
# 即一个字段的类型修改以后,那么该字段的所有数据都需要重新索引

# 创建索引
PUT my_index

# 创建索引mapping
POST my_index/my_child/_mapping
{
  "my_ child": {
    "_all": {
      "enabled": false
    },
    "properties": {
      "source": {
        "type": "string"
      },
      "content": {
        "type": "string",
        "index": "analyzed",
        "analyzer": "ik_max_word"
      },
      "id": {
        "type": "integer"
      },
      "title": {
        "type": "string"
      },
      "type": {
        "type": "string"
      },
      "create_time": {
        "type": "date",
        "format":"YYYY-MM-dd HH:mm:ss"
      }
    }
  }
}

# 新增索引mapping
POST my_index/my_child/_mapping
{
    "my_child": {
        "properties": {
            "age":{
                "type": "integer"
            },
            "work_exp":{
                "type": "integer"
            },
            "level1":{
                "type": "string",
                "index": "analyzed",
                "analyzer": "ik_max_word"
            },
            "level2":{
                "type": "string",
                "index": "analyzed",
                "analyzer": "ik_max_word"
            },
            "level3":{
                "type": "string",
                "index": "analyzed",
                "analyzer": "ik_max_word"
            }
        }
    }
}

# 查询索引mapping
GET my_index/my_child/_mapping

# 删除索引
DELETE my_index

# 查询内容
GET /my_index/my_child/_search
{
  "from": 0,
  "size": 10,
  "query": {
      "bool": {
        "must": {
          "match": {
            "title": "百度"
          }
        },
        "filter": [
                { 
                  "range": { 
                    "age": { 
                        "lte": "30"              
                      }
                  }
                },
                { 
                  "range": { 
                      "work_exp": { 
                        "gte": "5"              
                      }
                  }
                }
            ]
      }
  },
  "highlight": {
      "fields": {
          "title": {}
      }
  }
}

GET my_index/my_child/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "content": "宋建民" }},
        { "match": { "id": "43049"   }}
      ]
    }
  }
}

GET my_index/my_child/_search
{
  "from": 0,
  "size": 10,
  "query": {
      "match": {
          "title": "百度"
      }
  },
  "highlight": {
      "fields": {
          "title": {}
      }
  }
}

相关文章

网友评论

      本文标题:elasticsearch 创建索引

      本文链接:https://www.haomeiwen.com/subject/luitfftx.html