分享一個(gè)elasticSearch的幫助jar 【elasticSearchHelper】

? 地址:? https://github.com/tokenIsme/elasticSearchHelper



使用方式:

1. 創(chuàng)建連接 創(chuàng)建setting mapping index

//創(chuàng)建連接 鏈接方式1 然后使用accessor 對(duì)象進(jìn)行增刪改查數(shù)據(jù)

```?

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();//創(chuàng)建連接 鏈接方式2IAccessoraccessor2=newClientFactoryBuilder? ? ? ? ? ? ? ? .Config()? ? ? ? ? ? ? ? .setConfigPath("elasticsearch.properties")? ? ? ? ? ? ? ? .initConfig(true)? ? ? ? ? ? ? ? .createByConfig();//構(gòu)建indexhelperIndexAccessorindexAccessor=newIndexHelperBuilder? ? ? ? ? ? ? ? .Builder()? ? ? ? ? ? ? ? .withClient(accessor.getClient())? ? ? ? ? ? ? ? .creatAccessor();//創(chuàng)建索引 mapping 和setting//通過model 生成index mapping? 和settingbooleanflag=indexAccessor.createIndexWithSettings(TestModel.class);//添加一條數(shù)據(jù)TestModelmodel=newTestModel();? ? ? ? model.setPost_id(1);? ? ? ? model.setCreatetime(newDate().toString());? ? ? ? model.setIs_delete(1);? ? ? ? model.setPost_title("測(cè)試啊");booleanflagAdd=accessor.add(model);//添加多條數(shù)據(jù)、ListtestModelList=newArrayList<>();for(inti=0; i<=10; i++) {TestModelmodel2=newTestModel();? ? ? ? ? ? model2.setPost_id(i);//主鍵不要重復(fù) 重復(fù)的做updata操作model2.setCreatetime(newDate().toString());? ? ? ? ? ? model2.setIs_delete(1);? ? ? ? ? ? model2.setPost_title("測(cè)試啊");? ? ? ? ? ? testModelList.add(model2);? ? ? ? }booleanflagAddList=accessor.add(testModelList);//刪除一條 id 為1的數(shù)據(jù)accessor.delete("1",TestModel.class);//詳情見IAccessor 接口}? ? ? ? }

```

2. es搜索

```

publicclassTestSearch{publicstaticvoidmain(String[]args) {BoolQueryBuilderrootBuilder=QueryBuilders.boolQuery();//搜索商品名稱 名稱進(jìn)行ik分詞+商品skuMapfieldsMap=newHashMap<>();? ? ? ? ? ? fieldsMap.put("product_name.ik", (float)2);? ? ? ? ? ? fieldsMap.put("product_code", (float)1);QueryStringQueryBuilderfieldsMapBuilder=newQueryStringQueryBuilder(keyWord);? ? ? ? ? ? fieldsMapBuilder.fields(fieldsMap);//創(chuàng)建過濾構(gòu)造builderBoolQueryBuilderfilterbuilder=QueryBuilders.boolQuery();//添加過濾參數(shù) 未刪除的已經(jīng)上架的商品filterbuilder.must(QueryBuilders.termQuery("is_delete",0));? ? ? ? ? ? filterbuilder.must(QueryBuilders.termQuery("seller_status",1));//構(gòu)造總體查詢r(jià)ootBuilder.filter(filterbuilder);? ? ? ? ? ? rootBuilder.should(fieldsMapBuilder);//創(chuàng)建QueryBuilderCondition 構(gòu)建整體的查詢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());? ? ? ? ? ? }//查詢的數(shù)據(jù)listresult.getList();? ? result.getSearchHits().getTotalHits()? ? }? ? }

```

3. 數(shù)據(jù)實(shí)體類

```

importannotations.Document;importannotations.Field;importannotations.ID;importdata.FieldType;/***@Author:yang*@Date:2018/3/8.18:15*@Desc:es 建索引實(shí)體類*/@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;? ? }}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末狐史,一起剝皮案震驚了整個(gè)濱河市皆撩,隨后出現(xiàn)的幾起案子国觉,更是在濱河造成了極大的恐慌卿闹,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蝎困,居然都是意外死亡马僻,警方通過查閱死者的電腦和手機(jī)庄拇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來韭邓,“玉大人措近,你說我怎么就攤上這事∨纾” “怎么了瞭郑?”我有些...
    開封第一講書人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)鸭你。 經(jīng)常有香客問我屈张,道長(zhǎng),這世上最難降的妖魔是什么苇本? 我笑而不...
    開封第一講書人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任袜茧,我火速辦了婚禮,結(jié)果婚禮上瓣窄,老公的妹妹穿的比我還像新娘笛厦。我一直安慰自己,他們只是感情好俺夕,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開白布裳凸。 她就那樣靜靜地躺著,像睡著了一般劝贸。 火紅的嫁衣襯著肌膚如雪姨谷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評(píng)論 1 291
  • 那天映九,我揣著相機(jī)與錄音梦湘,去河邊找鬼。 笑死件甥,一個(gè)胖子當(dāng)著我的面吹牛捌议,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播引有,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼瓣颅,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了譬正?” 一聲冷哼從身側(cè)響起宫补,我...
    開封第一講書人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤檬姥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后粉怕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體健民,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年斋荞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了荞雏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡平酿,死狀恐怖凤优,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜈彼,我是刑警寧澤筑辨,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站幸逆,受9級(jí)特大地震影響棍辕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜还绘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一楚昭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拍顷,春花似錦抚太、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至踏揣,卻和暖如春庆亡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捞稿。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來泰國(guó)打工又谋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人娱局。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓彰亥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親铃辖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子剩愧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容