ElasticSearch搜索引擎的入門實戰


1.ElasticSearch簡介

引用自百度百科:

ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放源碼發布,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。

我們建立一個網站或應用程序,並要添加搜索功能,但是想要完成搜索工作的創建是非常困難的。我們希望搜索解決方案要運行速度快,我們希望能有一個零配置和一個完全免費的搜索模式,我們希望能夠簡單地使用JSON通過HTTP來索引數據,我們希望我們的搜索服務器始終可用,我們希望能夠從一台開始並擴展到數百台,我們要實時搜索,我們要簡單的多租戶,我們希望建立一個雲的解決方案。因此我們利用Elasticsearch來解決所有這些問題及可能出現的更多其它問題。

2.ElasticSearch的安裝

2.1.安裝Jdk1.8
1、查看Linux環境 是否含有JDK 卸載 如果檢查到有安裝就執行卸載命令
  rpm -qa|grep jdk
  卸載命令
  rpm -e --nodeps 軟件名稱  

2、下載jdk
打開網站http://www.oracle.com/technetwork/java/javase/downloads,選擇對應版本JDK,點擊下載

3、本人下載jdk為jdk-8u131-linux-x64.tar.gz 上傳並解壓:
 tar -xvf jdk-8u131-linux-x64.tar.gz -C /usr/local/

4、進入解壓縮目錄

  cd /usr/local

5、修改jdk的文件夾名稱

  mv jdk1.8.0_131  jdk

6、配置環境變量

   修改環境變量配置文件:  vi /etc/profile


   點 i鍵進入編輯模式

   跳轉到最后一行
   增加如下內容

   #java runtime seting
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib:.
export PATH=$JAVA_HOME/bin:$PATH


  按ESC 輸入:wq  保存退出

7、重新加載環境配置

   source /etc/profile

 
8、測試JDK安裝是否ok

  java -version
2.2.安裝elasticsearch-2.4.1

1、下載

wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.1/elasticsearch-2.4.1.tar.gz

2、解壓 注意解壓路徑不能為root

tar -zxvf elasticsearch-2.4.1.tar.gz -C /usr/local

3、配置
找到解壓后的config文件夾打開elasticsearch.yml

關鍵是要打開network.host: 0.0.0.0和http.port: 9200注釋,提供web訪問

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
# 集群名稱,默認為elasticsearch
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 節點名稱,es啟動時會自動創建節點名稱,但你也可進行配置
node.name: node-1
#
# Add custom attributes to the node:
#
# 是否作為主節點,每個節點都可以被配置成為主節點,默認值為true:
# node.master: true
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
# 分配給當前節點的索引數據所在的位置
# path.data: /path/to/data
#
# Path to log files:
# 日志文件所在位置
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.memory_lock: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

4、設置文件可訪問權限

chmod -R 777 elasticsearch-2.4.1

5、注意這里不能直接啟動 用root用戶啟動會報錯 所以我們需要新建一個用戶

useradd admin
su admin

6、啟動 在bin目錄下執行命令(最后可加& 表示在后台運行)

 ./elasticsearch -d 

7、查看內存

執行命令

 ps -ef|grep elasticsearch

ZSI2Je.png

8、查看內存 優化內存

可以看到在內存為2G的主機上,Elasticsearch的運行內存為 -Xms256m -Xmx1g

1.最簡單的一個方法就是指定ES_HEAP_SIZE環境變量。服務進程在啟動時候會讀取這個變量,並相應的設置堆的大小。設置命令如下:

export ES_HEAP_SIZE=1g

9、訪問網頁

因為開啟了http訪問,並設置可以遠程連接,所以我們直接請求網址即可:

http://你的服務器IP:9200

相應內容如下:

{
name: "Man-Spider",
cluster_name: "elasticsearch",
cluster_uuid: "hsuDHGiMQWaML6_xSJN8fw",
version: {
number: "2.4.1",
build_hash: "c67dc32e24162035d18d6fe1e952c4cbcbe79d16",
build_timestamp: "2016-09-27T18:57:55Z",
build_snapshot: false,
lucene_version: "5.5.2"
},
tagline: "You Know, for Search"
}

10、可視化插件

利用在bin目錄下提供的plugin插件安裝head,實現web可視化

注意目錄:

./bin/plugin install mobz/elasticsearch-head

安裝完后訪問

http://你的服務器IP:9200/_plugin/head/

ZSodk8.png

