Commit 7738fbe7 authored by 杨林's avatar 杨林

ES搜索工具

parent d169fec1
package tools.es
import org.elasticsearch.action.delete.{DeleteRequest, DeleteResponse}
import org.elasticsearch.action.get.{GetRequest, GetResponse}
import org.elasticsearch.action.index.{IndexRequest, IndexResponse}
import org.elasticsearch.action.search.SearchRequest
import org.elasticsearch.action.update.{UpdateRequest, UpdateResponse}
import org.elasticsearch.client.indices.GetIndexRequest
import org.elasticsearch.client.{RequestOptions, RestHighLevelClient}
import org.elasticsearch.common.unit.{Fuzziness, TimeValue}
import org.elasticsearch.common.xcontent.XContentType
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.builder.SearchSourceBuilder
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder
import org.elasticsearch.search.sort.SortOrder
import org.elasticsearch.search.{SearchHit, SearchHits}
import tools.properties.PropertiesTools
import java.io.IOException
import java.util.Properties
import java.util.concurrent.TimeUnit
object ESUtils {
private[this] val sourceBuilder = new SearchSourceBuilder
private[this] var searchRequest: SearchRequest = _
private[this] val client: RestHighLevelClient = ESClient.getClient
private[this] val properties: Properties = PropertiesTools.getProperties
private[this] val indexName: String = properties.getProperty("es.indices")
//增
/**
* 添加数据到ES,指定索引名和id
*
* @param id id
* @param content 要添加的内容,格式为json
*/
def addOne(id: String,
content: String): String = {
val indexRequest = new IndexRequest(indexName)
indexRequest.id(id)
indexRequest.source(content, XContentType.JSON)
var response: IndexResponse = null
try {
response = client.index(indexRequest, RequestOptions.DEFAULT)
} catch {
case e: IOException => e.printStackTrace()
}
response.getResult.toString
}
//删
/**
* 删除索引
*
* @return 是否删除成功
*/
def deleteIndex(): String = {
val deleteRequest = new DeleteRequest(indexName)
val response = client.delete(deleteRequest, RequestOptions.DEFAULT)
response.getResult.toString
}
/**
* 删除指定的数据
*
* @param id 数据id
* @return 删除结果
*/
def deleteDoc(id: String): String = {
val request = new DeleteRequest(indexName, id)
val response: DeleteResponse = client.delete(request, RequestOptions.DEFAULT)
response.getResult.toString
}
//改
/**
* 修改指定文件的数据
*
* @param id 指定id
* @param content 修改的内容
* @return 返回修改结果
*/
def updateIndex(id: String, content: String, routing: String): String = {
val request = new UpdateRequest(indexName, id).doc(content, XContentType.JSON).routing(routing)
var response: UpdateResponse = null
try {
response = client.update(request, RequestOptions.DEFAULT)
response.getResult.toString
} catch {
case exception: Exception => ""
}
}
//查
/**
* 查询指定的索引是否存在
*
* @return 返回是否存在
*/
def indexExists(): Boolean = {
var exits: Boolean = false
try {
val request: GetIndexRequest = new GetIndexRequest(indexName)
exits = client.indices.exists(request, RequestOptions.DEFAULT)
} catch {
case e: Exception => e.printStackTrace()
}
exits
}
/**
* 通过id获取一个文档
*
* @param id id
* @return 搜索结果
*/
def getById(id: String): GetResponse = {
val getRequest = new GetRequest(indexName, id)
client.get(getRequest, RequestOptions.DEFAULT)
}
/**
* 无条件查询指定索引
*
* @param size 取出多少条数据
* @return 返回长度为size的搜索结果数组
*/
def searchAll(size: Int): Array[SearchHit] = {
if (!indexExists) {
return null
}
searchRequest = new SearchRequest(indexName)
//构建查询
val queryBuilder = QueryBuilders.matchAllQuery()
//查询
sourceBuilder
.query(queryBuilder)
.timeout(new TimeValue(60, TimeUnit.SECONDS))
.size(size)
searchRequest.source(sourceBuilder)
//搜索
val response = client.search(searchRequest, RequestOptions.DEFAULT)
response.getHits.getHits
}
/**
* 根据字段精准匹配 term
*
* @param fieldName 字段名
* @param text 匹配的值
* @param size 取出的条数
* @return 返回搜索结果,长度为size的结果数组
*/
def termMatching(fieldName: String, text: AnyRef): SearchHits = {
if (!indexExists) {
return null
}
searchRequest = new SearchRequest(indexName)
val termQueryBuilder = QueryBuilders.termQuery(fieldName, text)
sourceBuilder.query(termQueryBuilder)
searchRequest.source(sourceBuilder)
val response = client.search(searchRequest, RequestOptions.DEFAULT)
response.getHits
}
/**
* 根据指定索引字段模糊匹配
*
* @param fieldName 字段名字
* @param text 匹配文本
* @param size 取出的长度
* @param sortField 排序字段
* @param sortOrder 排序方式
* 升序:SortOrder.ASC,
* 降序:SortOrder.DESC
* @return 返回size长度的结果数组
*/
def fuzzyMatching(fieldName: String, text: AnyRef, size: Int, sortField: String, sortOrder: SortOrder): Array[SearchHit] = {
if (!indexExists) {
return null
}
searchRequest = new SearchRequest(indexName)
val matchQueryBuilder = QueryBuilders.matchQuery(fieldName, text)
.fuzziness(Fuzziness.AUTO)
.prefixLength(3)
.maxExpansions(10)
sourceBuilder.query(matchQueryBuilder)
.timeout(new TimeValue(60, TimeUnit.SECONDS))
.size(size)
searchRequest.source(sourceBuilder)
val response = client.search(searchRequest, RequestOptions.DEFAULT)
response.getHits.getHits
}
/**
* 根据指定的多字段进行模糊匹配
*
* @param size 取出的长度
* @param text 匹配的文本
* @param fieldNames 字段名
* @return 返回结果的数组
*/
def multiFuzzyMatching(size: Int, text: Any, fieldNames: String*): SearchHits = {
// 多字段匹配
if (!indexExists) {
return null
}
searchRequest = new SearchRequest(indexName)
val multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(text, fieldNames: _*)
.fuzziness(Fuzziness.AUTO)
.maxExpansions(10)
.prefixLength(3)
sourceBuilder.query(multiMatchQueryBuilder).size(size)
searchRequest.source(sourceBuilder)
val response = client.search(searchRequest, RequestOptions.DEFAULT)
response.getHits
}
//复合查询
def boolQuery(source: String,
name: String,
price: Double,
value: Any,
size: Int): SearchHits = {
searchRequest = new SearchRequest(indexName)
val boolQueryBuilder = QueryBuilders.boolQuery
//精准查询source字段的值,filedName写‘source’,source写‘淘宝’、‘拼多多’……
.must(QueryBuilders.matchQuery("source", source))
//
.must(QueryBuilders.rangeQuery("price").gte(price * 0.8))
//匹配item_name字段的值
.should(QueryBuilders.matchQuery(name, value))
sourceBuilder.query(boolQueryBuilder).size(size)
searchRequest.source(sourceBuilder)
val highlightBuilder = new HighlightBuilder
highlightBuilder.field("item_name", 10)
.preTags("<")
.postTags(">")
sourceBuilder.highlighter(highlightBuilder)
val response = client.search(searchRequest, RequestOptions.DEFAULT)
response.getHits
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment