From 20cf0612d2d7f628ecd5012f7201353cea7e9791 Mon Sep 17 00:00:00 2001 From: wanghaun Date: Wed, 22 Jun 2022 00:32:18 +0800 Subject: [PATCH] =?UTF-8?q?feature/module=5Fsplit=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 393 ++++++++++++++++++++++++++++++++++++++++++++++++++- README.md | 2 + 2 files changed, 394 insertions(+), 1 deletion(-) diff --git a/README.en.md b/README.en.md index 391d758..7e42476 100644 --- a/README.en.md +++ b/README.en.md @@ -173,4 +173,395 @@ public class EsEngineProxyModelQueryTest { log.info("res:{}", JsonParser.asJson(res)); } } -```` \ No newline at end of file +```` +5) Query effect + +````json +{ + "from": 0, + "size": 100, + "timeout": "10s", + "query": { + "bool": { + "filter": [ + { + "wildcard": { + "address": { + "wildcard": "*Tianfu*" + } + } + }, + { + "prefix": { + "personName": { + "value": "Zhang" + } + } + }, + { + "term": { + "salary": { + "value": 67700 + } + } + }, + { + "range": { + "create_time": { + "from": "2021-08-23T21:17:23.385Z", + "to": "2022-06-19T21:17:23.385Z", + "include_lower": true, + "include_upper": true, + "time_zone": "+08:00", + "format": "8uuuu-MM-dd'T'HH:mm:ss.SSS'Z'" + } + } + } + ] + } + } +} +```` + +#### 1.2 Simple parameters + +1) Declare the query interface + +````java + +@EsQueryIndex(value = "person_es_index") +public interface PersonEsParamRepository extends BaseESRepository { + /** + * List query + * + * @return + */ + List queryList(@Terms List personNoList); +} +```` + +2) Test example + +````java + +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest +public class EsEngineProxyModelQueryTest { + @Resource + private PersonEsParamRepository personEsParamRepository; + + /** + * List query test + */ + @Test + public void queryListResponse() { + List personNoList = Lists.newArrayList("US2022060100001", "US2022060100002"); + List res = personEsParamRepository.queryList(personNoList); + log.info("res:{}", JsonParser.asJson(res)); + } +} + +```` + +3) Query effect + +````json +{ + "size": 1000, + "timeout": "10s", + "query": { + "bool": { + "filter": [ + { + "terms": { + "personNo": [ + "US2022060100001", + "US2022060100002" + ] + } + } + ] + } + } +} + +```` + +### 2.sql query + +1) Declare the query interface + +````java + +@EsQueryIndex("person_es_index") +public interface PersonEsSqlRepository extends BaseEsRepository { + /** + * Object parameter test + * @param person + * @return + */ + @EsQuery("SELECT * FROM person_es_index WHERE status = #{person.status} AND sex = #{person.sex}") + List pageQuery(PersonEntity person); +} +```` + +2) Test example + +````java +/** + * Object parameter query test + */ +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest +public class EsEngineProxySqlQueryTest { + @Resource + private PersonEsSqlRepository personEsSqlRepository; + + @Test + public void testSqlPageQuery() { + PersonEntity person = new PersonEntity(); + person.setStatus(1); + person.setSex(1); + List results = personEsSqlRepository.pageQuery(person); + System.out.println(JsonParser.asJson(results)); + } +} +```` + +3) Query effect + +```` +2022-06-21 15:46:24.781INFO 52845---[main]c.e.e.b.c.q.sql.EsSqlExecuteHandler:http://localhost:9200/_sql?format=json +2022-06-21 15:46:24.781INFO 52845---[main]c.e.e.b.c.q.sql.EsSqlExecuteHandler:{"query":"SELECT * FROM person_es_index WHERE status = 1 AND sex = 1"} +```` + +### 3. Expand the query + +#### 3.1 mybatis + +1) Add maven dependencies + +````xml + + + com.elasticsearch.engine + elasticsearch-engine-mybatis + 0.0.1-SNAPSHOT + + +```` + +2) Add the corresponding es query annotation to the mapper interface + +````java + +@EsQueryIndex("person_es_index") +@Mapper +public interface PersonMapper { + + @MybatisEsQuery + PersonEsEntity queryOne(@Param("personNo") String personNo, @Param("status") Integer status); +} +```` + +3) Test example + +````java + +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest +public class EsEngineExtendMybatisQueryTest { + @Resource + private PersonMapper personMapper; + + /** + * single query + */ + @Test + public void testSqlOne() { + PersonEsEntity personEsEntity = personMapper.queryOne("US2022060100001", 1); + log.info("res:{}", JsonParser.asJson(personEsEntity)); + } +} +```` + +4) Query effect +4) Query effect + +```` +2022-06-21 15:54:48.017 INFO 53454 --- [main] c.e.e.m.i.MybatisEsQueryInterceptor: raw sql: SELECT * FROM person WHERE person_no = ? AND status = ? +2022-06-21 15:54:48.075 INFO 53454 --- [main] c.e.e.m.i.MybatisEsQueryInterceptor : After rewriting sql: SELECT * FROM person_es_index WHERE personNo = ? AND status = ? +2022-06-21 15:54:48.076 INFO 53454 --- [main] c.e.e.m.i.MybatisEsQueryInterceptor : After replacing parameters sql: SELECT * FROM person_es_index WHERE personNo = 'US2022060100001' AND status = 1 +2022-06-21 15:54:48.076 INFO 53454 --- [main] c.e.e.b.c.q.sql.EsSqlExecuteHandler: http://localhost:9200/_sql?format=json +2022-06-21 15:54:48.076 INFO 53454 --- [main] c.e.e.b.c.q.sql.EsSqlExecuteHandler : {"query":"SELECT * FROM person_es_index WHERE personNo = 'US2022060100001' AND status = 1"} +```` + +#### 3.2 jpa + +1) Add maven dependencies + +````xml + + + com.elasticsearch.engine + elasticsearch-engine-jpa + 0.0.1-SNAPSHOT + + +```` + +2) Add the corresponding es query annotation to the repository interface + +````java + +@EsQueryIndex("person_es_index") +public interface PersonRepository extends JpaRepository { + + @JpaEsQuery + PersonEntity getByPersonNoAndStatus(String personNo, Integer status); +} +```` + +3) Test example + +````java + +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest +public class EsEngineExtendJpaQueryTest { + @Resource + private PersonRepository personRepository; + + /** + * single query + */ + @Test + public void testSqlOne() { + PersonEntity personEntity = personRepository.getByPersonNoAndStatus("US2022060100001", 1); + log.info("res:{}", JsonParser.asJson(personEntity)); + } +} +```` + +4) Query effect + +```` +2022-06-21 16:00:20.962 INFO 53773 --- [main] c.e.e.b.c.parse.sql.EsSqlQueryHelper : raw sql: select personenti0_.id as id1_1_, personenti0_.address as address2_1_, personenti0_.company as company3_1_, personenti0_.create_time as create_t4_1_, personenti0_.create_user as create_u5_1_, personenti0_.person_name as person_n6_1_, personenti0_.person_no as person_n7_1_, personenti0_.phone as phone8_1_, personenti0_.salary as salary9_1_, personenti0_.sex as sex10_1_, personenti0_.status as status11_1_ from person personenti0_ where personenti0 person_no='US2022060100001' and personenti0_.status=1 +2022-06-21 16:00:21.008 INFO 53773 --- [ main] c.e.e.b.c.parse.sql.EsSqlQueryHelper : After rewriting sql: SELECT id, address, company, createTime, createUser, personName, personNo, phone, salary, sex, status FROM person_es_index WHERE personNo = 'US2022060100001' AND status = 1 +2022-06-21 16:00:21.009 INFO 53773 --- [ main] c.e.e.b.c.parse.sql.EsSqlQueryHelper : sql after replacing parameters: SELECT id, address, company, createTime, createUser, personName, personNo, phone, salary, sex , status FROM person_es_index WHERE personNo = 'US2022060100001' AND status = 1 +2022-06-21 16:00:21.010 INFO 53773 --- [main] c.e.e.b.c.q.sql.EsSqlExecuteHandler : http://localhost:9200/_sql?format=json +2022-06-21 16:00:21.010 INFO 53773 --- [ main] c.e.e.b.c.q.sql.EsSqlExecuteHandler : {"query":"SELECT id, address, company, createTime, createUser, personName, personNo, phone, salary, sex, status FROM person_es_index WHERE personNo = 'US2022060100001' AND status = 1"} +```` + +#### 3.3 jooq + +1) Add maven dependencies + +````xml + + + com.elasticsearch.engine + elasticsearch-engine-jooq + 0.0.1-SNAPSHOT + + +```` + +2) The dao implementation class adds the corresponding es query annotation + +````java + +@EsQueryIndex("person_es_index") +@Component +public class PersonJooqDaoImpl implements PersonJooqDao { + + @Autowired + private DSLContext context; + + private final Person PERSON = Tables.PERSON; + + /** + * @param personNo + * @param status + * @return + */ + @JooqEsQuery + @Override + public PersonEntity getByPersonNoAndStatus(String personNo, Integer status) { + return context.selectFrom(PERSON).where( + PERSON.PERSON_NO.eq(personNo).and(PERSON.STATUS.eq(status.byteValue())) + ).fetchOneInto(PersonEntity.class); + } +} +```` + +3) Test example + +````java + +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest +public class EsEngineExtendJooqQueryTest { + @Resource + private PersonJooqDao personJooqDao; + + /** + * single query + */ + @Test + public void testSqlOne() { + PersonEntity personEntity = personJooqDao.getByPersonNoAndStatus("US2022060100001", 4); + log.info("res:{}", JsonParser.asJson(personEntity)); + } +} +```` +4) Query effect + +```` +2022-06-21 16:03:05.629 INFO 53945 --- [main] c.e.e.b.c.parse.sql.EsSqlQueryHelper: raw sql: select + `user`.`person`.`id`, + `user`.`person`.`person_no`, + `user`.`person`.`person_name`, + `user`.`person`.`phone`, + `user`.`person`.`salary`, + `user`.`person`.`company`, + `user`.`person`.`status`, + `user`.`person`.`sex`, + `user`.`person`.`address`, + `user`.`person`.`create_time`, + `user`.`person`.`create_user` +from `user`.`person` +where ( + `user`.`person`.`person_no` = 'US2022060100001' + and `user`.`person`.`status` = 4 +) +2022-06-21 16:03:05.674 INFO 53945 --- [main] c.e.e.b.c.parse.sql.EsSqlQueryHelper : rewritten sql: SELECT `id`, `personNo`, `personName`, `phone`, `salary`, `company`, `status`, `sex`, `address`, `createTime`, `createUser` FROM person_es_index WHERE (`personNo` = 'US2022060100001' AND `status` = 4) +2022-06-21 16:03:05.675 INFO 53945 --- [main] c.e.e.b.c.parse.sql.EsSqlQueryHelper : After replacing parameters sql: SELECT id, personNo, personName, phone, salary, company, status, sex, address, createTime , createUser FROM person_es_index WHERE (personNo = 'US2022060100001' AND status = 4) +2022-06-21 16:03:05.676 INFO 53945 --- [main] c.e.e.b.c.q.sql.EsSqlExecuteHandler: http://localhost:9200/_sql?format=json +2022-06-21 16:03:05.676 INFO 53945 --- [ main] c.e.e.b.c.q.sql.EsSqlExecuteHandler : {"query":"SELECT id, personNo, personName, phone, salary, company, status, sex, address, createTime, createUser FROM person_es_index WHERE (personNo = 'US2022060100001' AND status = 4)"} +```` + +## Usage example + +https://gitee.com/my-source-project/elasticsearch-engine-demo + +https://github.com/wanghuan9/elasticsearch-engine-demo + +## Related documents + +To be completed... + +## Compatibility + +elasticsearch field naming supports camel case and underscore + +elasticsearch version supports v6 and v7 + +## References and references + +The annotation query of this project refers to the open source project https://gitee.com/JohenTeng/elasticsearch-helper \ No newline at end of file diff --git a/README.md b/README.md index c3a2eed..037c1f0 100644 --- a/README.md +++ b/README.md @@ -551,6 +551,8 @@ where ( https://gitee.com/my-source-project/elasticsearch-engine-demo +https://github.com/wanghuan9/elasticsearch-engine-demo + ## 相关文档 待补全... -- Gitee