3.使用logstash導入mysql數據到elasticSearch

Elasticsearch-jdbc工具包(廢棄),雖然是官方推薦的,但是已經幾年不更新了。所以選擇安裝logstash-input-jdbc,首選 logstash-input-jdbc,logstash5.X開始,已經至少集成了logstash-input-jdbc插件。所以,你如果使用的是logstash5.X,可以不必再安裝,可以直接跳過這一步。

3.1.下載mysql-jdbc-driver.jar

下載地址:

 jdbc連接mysql驅動的文件目錄,可去官網下載:https://dev.mysql.com/downloads/connector/j/

此處我使用的jar的版本為:mysql-connector-java-5.1.46.jar.后面我們會把這個jar包放在logstash的config目錄下面

3.2.下載logstash-6.3.0

下載命令:

sudo wget  https://artifacts.elastic.co/downloads/logstash/logstash-6.3.0.zip

解壓命令:

yum install -y unzip
unzip logstash-6.3.0.zip
3.3.elasticSearch與數據庫表的對應關系
ES MYSQL
索引 數據庫
類型 數據表
文檔 數據表的一行
屬性 數據表的一列
3.4.建立測試數據表

sql語句如下:

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '調度', '12', '2019-06-13 19:24:55');
INSERT INTO `student` VALUES ('2', '李四', '13', '2019-06-13 19:24:55');
INSERT INTO `student` VALUES ('3', '王五', '15', '2019-06-13 19:24:55');
INSERT INTO `student` VALUES ('4', '趙六', '18', '2019-06-13 21:01:32');
INSERT INTO `student` VALUES ('5', '的地方', '52', '2019-06-13 21:13:51');
INSERT INTO `student` VALUES ('6', '測試', '45', '2019-06-13 21:17:31');

在logstatsh的目錄下面建立my_logstash文件夾,里面建立myjdbc.conf:(這個僅供參考 實際不使用)

input {
  jdbc {
    # mysql相關jdbc配置
    jdbc_connection_string => "jdbc:mysql://IP:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"
    jdbc_user => "****"
    jdbc_password => "****"

    # jdbc連接mysql驅動的文件目錄,可去官網下載:https://dev.mysql.com/downloads/connector/j/
    jdbc_driver_library => "./config/mysql-connector-java-5.1.46.jar"
    # the name of the driver class for mysql
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_paging_enabled => true
    jdbc_page_size => "50000"

    jdbc_default_timezone =>"Asia/Shanghai"

    # mysql文件, 也可以直接寫SQL語句在此處,如下:
    #statement => "select * from student where update_time >= :sql_last_value"
     statement => "select * from student"
    # statement_filepath => "./config/jdbc.sql"

    # 這里類似crontab,可以定制定時操作,比如每分鍾執行一次同步(分 時 天 月 年)
    schedule => "* * * * *"
    #type => "jdbc"

    # 是否記錄上次執行結果, 如果為真,將會把上次執行到的 tracking_column 字段的值記錄下來,保存到      last_run_metadata_path 指定的文件中
    #record_last_run => true

    # 是否需要記錄某個column 的值,如果record_last_run為真,可以自定義我們需要 track 的 column 名稱,此時該參數就要為 true. 否則默認 track 的是 timestamp 的值.
    use_column_value => true

    # 如果 use_column_value 為真,需配置此參數. track 的數據庫 column 名,該 column 必須是遞增的. 一般是mysql主鍵
    tracking_column => "update_time"

    tracking_column_type => "timestamp"

    last_run_metadata_path => "./logstash_capital_bill_last_id"

    # 是否清除 last_run_metadata_path 的記錄,如果為真那么每次都相當於從頭開始查詢所有的數據庫記錄
    clean_run => false

    #是否將 字段(column) 名稱轉小寫
    lowercase_column_names => false
  }
}

output {
  elasticsearch {
    hosts => "192.168.142.128:9200"
    index => "resource"
    document_id => "%{id}"
    template_overwrite => true
  }

  # 這里輸出調試,正式運行時可以注釋掉
  stdout {
      codec => json_lines
  } 
}

在logstash的config目錄下面新建logstash-mysql.conf:

input {
  jdbc {
    jdbc_driver_library => "/usr/local/logstash-6.3.0/config/mysql-connector-java-5.1.46.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://192.168.142.128:3306/test?characterEncoding=UTF-8&useSSL=false"
    jdbc_user => "root"
    jdbc_password => "root"
    statement => "SELECT * FROM student WHERE update_time > :sql_last_value"
    jdbc_paging_enabled => "true"
    jdbc_page_size => "50000"
    schedule => "* * * * *"
  }
}

filter {
   json {
        source => "message"
        remove_field => ["message"]
    }
}

output {
  stdout {
    codec => rubydebug
  }
  elasticsearch {
    hosts => "192.168.142.128:9200"   
    index => "test"
  }        
}

啟動命令:

 /usr/local/logstash-6.3.0/bin/logstash -f /usr/local/logstash-6.3.0/config/logstash-mysql.conf

啟動成功后界面如下,開始導入數據:

Zu0wTI.png

訪問地址:http://192.168.142.131:9200/_plugin/head/

ZuDmZ9.png

ZuDMPx.png

4.查詢語句相關

URL查詢:
CURL GET http://192.168.142.131:9200/索引(test)/_search?q = name:李
響應:
{
    took: 5,
    timed_out: false,
    _shards: {
    total: 5,
    successful: 5,
    failed: 0
    },
    hits: {
    total: 6,
    max_score: 1,
    hits: [
            {
    _index: "test",
    _type: "doc",
    _id: "AWueHx85GWxHTP0ts5i7",
    _score: 1,
    _source: {
    name: "李四",
    id: 2,
    update_time: "2019-06-13T10:24:55.000Z",
    @version: "1",
    age: 13,
    @timestamp: "2019-06-28T12:46:04.976Z"
                }
            },
            {
    _index: "test",
    _type: "doc",
    _id: "AWueHx85GWxHTP0ts5i9",
    _score: 1,
    _source: {
    name: "趙六",
    id: 4,
    update_time: "2019-06-13T12:01:32.000Z",
    @version: "1",
    age: 18,
    @timestamp: "2019-06-28T12:46:04.977Z"
                }
            },
            {
    _index: "test",
    _type: "doc",
    _id: "AWueHx85GWxHTP0ts5i8",
    _score: 1,
    _source: {
    name: "王五",
    id: 3,
    update_time: "2019-06-13T10:24:55.000Z",
    @version: "1",
    age: 15,
    @timestamp: "2019-06-28T12:46:04.977Z"
                }
            },
            {
    _index: "test",
    _type: "doc",
    _id: "AWueHx85GWxHTP0ts5i_",
    _score: 1,
    _source: {
    name: "測試",
    id: 6,
    update_time: "2019-06-13T12:17:31.000Z",
    @version: "1",
    age: 45,
    @timestamp: "2019-06-28T12:46:04.978Z"
                }
            },
            {
    _index: "test",
    _type: "doc",
    _id: "AWueHx85GWxHTP0ts5i6",
    _score: 1,
    _source: {
    name: "調度",
    id: 1,
    update_time: "2019-06-13T10:24:55.000Z",
    @version: "1",
    age: 12,
    @timestamp: "2019-06-28T12:46:04.962Z"
                }
            },
            {
    _index: "test",
    _type: "doc",
    _id: "AWueHx85GWxHTP0ts5i-",
    _score: 1,
    _source: {
    name: "的地方",
    id: 5,
    update_time: "2019-06-13T12:13:51.000Z",
    @version: "1",
    age: 52,
    @timestamp: "2019-06-28T12:46:04.978Z"
                }
            }
        ]
    }
}
相關參數

如下這些參數可以使用統一資源標識符在搜索操作中傳遞 -

編號 參數 說明
1 Q 此參數用於指定查詢字符串。
2 lenient 基於格式的錯誤可以通過將此參數設置為true來忽略。默認情況下為false
3 fields 此參數用於在響應中選擇返回字段。
4 sort 可以通過使用這個參數獲得排序結果,這個參數的可能值是fieldNamefieldName:ascfieldname:desc
5 timeout 使用此參數限定搜索時間,響應只包含指定時間內的匹配。默認情況下,無超時。
6 terminate_after 可以將響應限制為每個分片的指定數量的文檔,當到達這個數量以后,查詢將提前終止。 默認情況下不設置terminate_after
7 從命中的索引開始返回。默認值為0
8 size 它表示要返回的命中數。默認值為10

5.短語搜索

5.1.短語搜索是ElasticSearch中比較常用的方式,相關的語法個格式為JSON,如下:(這里使用POSTMAN進行演示)
//請求的地址為:
http://192.168.142.131:9200/test/_search

請求體:

