进阶-第82__熟练掌握ES Java API_基于upsert实现汽车最新价格的调整

 

做一个汽车零售数据的mapping,我们要做的第一份数据,其实汽车信息\

设置mappings

PUT /car_shop

{

    "mappings": {

        "cars": {

            "properties": {

                "brand": {

                    "type": "text",

                    "analyzer": "ik_max_word",

                    "fields": {

                        "raw": {

                            "type": "keyword"

                        }

                    }

                },

                "name": {

                    "type": "text",

                    "analyzer": "ik_max_word",

                    "fields": {

                        "raw": {

                            "type": "keyword"

                        }

                    }

                }

            }

        }

    }

}

 

首先的话呢,第一次调整宝马320这个汽车的售价,我们希望将售价设置为32万,用一个upsert语法,如果这个汽车的信息之前不存在,那么就insert,如果存在,那么就update

基于upsert实现最新价格的调整

package com.es.core.senior;

import java.net.InetAddress;

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class UpsertCarInfoApp {

    @SuppressWarnings({"unchecked", "resource"})
    public static void main(String[] args) throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        //汽车信息的保存-->如果id不存在则进行插入
        IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1")
                .source(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("brand", "宝马")
                        .field("name", "宝马320")
                        .field("price", 320000)
                        .field("produce_date", "2017-01-01")
                        .endObject());

        //信息的更新。如果存在,则进行更新
        UpdateRequest updateRequest = new UpdateRequest("car_shop", "cars", "1")
                .doc(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("price", 310000)//降价
                        .endObject()
                ).upsert(indexRequest);

        UpdateResponse updateResponse  = client.update(updateRequest).get();
        System.out.println(updateResponse.getVersion());
        client.close();



    }

}

 

 

查看结果:

GET /car_shop/cars/_search

结果:

{

  "took": 4,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 1,

    "hits": [

      {

        "_index": "car_shop",

        "_type": "cars",

        "_id": "1",

        "_score": 1,

        "_source": {

          "brand": "宝马",

          "name": "宝马320",

          "price": 320000,

          "produce_date": "2017-01-01"

        }

      }

    ]

  }

}

 

再执行一遍

package com.es.core.senior;

import java.net.InetAddress;

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class UpsertCarInfoApp {

    @SuppressWarnings({"unchecked", "resource"})
    public static void main(String[] args) throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        //汽车信息的保存-->如果id不存在则进行插入
        IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1")
                .source(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("brand", "宝马")
                        .field("name", "宝马320")
                        .field("price", 320000)
                        .field("produce_date", "2017-01-01")
                        .endObject());

        //信息的更新。如果存在,则进行更新
        UpdateRequest updateRequest = new UpdateRequest("car_shop", "cars", "1")
                .doc(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("price", 310000)//降价
                        .endObject()
                ).upsert(indexRequest);

        UpdateResponse updateResponse  = client.update(updateRequest).get();
        System.out.println(updateResponse.getVersion());

       client.close();


    }

}

 

 

查看结果:

GET /car_shop/cars/_search

结果:

{

  "took": 1,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 1,

    "hits": [

      {

        "_index": "car_shop",

        "_type": "cars",

        "_id": "1",

        "_score": 1,

        "_source": {

          "brand": "宝马",

          "name": "宝马320",

          "price": 310000,

          "produce_date": "2017-01-01"

        }

      }

    ]

  }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值