ElasticSearch

获取传输地址对象

java连接9300,rest连接9200
集群名称在直接访问9200端口里

Settings settings = Settings.builder().put("cluster.name", "集群名称").build();
TransportClient transportClient = new PreBuiltTransportClient(settings);
transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("ip地址"), 9300));

//数据操作

transportClient.close();

创建索引库

IndicesAdminClient indicesAdminClient = transportClient.admin().indices();
//创建空索引库
indicesAdminClient.prepareCreate("index").get();

//添加映射(可选),只可以空索引库状态添加
XContentBuilder xContentBuilder = jsonBuilder();
xContentBuilder.startObject()
	.startObject("type")
	.startObject("properties");
xContentBuilder.startObject("字段")
	.field("index",true)//是否建立索引(可以搜索)
	.field("type", "数据类型")
	.field("store", true)//是否储存
	.field("analyzer", "ik_smart")//不分词不写这条
	.endObject();
xContentBuilder.endObject()
	.endObject()
	.endObject();

//映射请求对象
PutMappingRequest putMappingRequest = new PutMappingRequest("index").type("type").source(xContentBuilder);
//添加映射
indices.putMapping(putMappingRequest).get();
//添加映射(可选)

CURD

数据

XContentBuilder: 
XContentFactory.jsonBuilder() 
	.startObject() //{
	.field("key", value) 
	.endObject(); //}

Map

实体类转换为json字符串:
new ObjectMapper().writeValueAsString(article);
要在setSource加上XContentType.JSON参数

添加

单个添加
transportClient.prepareIndex("index", "type", "id").setSource(数据).get();

批量添加
BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
IndexRequest indexRequest = new IndexRequest("index", "type", "id").source(数据);
bulkRequestBuilder.add(indexRequest);
bulkRequestBuilder.get();

修改

transportClient.prepareUpdate("index", "type", "id") .setDoc(数据).get();

删除

transportClient.prepareDelete("index", "type", "id").get();

查询

搜索请求对象

SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("index").setTypes("type");

字符串查询,会分词再查询

SearchResponse searchResponse = searchRequestBuilder.setQuery(QueryBuilders.queryStringQuery("")).get();

匹配查询,设定查询字段

SearchResponse searchResponse = searchRequestBuilder.setQuery(QueryBuilders.matchQuery("字段","")).get();

词条查询,只能查询分词后是词条的文档

SearchResponse searchResponse = searchRequestBuilder.setQuery(QueryBuilders.termQuery("字段","")).get();

范围查询

SearchResponse searchResponse = searchRequestBuilder.setQuery(QueryBuilders.rangeQuery("字段").from("开始", boolean includeLower).to("结束", boolean includeUpper)).get();

分页

searchRequestBuilder.setQuery(查询方法);
searchRequestBuilder.setFrom(起始页);
searchRequestBuilder.setSize(页大小);
searchRequestBuilder.addSort("字段", SortOrder.ASC);
SearchResponse searchResponse = searchRequestBuilder.get();

查询结果

SearchHits searchHits = searchResponse.getHits();
//结构总条数
long totalHits = searchHits.totalHits;

//获取单条记录,封装到map,key为字段名
Map<String, Object> map = searchHits.getAt(int index).getSourceAsMap();

高亮

searchRequestBuilder.setQuery(查询方法);

//设定高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("field");//加前缀字段
highlightBuilder.preTags("<font>");//前缀
highlightBuilder.postTags("</font>");//后缀
searchRequestBuilder.highlighter(highlightBuilder);

//执行查询
SearchResponse searchResponse = searchRequestBuilder.get();
//获取每个文档 字段,HighlightField的map
Map<String, HighlightField> highlightFields = searchResponse.getHits().getAt(0).getHighlightFields();
//获取分词加上前后缀的内容
highlightFields.get("field").getFragments()[0].toString();

org.springframework.data.elasticsearch

application.yml

spring:
  data:
    elasticsearch:
      cluster-name: docker-cluster
      cluster-nodes: url:9300

pojo

@Document(indexName = "indexName",type = "type")
public class Pojo {

    //FieldType.Keyword不索引
    @Field(type = FieldType.Keyword)
    private Long id;
    //FieldType.Text索引
    @Field(type =  FieldType.Text, analyzer = "ik_max_word")
    private String prop;

repository

public interface PojoRepository extends ElasticsearchRepository<Pojo,Long> {
}

service

public class SearchService {
	//通过repository操作ElasticSearch创建仓库
    @Autowired
    private PojoRepository repository;

	//通过elasticsearchTemplate查询,
	//多条件Query用org.springframework.data.elasticsearch.core.query的Builder创建
	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值