{
    "query": {
        "match_phrase": {
            "name": "李"
        }
    }
}

響應:

{
    "took": 26,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.19178301,
        "hits": [
            {
                "_index": "test",
                "_type": "doc",
                "_id": "AWueHx85GWxHTP0ts5i7",
                "_score": 0.19178301,
                "_source": {
                    "name": "李四",
                    "id": 2,
                    "update_time": "2019-06-13T10:24:55.000Z",
                    "@version": "1",
                    "age": 13,
                    "@timestamp": "2019-06-28T12:46:04.976Z"
                }
            }
        ]
    }
}

ZMsrUH.png

5.2.提高精度搜索:

搜索結果精准控制的第一步:靈活使用and關鍵字,如果你是希望所有的搜索關鍵字都要匹配的,那么就用and,可以實現單純match query無法實現的效果

{
    "query": {
        "match_phrase": {
            "name": {
         		 "query": "李",
        		 "operator": "and"
            }
        }
    }
}

控制搜索結果的精准度的第二步:指定一些關鍵字中,必須至少匹配其中的多少個關鍵字,才能作為結果返回

{
    "query": {
        "match_phrase": {
            "name": {
        		"query": "李",
        		"minimum_should_match": "10%"
            }
        }
    }
}

用bool組合多個搜索條件,來搜索name

{
  "query": {
    "bool": {
      "must":     { "match": { "name": "李" }},
      "must_not": { "match": { "name": "張"  }},
      "should": [
                  { "match": { "age": "13" }},
                  { "match": { "name": "四"   }}
      ]
    }
  }
}
  • 注意:bool組合多個搜索條件,如何計算relevance score

    must和should搜索對應的分數,加起來,除以must和should的總數

    排名第一:test,同時包含should中所有的關鍵字,testOne,testTwo
    排名第二:test,同時包含should中的testOne
    排名第三:test,不包含should中的任何關鍵字

      should是可以影響相關度分數的,must是確保說,誰必須有這個關鍵字,同時會根據這個must的條件去計算出document對這個搜索條件的relevance score,在滿足must的基礎之上,should中的條件,不匹配也可以,但是如果匹配的更多,那么document的relevance score就會更高

  • 默認情況下,should是可以不匹配任何一個的,比如上面的搜索中,"李四",就不匹配任何一個should條件,但是有個例外的情況,如果沒有must的話,那么should中必須至少匹配一個才可以,比如下面的搜索,should中有4個條件,默認情況下,只要滿足其中一個條件,就可以匹配作為結果返回,但是可以精准控制,should的4個條件中,至少匹配幾個才能作為結果返回

{
  "query": {
    "bool": {
      "should": [
        { "match": { "name": "李四" }},
        { "match": { "name": "張三"   }},
        { "match": { "name": "王五"   }},
        { "match": { "name": "趙六"   }}
      ],
      "minimum_should_match": 3 
    }
  }
}  
  • 指定返回屬性

    只返回查詢文檔的name和age屬性

{
  "query": {
    "bool": {
      "should": [
        { "match": { "name": "李四" }},
        { "match": { "name": "張三"   }},
        { "match": { "name": "王五"   }},
        { "match": { "name": "趙六"   }}
      ],
      "minimum_should_match": 1
    }
  },
  "_source": [
        "name",
        "age"
    ]
}
  • 高亮搜索
  {
    "query": {
        "bool": {
            "must": {
                "match": {
                    "name": {
                        "query": "李四",
                        "minimum_should_match": "100%"
                    }
                }
            }
        }
    },
    "highlight": {
        "fields": {
            "name": {}
        }
    }
}

返回如下:

{
    "took": 72,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2712221,
        "hits": [
            {
                "_index": "test",
                "_type": "doc",
                "_id": "AWueHx85GWxHTP0ts5i7",
                "_score": 0.2712221,
                "_source": {
                    "name": "李四",
                    "id": 2,
                    "update_time": "2019-06-13T10:24:55.000Z",
                    "@version": "1",
                    "age": 13,
                    "@timestamp": "2019-06-28T12:46:04.976Z"
                },
                "highlight": {
                    "name": [
                        "<em>李</em><em>四</em>"
                    ]
                }
            }
        ]
    }
}

6.使用SpringBoot+ElasticSearch

​ 這里我們需要注意SpringBoot和ElasticSearch的版本號能對應,我們查看官網的介紹進行構建項目:

https://github.com/spring-projects/spring-data-elasticsearch ,主要點如下所示:

