地址: https://github.com/tokenIsme/elasticSearchHelper
使用方式:
1. 创建连接 创建setting mapping index
//创建连接 链接方式1 然后使用accessor 对象进行增删改查数据
```
publicclassTestIndex{publicstaticvoidmain(String[]args) {IAccessoraccessor=newClientFactoryBuilder .Builder() .setCLUSTER_NAME("elasticsearch")//es别名.setCLIENT_PORT(9300)//es 端口.setHOSTS(newArrayList<>(Arrays.asList("127.0.0.1")))//es 地址.create();//创建连接 链接方式2IAccessoraccessor2=newClientFactoryBuilder .Config() .setConfigPath("elasticsearch.properties") .initConfig(true) .createByConfig();//构建indexhelperIndexAccessorindexAccessor=newIndexHelperBuilder .Builder() .withClient(accessor.getClient()) .creatAccessor();//创建索引 mapping 和setting//通过model 生成index mapping 和settingbooleanflag=indexAccessor.createIndexWithSettings(TestModel.class);//添加一条数据TestModelmodel=newTestModel(); model.setPost_id(1); model.setCreatetime(newDate().toString()); model.setIs_delete(1); model.setPost_title("测试啊");booleanflagAdd=accessor.add(model);//添加多条数据、ListtestModelList=newArrayList<>();for(inti=0; i<=10; i++) {TestModelmodel2=newTestModel(); model2.setPost_id(i);//主键不要重复 重复的做updata操作model2.setCreatetime(newDate().toString()); model2.setIs_delete(1); model2.setPost_title("测试啊"); testModelList.add(model2); }booleanflagAddList=accessor.add(testModelList);//删除一条 id 为1的数据accessor.delete("1",TestModel.class);//详情见IAccessor 接口} }
```
2. es搜索
```
publicclassTestSearch{publicstaticvoidmain(String[]args) {BoolQueryBuilderrootBuilder=QueryBuilders.boolQuery();//搜索商品名称 名称进行ik分词+商品skuMapfieldsMap=newHashMap<>(); fieldsMap.put("product_name.ik", (float)2); fieldsMap.put("product_code", (float)1);QueryStringQueryBuilderfieldsMapBuilder=newQueryStringQueryBuilder(keyWord); fieldsMapBuilder.fields(fieldsMap);//创建过滤构造builderBoolQueryBuilderfilterbuilder=QueryBuilders.boolQuery();//添加过滤参数 未删除的已经上架的商品filterbuilder.must(QueryBuilders.termQuery("is_delete",0)); filterbuilder.must(QueryBuilders.termQuery("seller_status",1));//构造总体查询rootBuilder.filter(filterbuilder); rootBuilder.should(fieldsMapBuilder);//创建QueryBuilderCondition 构建整体的查询QueryBuilderConditionbooleanCondtionBuilder=newQueryBuilderCondition.builder() .setBoolQueryBuilder(rootBuilder) .setStart(pageSize*(pageIndex-1)) .setRow(pageSize) .setMinScore(0) .builder();Resultresult=null;try{//开始搜索 just for funresult=EleasticAccessorManager.ACCESSOR_MANAGE.getAccessor() .searchFun(ProductsEntity.class, booleanCondtionBuilder); }catch(Exceptione) { logger.error("elasticSearch 查询接口异常 异常信息:"+e.getMessage()); }//查询的数据listresult.getList(); result.getSearchHits().getTotalHits() } }
```
3. 数据实体类
```
importannotations.Document;importannotations.Field;importannotations.ID;importdata.FieldType;/***@Author:yang*@Date:2018/3/8.18:15*@Desc:es 建索引实体类*/@Document(index="index",type="type",replicas=1,shards=5,settings="Setting.json")publicclassProductsEntity{@IDprivateIntegerpost_id;@Field(type=FieldType.date)privateStringcreatetime;@Field(type=FieldType.date)privateStringupdatatime;@Field(type=FieldType.keyword,fields=true,fieldsName={"ik","pinyin","first_py"},fieldsAnalyzer={"ik_max_word","full_pinyin_letter_analyzer","first_py_letter_analyzer"})privateStringpost_title;@Field(type=FieldType.keyword,fields=true,fieldsName={"ik","pinyin","first_py"},fieldsAnalyzer={"ik_max_word","full_pinyin_letter_analyzer","first_py_letter_analyzer"})privateStringpost_content;@Field(type=FieldType.keyword,fields=true,fieldsName={"ik","pinyin","first_py"},fieldsAnalyzer={"ik_max_word","full_pinyin_letter_analyzer","first_py_letter_analyzer"})privateStringbtt_name;@Field(type=FieldType.text)privateStringpost_img;@Field(type=FieldType.Integer)privateIntegercomments_amount;@Field(type=FieldType.Integer)privateIntegerlike_amount;@Field(type=FieldType.Integer)privateIntegergreen_status;@Field(type=FieldType.Integer)privateIntegeris_delete;@Field(type=FieldType.Integer)privateIntegeruser_id;@Field(type=FieldType.text)privateStringuser_avatar;@Field(type=FieldType.keyword,fields=true,fieldsName={"ik","pinyin","first_py"},fieldsAnalyzer={"ik_max_word","full_pinyin_letter_analyzer","first_py_letter_analyzer"})privateStringnick_name;publicStringgetBtt_name() {returnbtt_name; }publicvoidsetBtt_name(Stringbtt_name) {this.btt_name=btt_name; }publicIntegergetGreen_status() {returngreen_status; }publicvoidsetGreen_status(Integergreen_status) {this.green_status=green_status; }publicIntegergetIs_delete() {returnis_delete; }publicvoidsetIs_delete(Integeris_delete) {this.is_delete=is_delete; }publicIntegergetPost_id() {returnpost_id; }publicvoidsetPost_id(Integerpost_id) {this.post_id=post_id; }publicStringgetCreatetime() {returncreatetime; }publicvoidsetCreatetime(Stringcreatetime) {this.createtime=createtime; }publicStringgetUpdatatime() {returnupdatatime; }publicvoidsetUpdatatime(Stringupdatatime) {this.updatatime=updatatime; }publicStringgetPost_title() {returnpost_title; }publicvoidsetPost_title(Stringpost_title) {this.post_title=post_title; }publicStringgetPost_content() {returnpost_content; }publicvoidsetPost_content(Stringpost_content) {this.post_content=post_content; }publicStringgetPost_img() {returnpost_img; }publicvoidsetPost_img(Stringpost_imgs) {this.post_img=post_imgs; }publicIntegergetComments_amount() {returncomments_amount; }publicvoidsetComments_amount(Integercomments_amount) {this.comments_amount=comments_amount; }publicIntegergetLike_amount() {returnlike_amount; }publicvoidsetLike_amount(Integerlike_amount) {this.like_amount=like_amount; }publicIntegergetUser_id() {returnuser_id; }publicvoidsetUser_id(Integeruser_id) {this.user_id=user_id; }publicStringgetUser_avatar() {returnuser_avatar; }publicvoidsetUser_avatar(Stringuser_avatar) {this.user_avatar=user_avatar; }publicStringgetNick_name() {returnnick_name; }publicvoidsetNick_name(Stringnick_name) {this.nick_name=nick_name; }}
```