美文网首页
83_熟练掌握ES Java API_基于upsert实现汽车最

83_熟练掌握ES Java API_基于upsert实现汽车最

作者: 小山居 | 来源:发表于2020-03-04 16:56 被阅读0次

    <meta charset="utf-8">

    83_熟练掌握ES Java API_基于upsert实现汽车最新价格的调整

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

    maven工程目录结构:

    image.png
    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

    
    IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1")
            .source(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(jsonBuilder()
                .startObject()
                    .field("price", 320000)
                .endObject())
            .upsert(indexRequest);       
                   
    client.update(updateRequest).get();
    
    IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1")
            .source(jsonBuilder()
                .startObject()
                    .field("brand", "宝马")
                    .field("name", "宝马320")
                    .field("price", 310000)
                    .field("produce_date", "2017-01-01")
                .endObject());
    UpdateRequest updateRequest = new UpdateRequest("car_shop", "cars", "1")
            .doc(jsonBuilder()
                .startObject()
                    .field("price", 310000)
                .endObject())
            .upsert(indexRequest);              
    client.update(updateRequest).get();
    

    完整的源码信息:

    UpsertCarInfoApp

    
    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));
        
            IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1")
                    .source(XContentFactory.jsonBuilder()
                                .startObject()
                                    .field("brand", "宝马")
                                    .field("name", "宝马320")
                                    .field("price", 310000)
                                    .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()); 
        }
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:83_熟练掌握ES Java API_基于upsert实现汽车最

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