elasticsearch的基本操作2


#创建索引,指定数据结构
PUT /novel
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik": {
          "type": "ik_max_word"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "author": {
        "type": "keyword"
      },
      "descr": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "count": {
        "type": "long"
      },
      "onsale": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}



#添加文档,自动生成id
POST /novel/_doc
{
  "name": "盘龙",
  "author": "我是西红柿",
  "count": 100000,
  "onsale": "2021-01-01",
  "descr": "嘻嘻哈哈嘻嘻哈啊信息哈哈"
}

#添加文档,设置id
POST /novel/_doc/2
{
  "name": "盘龙古",
  "author": "我是西红柿",
  "count": 100000,
  "onsale": "2021-01-01",
  "descr": "嘻嘻哈哈嘻嘻哈啊信息哈哈"
}



test2026-08-12 18:56


#[RequestMapping(path:'deleteDoc',methods:'get')]
    public function deleteDoc(ElasticsearchService $es)
    {
        //删除文档
        $client = $es->getClient();
    
        $response = $client->delete([
            'index' => 'novel',
            'id'    => '1'
        ]);
    
        return $response;
    }

test2026-08-12 21:14