実験環境
まず第一に、もちろん、ElasticSearch環境をインストールする必要があります。ビジュアルプラグインElasticsearch-Headをインストールして、データを直感的に表示できるようにすることをお勧めします。
もちろん、この部分での私の投稿を参照できます:「Centos7の弾性検索のインストールと充填ピット」
私のESはhttp://113.209.119.170:9200/にインストールされています(このアドレスはスプリングブートプロジェクトに割り当てる必要があります)
春のプロジェクトの作成
この部分には特別な説明はありませんが、注意すべきことがいくつかあります。
新しいプロジェクトを作成するときは、WebとNoSQLのElasticSearchの依存関係を確認し、写真を撮って説明してみましょう。
プロジェクトを作成するときは、NOSQLのES依存関係オプションを確認してください。
プロジェクトが自動的に生成された後、Spring-Boot-Starter-Data-Elasticsearchの依存性がpom.xmlに自動的に追加されます。
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-data-elasticsearch </artifactid> </dependency>
このプロジェクトでは、オープンソースのRestful ES JavaクライアントJestを使用するため、pom.xmlにJest依存関係を追加する必要があります。
<Dependency> <GroupId> io.searchbox </groupId> <artifactid> jest </artifactid> </dependency>
さらに、JNA依存関係を追加する必要があります。
<Dependency> groupId> net.java.dev.jna </groupid> <artifactid> jna </artifactid> </dependency>
それ以外の場合、Springプロジェクトを開始するとき、JNAのエラーが見つかりません。ネイティブの方法は無効になります。
JNAが見つかりません。ネイティブの方法は無効になります。
ESサーバーアドレスは、プロジェクト構成ファイルapplication.ymlで構成する必要があります
サーバー:ポート:6325SPRING:ElasticSearch:Jest:URIS:-http://113.209.119.170:9200#esserのアドレス!読み取り時間:5000
コード組織
私のプロジェクトコードは次のように整理されています。
プロジェクトコード組織
コードの各部分は次のように説明されており、コメントは次のとおりです。
entity.java
パッケージcom.hansonwang99.springboot_es_demo.entity; Import java.io.serializable; import org.springframework.data.elasticsearch.annotations.document;パブリッククラスエンティティはシリアル化可能{プライベート統計統計統計統計統計統計統計統計統計統計統計統計統計統計統計統計統計統計統計{ public static final string index_name = "index_entity"; public static final string type = "tstype";プライベートロングID;プライベート文字列名; public entity(){super(); } public Entity(long id、string name){this.id = id; this.name = name; } public long getId(){return id; } public void setid(long id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; }} testservice.java
パッケージcom.hansonwang99.springboot_es_demo.service;インポートcom.hansonwang99.springboot_es_demo.entity.entity; import java.util.list; public interface testservice {void saveentity(Entity Entity); void saveentity(list <entity> entityList); List <Entity> SearchEntity(String SearchContent);}testserviceimpl.java
パッケージcom.hansonwang99.springboot_es_demo.service.impl; Import java.io.ioexception; Import java.util.list; import com.hansonwang99.springboot_es_demo.entity; entity; import com.hansonwang99.springBoot_ES_ES_ES_DEMO.SERVICE.TERVICE.TERVICE.TEMO.SERVICE org.elasticsearch.index.query.querybuilders; import org.elasticsearch.search.builder.searchsourcebuilder; Import org.slf4j.logger; import org.slf.slf4j.loggeractory; import org.springframework.beans.beans.beans.beans.beans.beans.beans.beans.beans.beans.beans.beans.beans.beans. springframework.stereotype.service; Import io.searchbox.client.jestclient; import io.searchbox.client.jestresult; import io.searchbox.core.bulk; import io.searchbox.core.index; import io.searchbox.core.searchpublice extublice extublice upublic static final logger logger = loggerfactory.getLogger(testserviceimpl.class); @autowired private jestclient jestclient; @Override public void SaveEntity(Entity Entity){index = new index.builder(entity).index(entity.index_name).type(entity.type).build(); {jestclient.execute(index); logger.info( "es insert complete"); } catch(ioexception e){e.printstacktrace(); logger.error(e.getmessage()); }} / ** *バッチでコンテンツをES * / @Override public void SaveEntity(list <Entity> entityList){bulk.builder bulk = new bulk.builder(); for(entity entity:entityList){index = new index.builder(entity).index(entity.index_name).type(entity.type).build(); bulk.addaction(index); } try {jestclient.execute(bulk.build()); logger.info( "es insert complete"); } catch(ioexception e){e.printstacktrace(); logger.error(e.getmessage()); }} / *** ess* / @override public list <Entity> SearchEntity(String SearchContent){SearchSourceBuilder SearchSourceBuilder = new SearchSourceBuilder(); //SearchSourceBuilder.Query(QueryBuilders.QueryStringQuery(SearchContent)); //searchsourcebuilder.field("name "); SearchSourceBuilder.Query(queryBuilders.Matchquery( "name"、searchContent));検索検索= new Search.Builder(searchSourceBuilder.ToString()).AddindEx(Entity.index_name).addtype(entity.type).build(); try {jestresult result = jestclient.execute(search); return result.getSourceasObjectList(entity.class); } catch(ioException e){logger.error(e.getmessage()); e.printstacktrace(); } nullを返します。 }} EntityController.java
パッケージcom.hansonwang99.springboot_es_demo.controller; import java.util.arraylist; Import java.util.list; Import com.hansonwang99.springboot_es_demo.entity; entity; Import com.hansonwang99.springBoot_ES_ES_ES_ES_DEMO.TERVICE.TEMO.SERVICE.TEMO.SPRINGBOOT_ESBOOT_DEMO.SERVICE org.apache.commons.lang.stringutils; Import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; Import org.springframework.web.bind.annotation.Requestmethod; Import; org.springframework.web.bind.annotation.restcontroller;@restcontroller@requestmapping( "/entitycontroller")public class entitycontroller {@autowired testService CityEsservice; @RequestMapping(value = "/save"、method = requestmethod.get)public string save(long id、string name){system.out.println( "save interface"); if(id> 0 && stringutils.isnotempty(name)){entity newEntity = new Entity(id、name); List <Entity> addList = new ArrayList <Entity>(); addlist.add(newEntity); CityEsservice.SaveEntity(AddList); 「OK」を返します。 } else {return "bad input値"; }} @requestMapping(value = "/search"、method = requestmethod.get)public list <entity> save(string name){list <Entity> entityList = null; if(stringutils.isnotempty(name)){entityList = CityEsservice.SearchEntity(name); } return entitityList; }}実際の実験
いくつかのデータを追加するには、Postmanツールを使用するか、次の5つのデータを追加するなど、ブラウザに直接入力できます。
http:// localhost:6325/entitycontroller/save?id = 1&name = nanjing sun yat-sen mausoleum http:// localhost:6325/entitycontroller/save?id = 2&name = nanjing normal normal in http:// localhost:6325/entiitycontroller孔子寺院http:// localhost:6325/entitycontroller/save?id = 4&name = hangzhouも非常に良いhttp:// localhost:6325/entitycontroller/save?id = 5&name =中国南部には都市と呼ばれる都市はないようです。
データ挿入効果は次のとおりです(Visual Plug-in Elasticsearch-Headを使用):
データ挿入効果
検索テストをしましょう:たとえば、キーワード「南京」を検索したい
ブラウザを入力します。
http:// localhost:6325/entitycontroller/search?name = nanjing
検索結果は次のとおりです。
キーワード「南京」の検索結果
キーワード「南京」が挿入された4つのレコードはすべて、今すぐ検索されました!
もちろん、標準の単語セグメンテーション方法はここで使用され、各中国語は用語として使用されます。キーワード「南」と「北京」を含むすべてのレコードは検索されていますが、スコアは異なります。もちろん、他の単語セグメンテーション方法があります。現時点では、他の単語セグメンテーションプラグインのサポートが必要です。これはここではカバーされていません。後で探索します。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。