Search in sources :

Example 1 with IndexQueryBuilder

use of org.springframework.data.elasticsearch.core.query.IndexQueryBuilder in project spring-data-elasticsearch by spring-projects.

the class AbstractElasticsearchTemplate method getIndexQuery.

private <T> IndexQuery getIndexQuery(T entity) {
    String id = getEntityId(entity);
    if (id != null) {
        id = elasticsearchConverter.convertId(id);
    }
    // noinspection ConstantConditions
    IndexQueryBuilder builder = // 
    new IndexQueryBuilder().withId(// 
    id).withObject(entity);
    SeqNoPrimaryTerm seqNoPrimaryTerm = getEntitySeqNoPrimaryTerm(entity);
    if (seqNoPrimaryTerm != null) {
        builder.withSeqNoPrimaryTerm(seqNoPrimaryTerm);
    } else {
        // version cannot be used together with seq_no and primary_term
        // noinspection ConstantConditions
        builder.withVersion(getEntityVersion(entity));
    }
    String routing = getEntityRouting(entity);
    if (routing != null) {
        builder.withRouting(routing);
    }
    return builder.build();
}
Also used : IndexQueryBuilder(org.springframework.data.elasticsearch.core.query.IndexQueryBuilder) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)

Example 2 with IndexQueryBuilder

use of org.springframework.data.elasticsearch.core.query.IndexQueryBuilder in project HuaXing-learningCenter by 17666555910.

the class BaseServiceImpl method batchInsertOrUpdate.

@Override
public void batchInsertOrUpdate(List<T> entityModelList) {
    if (CollectionUtils.isEmpty(entityModelList)) {
        throw new CommonException("entityModelList Can't be empty");
    }
    // 判断索引是否存在 若不存在则创建索引和映射
    if (!elasticsearchTemplate.indexExists(getEntityClass())) {
        this.createEntityEsIndex();
    }
    List<IndexQuery> queries = new ArrayList<>();
    for (T entityEsModel : entityModelList) {
        IndexQuery indexQuery = new IndexQueryBuilder().withId(entityEsModel.getId()).withObject(entityEsModel).build();
        queries.add(indexQuery);
    }
    // 批量插入
    this.bulkInsert(queries);
}
Also used : IndexQueryBuilder(org.springframework.data.elasticsearch.core.query.IndexQueryBuilder) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) ArrayList(java.util.ArrayList) CommonException(com.huaxing.springboot_elasticsearch.common.utils.CommonException)

Example 3 with IndexQueryBuilder

use of org.springframework.data.elasticsearch.core.query.IndexQueryBuilder in project demo by breeze0630.

the class ElasticsearchTemplateTest method testInsert.

@Test
public void testInsert() {
    Commodity commodity = new Commodity();
    commodity.setSkuId("1501009005");
    commodity.setName("葡萄吐司面包(10片装)");
    commodity.setCategory("101");
    commodity.setPrice(160);
    commodity.setBrand("良品铺子");
    IndexQuery indexQuery = new IndexQueryBuilder().withObject(commodity).build();
    elasticsearchTemplate.index(indexQuery);
}
Also used : IndexQueryBuilder(org.springframework.data.elasticsearch.core.query.IndexQueryBuilder) Commodity(com.example.esdemo.dto.Commodity) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with IndexQueryBuilder

use of org.springframework.data.elasticsearch.core.query.IndexQueryBuilder in project IT-Demo by yanghaiji.

the class EsRestTemplateImpl method index.

public String index() {
    UserEntity userEntity = UserEntity.builder().id(System.currentTimeMillis()).brand("javayh").images("es.png").price(2020.29).title("Java有货").build();
    IndexQuery indexQuery = new IndexQueryBuilder().withObject(userEntity).build();
    String sys_user = elasticsearchTemplate.index(indexQuery, IndexCoordinates.of("sys_user"));
    return sys_user;
}
Also used : IndexQueryBuilder(org.springframework.data.elasticsearch.core.query.IndexQueryBuilder) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) UserEntity(com.javayh.elaticsearh.docment.UserEntity)

Example 5 with IndexQueryBuilder

use of org.springframework.data.elasticsearch.core.query.IndexQueryBuilder in project IT-Demo by yanghaiji.

the class EsRestTemplateImpl method insertItemDocBulk.

/**
 * <p>
 * 批量信息
 * </p>
 *
 * @param
 * @return void
 * @version 1.0.0
 * @author hai ji
 * @since 2022/2/9
 */
public void insertItemDocBulk() {
    List<IndexQuery> list = new ArrayList<>();
    list.add(new IndexQueryBuilder().withObject(UserEntity.builder().id(System.currentTimeMillis()).brand("javayh").images("es.png").price(2020.29).title("Java有货").build()).build());
    list.add(new IndexQueryBuilder().withObject(UserEntity.builder().id(System.currentTimeMillis() + 1).brand("javayh").images("es.png").price(2020.29).title("Java有货").build()).build());
    elasticsearchTemplate.bulkIndex(list, IndexCoordinates.of("sys_user"));
}
Also used : IndexQueryBuilder(org.springframework.data.elasticsearch.core.query.IndexQueryBuilder) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) ArrayList(java.util.ArrayList)

Aggregations

IndexQueryBuilder (org.springframework.data.elasticsearch.core.query.IndexQueryBuilder)5 IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)4 ArrayList (java.util.ArrayList)2 Commodity (com.example.esdemo.dto.Commodity)1 CommonException (com.huaxing.springboot_elasticsearch.common.utils.CommonException)1 UserEntity (com.javayh.elaticsearh.docment.UserEntity)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 SeqNoPrimaryTerm (org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)1