Maven configuration

Add the Maven dependency:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>x.y.z.RELEASE</version>
</dependency>

If you’d rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-elasticsearch</artifactId>
  <version>x.y.z.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/libs-snapshot</url>
</repository>
Versions

The following table shows the Elasticsearch versions that are used by Spring Data Elasticsearch:

Spring Data Elasticsearch Elasticsearch
3.2.x 6.7.2
3.1.x 6.2.2
3.0.x 5.5.0
2.1.x 2.4.0
2.0.x 2.2.0
1.3.x 1.5.2

我們這里的elastic的版本為2.4.1,所以我們選取2.4.1.RELEASE:

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.1.14.RELEASE</version>
        </dependency>

然后在application.yml配置一下服務器的elasticsearch地址(注意線上的建立連接監聽的端口為9300,並非9200):

spring:
  data:
    elasticsearch:
      cluster-name: my-application
      cluster-nodes: 服務器Ip:9300

配置好后啟動我們的項目,會打印如下的日志,說明連接成功:

ZlCkY8.png

完整pom文件如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.home</groupId>
    <artifactId>es-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>es-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.1.14.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
入門Demo:

ElasticsearchCRUD介紹網站:https://damienbod.com/2014/10/01/full-text-search-with-asp-net-mvc-jquery-autocomplete-and-elasticsearch/

The article explains how to use the ElasticsearchCRUD NuGet package. ElasticsearchCRUD is designed so that you can do CRUD operations for any entity and insert, delete, update or select single documents from Elasticsearch. The package only includes basic search or query possibilities. (本文解釋了如何使用ElasticsearchCRUD NuGet包。ElasticsearchCRUD的設計使您可以對任何實體執行CRUD操作,並從Elasticsearch中插入、刪除、更新或選擇單個文檔。該包只包含基本的搜索或查詢可能性。 )

6.1.1.我們先基於student表創建一個實體類用於測試
package com.home.esdemo.entity;

import org.springframework.data.elasticsearch.annotations.Document;

import java.util.Date;

/**
 * indexName索引名稱,type類別
 */
@Document(indexName = "test",type = "student")
public class Student {

    private  Integer id;
    private  String name;
    private  Integer age;
    private  Date updateTime;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, Date updateTime) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.updateTime = updateTime;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", updateTime=" + updateTime +
                '}';
    }
}

6.1.2.新建新建ElasticsearchRepository接口
@Repository
public interface StudentRepository extends ElasticsearchRepository<Student, Integer> {
    Student findAllByName(String name);
}

6.1.3.測試
package com.home.esdemo;

import com.home.esdemo.entity.Student;
import com.home.esdemo.esinterface.StudentRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.awt.print.Book;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EsDemoApplicationTests {

    @Autowired
    StudentRepository studentRepository;

    @Test
    public void contextLoads() {
        Student student = new Student(8, "小白", 18, new Date());
        studentRepository.index(student);//保存
        Student name = studentRepository.findAllByName("小"); //查找
        System.out.println(name);
        
        studentRepository.save( new Student(9, "其他", 28, new Date()));
    }

}

}

StudentRepository可以使用如下關鍵字來實現復雜的操作:
關鍵字 使用示例 等同於的ES查詢
And findByNameAndPrice {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Or findByNameOrPrice {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Is findByName {“bool” : {“must” : {“field” : {“name” : “?”}}}}
Not findByNameNot {“bool” : {“must_not” : {“field” : {“name” : “?”}}}}
Between findByPriceBetween {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
LessThanEqual findByPriceLessThan {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
GreaterThanEqual findByPriceGreaterThan {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Before findByPriceBefore {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
After findByPriceAfter {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Like findByNameLike {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
StartingWith findByNameStartingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
EndingWith findByNameEndingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}}
Contains/Containing findByNameContaining {“bool” : {“must” : {“field” : {“name” : {“query” : “?”,”analyze_wildcard” : true}}}}}
In findByNameIn(Collectionnames) {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}}
NotIn findByNameNotIn(Collectionnames) {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}}
True findByAvailableTrue {“bool” : {“must” : {“field” : {“available” : true}}}}
False findByAvailableFalse {“bool” : {“must” : {“field” : {“available” : false}}}}
OrderBy findByAvailableTrueOrderByNameDesc {“sort” : [{ “name” : {“order” : “desc”} }],”bool” : {“must” : {“field” : {“available” : true}}}}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM