ElasticSearch基於Lucene的搜索服務器,支持分布式,提供REST接口,可用於雲計算,可以實現實時搜索,開源免費。這時很官方的一句話,在使用之前,我們簡單的介紹一下安裝過程。在官網下載之后,直接解壓出來,找到bin文件夾下,運行elasticsearch.bat,運行后的結果如下:
程序的連接端口是9300,服務器的管理端口是9200。運行之后看結果,則在瀏覽器上輸入127.0.0.1:9200,顯示如下:
這樣表示es已啟動,為了更加直觀,可以安裝插件,即圖形化管理工具:下載esHead插件,解壓之后,放到 es的plugins文件夾下,文件名為head即可,后重啟服務器,輸入http://127.0.0.1:9200/_plugin/head,出現如下界面表示安裝插件成功:
安裝完成之后,下面簡單介紹索引的創建和刪除:
首先創建maven工程,引入相應的坐標:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.es.demo</groupId> <artifactId>elaticsearchdemo</artifactId> <version>1.0-SNAPSHOT</version> <name>elaticsearchdemo</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!-- ElasticSearch core --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.4.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.1</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
然后創建測試類,創建客戶端對象,利用對象創建索引和刪除索引,代碼如下:
創建索引之后,可視化根據中出現:
表示索引成功,刪除索引代碼如上所示,同理。介紹完了索引的創建和刪除,下面看看es的映射規則是怎么操作的: