国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - Java教程 - Java使用強大的Elastisearch搜索引擎實例代碼

Java使用強大的Elastisearch搜索引擎實例代碼

2020-10-08 16:44饑渴計科極客杰鏗 Java教程

本篇文章主要介紹了Java使用強大的Elastisearch搜索引擎實例代碼,具有一定的參考價值,有興趣的可以了解一下

Elastisearch是一個很強大,易用的搜索引擎

在系統上運行Elastisearch只需以下幾步

1.下載Elastisearch

 

復制代碼 代碼如下:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.0.zip

 

 

2.解壓

?
1
unzip elasticsearch-5.4.0.zip

3.運行

?
1
elasticsearch-5.4.0/bin/elasticsearch

這時有可能會直接被Killed掉,因為內存溢出(OOM),elastisearch占用的內存非常大,所以在內存比較小的服務器上運行要先修改jvm的內存大小

?
1
vi elasticsearch-5.4.0/config/jvm.options

將22和23行的棧堆大小改為512M

?
1
2
-Xms512M
-Xmx512M

如果重新啟動后還是killed就再改小一點

4.測試是否成功

?
1
curl  'http://localhost:9200/?pretty'

你能看到以下返回信息:

?
1
2
3
4
5
6
7
8
9
{
      "status":  200,
      "name""Shrunken  Bones",
      "version":  {
            "number""1.4.0",
            "lucene_version""4.10"
      },
      "tagline""You  Know,  for  Search"
}

則表明啟動成功

接下來我們用Java的API來操作Elasticsearch

首先是導入elastisearch和log4j的包

POM

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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.jk</groupId>
  <artifactId>ElasticsearchExample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>transport</artifactId>
      <!--用哪個版本就填什么-->
      <version>5.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.7</version>
    </dependency>
  </dependencies>
 
</project>

以下是常用的幾種操作

1.創建client

?
1
2
3
4
5
6
7
Client client = null;
    try {
      client = new PreBuiltTransportClient(Settings.EMPTY)
          .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    } catch (Exception e) {
      e.printStackTrace();
    }

2.創建索引

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
   * 創建索引,有則先刪除
   * @param client
   */
  private static void recreateIndex(Client client) {
    if (client.admin().indices().prepareExists(index).execute().actionGet()
        .isExists()) {
      DeleteIndexResponse deleteIndexResponse = client.admin().indices()
          .delete(new DeleteIndexRequest(index)).actionGet();
      System.out.println("delete index :");
      System.out.println(deleteIndexResponse);
    }
 
    CreateIndexResponse createIndexResponse = client.admin().indices()
        .prepareCreate(index).execute().actionGet();
    System.out.println("create index :");
    System.out.println(createIndexResponse);
  }

3.插入數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
   * 插入數據
   * @param client
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  private static void doIndex(final Client client) {
    Map s11 = new LinkedHashMap();
    s11.put("title", "Think in java");
    s11.put("origin", "美國");
    s11.put("description", "初級java開發人員必讀的書");
    s11.put("author", "Bruce Eckel");
    s11.put("price", 108);
 
    Map s12 = new LinkedHashMap();
    s12.put("title", "Head First Java");
    s12.put("origin", "英國");
    s12.put("description", "java入門教材");
    s12.put("author", "Kathy Sierra");
    s12.put("price", 54);
 
    Map s21 = new LinkedHashMap();
    s21.put("title", "Design Pattern");
    s21.put("origin", "法國");
    s21.put("description", "程序員不得不讀的設計模式");
    s21.put("author", "Kathy Sierra");
    s21.put("price", 89);
 
    Map s22 = new LinkedHashMap();
    s22.put("title", "黑客與畫家");
    s22.put("origin", "法國");
    s22.put("description", "讀完之后腦洞大開");
    s22.put("author", "Paul Graham");
    s22.put("price", 35);
 
    BulkResponse bulkResponse = client
        .prepareBulk()
        .add(client.prepareIndex(index, type).setId("11").setSource(s11).setOpType(IndexRequest.OpType.INDEX).request())
        .add(client.prepareIndex(index, type).setId("12").setSource(s12).setOpType(IndexRequest.OpType.INDEX).request())
        .add(client.prepareIndex(index, type).setId("21").setSource(s21).setOpType(IndexRequest.OpType.INDEX).request())
        .add(client.prepareIndex(index, type).setId("22").setSource(s22).setOpType(IndexRequest.OpType.INDEX).request())
        .execute().actionGet();
    if (bulkResponse.hasFailures()) {
      System.err.println("index docs ERROR:" + bulkResponse.buildFailureMessage());
    } else {
      System.out.println("index docs SUCCESS:");
      System.out.println(bulkResponse);
    }
  }

4.查詢所有

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 查詢所有
 */
private static void searchAll(Client client) {
  SearchResponse response = client.prepareSearch(index)
      .setQuery(QueryBuilders.matchAllQuery())
      .setExplain(true).execute().actionGet();
  System.out.println("searchAll : ");
  for (SearchHit searchHit : response.getHits()) {
    System.out.println("********");
    System.out.println(searchHit.getSource());
  }
}

5.關鍵詞查詢

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /**
   * 關鍵詞查詢
   *
   * @param client
   */
  private static void searchKeyWord(Client client) {
    SearchResponse response = client.prepareSearch(index)
        //查詢所有字段匹配關鍵字
        .setQuery(QueryBuilders.matchQuery("_all", "法國"))
        //設置最小匹配程度
//        .setQuery(QueryBuilders.matchQuery("_all", "法國").minimumShouldMatch("100%"))
        .execute().actionGet();
    System.out.println("searchKeyWord : ");
    System.out.println(response);
  }

6.數值范圍過濾

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 數值范圍過濾
 *
 * @param client
 */
private static void searchRange(Client client) {
  SearchResponse response = client.prepareSearch(index).
      //大于80,小于100
      setQuery(QueryBuilders.rangeQuery("price").gt(80).lt(100))
      .execute()
      .actionGet();
  System.out.println("searchRange : ");
  System.out.println(response);
}

7.排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 排序
 *
 * @param client
 */
private static void searchOrdered(Client client) {
  SearchResponse response = client.prepareSearch(index)
      .setQuery(QueryBuilders.matchAllQuery())
      //根據價格降序排序
      .addSort(SortBuilders.fieldSort("price")
          .order(SortOrder.DESC)).execute().actionGet();
  System.out.println("searchOrdered : ");
  System.out.println(response);
}

8.高亮關鍵字

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
   * 高亮關鍵字
   * @param client
   */
  private static void searchHightlight(Client client) {
    //高亮多個字段
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    highlightBuilder.field("title");
    highlightBuilder.field("description");
    SearchResponse response = client.prepareSearch(index)
        //單條件匹配,高亮時只能高亮該字段
//        .setQuery(QueryBuilders.matchQuery("title", "java"))
        //多條件匹配,高亮時只能高亮多個字段
        .setQuery(QueryBuilders.multiMatchQuery("開發人員必讀", "title", "description"))
        .highlighter(highlightBuilder)
        .execute()
        .actionGet();
    System.out.println("searchHightlight : ");
    System.out.println(response);
  }

9.根據id查找

?
1
2
3
4
5
6
7
8
9
10
/**
  * 根據id查找
  * @param client
  */
 private static void findById(final Client client) {
   String id="12";
   GetResponse response = client.prepareGet(index, type, id).get();
   System.out.println("findById");
   System.out.println(response);
 }

10.刪除

?
1
2
3
4
5
6
7
8
/**
 * 刪除
 * @param client
 */
private static void deleteById(Client client) {
  String id="12";
  DeleteResponse response = client.prepareDelete(index, type, id).get();
}

11.更新

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 更新
 * @param client
 */
private static void updateById(Client client) {
  try {
    String id="11";
    client.prepareUpdate(index, type, id)
        .setDoc(jsonBuilder()
            .startObject()
            .field("title", "白鹿原")
            .endObject())
        .get();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

常用的操作就這些,代碼上傳到https://github.com/jkgeekJack/ElasticsearchExample

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.jianshu.com/p/46c38f352c27

延伸 · 閱讀

精彩推薦
264
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲国产精品成人 | 亚洲一区二区三区精品动漫 | 国产xxxx成人精品免费视频频 | 成人免费xxx在线观看 | 老色批影院| 中文字幕观看 | 91视视频在线观看入口直接观看 | 午夜欧美 | 精品美女 | 91精品久久 | 精品影视 | 国产精品久久久久久久久久免费看 | 中文字幕高清一区 | 日韩欧美在线观看视频 | 最新国产视频 | 在线a视频| 日韩午夜激情视频 | 欧美精品在线一区 | 亚洲天堂一区二区 | 成版人性视频 | 色婷婷狠狠 | 自拍偷拍亚洲欧美 | 国产1级片 | 精品国产91亚洲一区二区三区www | 中文字幕久久精品 | 日本精品在线观看 | 欧美日韩一区在线 | 欧美一区二区三区精品免费 | 99热99| 国产综合视频 | 激情在线观看视频 | 日韩精品一区二区三区在线观看视频网站 | 98久9在线 | 免费 | 国产午夜精品美女视频明星a级 | 久久精品国产亚洲 | 欧美日韩亚洲国产 | 国产精品一区二区三区四区 | 午夜四虎 | 亚洲成人免费电影 | 国产一区二区免费 | 九九九久久久 |