ElasticSearch教程——Java進行搜尋
阿新 • • 發佈:2018-12-10
pom.xml
<?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.elasticsearch</groupId> <artifactId>elasticSearch</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>elasticSearch</name> <description>Demo project for elasticsearch</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <elsaticsearch.version>6.4.0</elsaticsearch.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> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>${elsaticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${elsaticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>${elsaticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elsaticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elsaticsearch.version}</version> <type>pom</type> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
HandleDocument.java
package com.gwd.elasticsearch.test; import java.net.InetAddress; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.transport.client.PreBuiltTransportClient; /** * Description: * 作者:gu.weidong(Jack) * date:2018年9月18日 * ProjectName:elasticSearch */ public class HandleDocument{ private static TransportClient client = null; public static void getResult() throws Exception{ SearchResponse response = getClient().prepareSearch("blog","index")//建立查詢索引,引數productindex表示要查詢的索引庫為blog、index .setTypes("article") //設定type .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)//設定查詢型別 1.SearchType.DFS_QUERY_THEN_FETCH = 精確查詢 2.SearchType.SCAN =掃描查詢,無序 .setQuery(QueryBuilders.termQuery("content", "today")) //設定查詢項以及對應的值 // .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // 設定Filter過濾 .setFrom(0).setSize(60)//設定分頁 .setExplain(true) //設定是否按查詢匹配度排序 // .addSort("id", SortOrder.DESC)//設定按照id排序 .execute() .actionGet(); SearchHits hits = response.getHits(); System.out.println("總數:"+hits.getTotalHits()); for(SearchHit hit: hits.getHits()) { if(hit.getSourceAsMap().containsKey("title")) { System.out.println("source.title: " + hit.getSourceAsMap().get("title")); } } System.out.println(response.toString()); closeClient(); } // 獲取客戶端 public static TransportClient getClient() throws Exception{ Settings settings = Settings.builder() .put("cluster.name", "elasticsearch").build(); client = new PreBuiltTransportClient(settings). addTransportAddress(new TransportAddress(InetAddress.getByName("132.232.38.38"), 9300)); return client; } // 關閉客戶端 public static void closeClient(){ if (client != null){ client.close(); } } public static void main(String[] args) throws Exception { getResult(); } }
返回結果
總數:2 source.title: New version of Elasticsearch released! source.title: New version of Elasticsearch released! { "took": 7, "timed_out": false, "_shards": { "total": 10, "successful": 10, "skipped": 0, "failed": 0 }, "_clusters": { "total": 0, "successful": 0, "skipped": 0 }, "hits": { "total": 2, "max_score": 0.61827284, "hits": [{ "_shard": "[blog][1]", "_node": "xDV-GTebTRqc7es0hDR4rQ", "_index": "blog", "_type": "article", "_id": "eTmX5mUBtZGWutGW0TNs", "_score": 0.61827284, "_source": { "title": "New version of Elasticsearch released!", "content": "Version 1.0 released today!", "priority": 10, "tags": ["announce", "elasticsearch", "release"] }, "_explanation": { "value": 0.61827284, "description": "weight(content:today in 0) [PerFieldSimilarity], result of:", "details": [{ "value": 0.61827284, "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:", "details": [{ "value": 0.47000363, "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:", "details": [{ "value": 2.0, "description": "docFreq", "details": [] }, { "value": 3.0, "description": "docCount", "details": [] }] }, { "value": 1.3154639, "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:", "details": [{ "value": 1.0, "description": "termFreq=1.0", "details": [] }, { "value": 1.2, "description": "parameter k1", "details": [] }, { "value": 0.75, "description": "parameter b", "details": [] }, { "value": 9.666667, "description": "avgFieldLength", "details": [] }, { "value": 4.0, "description": "fieldLength", "details": [] }] }] }] } }, { "_shard": "[blog][3]", "_node": "xDV-GTebTRqc7es0hDR4rQ", "_index": "blog", "_type": "article", "_id": "1", "_score": 0.61827284, "_source": { "id": "1", "title": "New version of Elasticsearch released!", "content": "Version 1.0 released today!", "priority": 10, "tags": ["announce", "elasticsearch", "release"] }, "_explanation": { "value": 0.61827284, "description": "weight(content:today in 0) [PerFieldSimilarity], result of:", "details": [{ "value": 0.61827284, "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:", "details": [{ "value": 0.47000363, "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:", "details": [{ "value": 2.0, "description": "docFreq", "details": [] }, { "value": 3.0, "description": "docCount", "details": [] }] }, { "value": 1.3154639, "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:", "details": [{ "value": 1.0, "description": "termFreq=1.0", "details": [] }, { "value": 1.2, "description": "parameter k1", "details": [] }, { "value": 0.75, "description": "parameter b", "details": [] }, { "value": 9.666667, "description": "avgFieldLength", "details": [] }, { "value": 4.0, "description": "fieldLength", "details": [] }] }] }] } }] } }