Coder Social home page Coder Social logo

spring-data-elasticsearch-demo's Introduction

The best demo for spring-data-elasticsearch, it shows save, find by page, find by tow parameters using like and @Query.

import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.annotations.Query; import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository; import org.springframework.stereotype.Repository;

@Repository public interface FeedbackRepository extends ElasticsearchCrudRepository<Feedback, Long> {

@Query("{\"bool\" : {\"must\" : {\"term\" : {\"content\" : \"?0\"}}}}")
List<Feedback> findByContent(String content, Pageable pageable);

List<Feedback> findByContentAndIpLike(String content, String ip);

/** Find by Feedback.user, this can't get the correct result. */
List<Feedback> findByUser(User user);

/** Find by Feedback.user.id */
List<Feedback> findByUserId(Long userId);

}

And custom date fomat, ip seaching, and ignore fields save to _source.

import org.springframework.data.elasticsearch.annotations.DateFormat; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldIndex; import org.springframework.data.elasticsearch.annotations.FieldType;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Document(indexName = "feedback1", type = "feedback") public class Feedback extends Searchable {

@Field(type = FieldType.Long)
private Long id;

@Field(
		type = FieldType.Date, 
		format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss"
)
private String time;

@Field(type = FieldType.Long)
private String uid;

/** 没有使用FieldType.ip,是因为这种类型不能模糊搜索 */
/** Not using FieldType.ip,because this type can't support fuzzy search. */
@Field(index = FieldIndex.analyzed, type = FieldType.String)
private String ip;

/** 不序列化到_source */
/** Ignore this field when save to _source */
@JsonIgnore
private String area;

}

spring-data-elasticsearch-demo's People

Contributors

javalover123 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.