1,新增索引film(分片5,副本1),建立映射关系dongzuo类,和其它字段(tittle,publishDate,content,director,
price),可以参考上一节知识;在Java代码中添加测试数据,
{
"properties": {
"title": {
"type": "text"
},
"publishDate": {
"type": "date"
},
"content": {
"type": "text"
},
"director": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
添加测试数据:见附件
2,在elasticsearch-head 图形化界面端查询所有,
写Java代码:查询所有的方法searchALL(),测试,
package com.cruise;
import java.net.InetAddress;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.JsonObject;
public class TestSearch {
private static String host="192.168.245.40";
private static int port=9300;
private TransportClient client =null;
public static final String CLUSTER_NAME="my-application";
private static Settings.Builder settings=Settings.builder().put("cluster.name",CLUSTER_NAME);
@SuppressWarnings({ "resource", "unchecked" })
@Before
public void getClient() throws Exception{
client = new PreBuiltTransportClient(settings.build())
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
}
@After
public void close(){
if(clie
第7讲 7.ElasticSearch简单查询
最新推荐文章于 2023-08-27 20:13:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)