第7讲 7.ElasticSearch简单查询

这篇博客详细介绍了如何使用Java API进行Elasticsearch的查询操作,包括创建索引、映射设置、添加测试数据,以及在elasticsearch-head中进行的简单查询、分页查询、按日期排序查询、指定字段显示、条件查询和高亮显示等操作。
摘要由CSDN通过智能技术生成

1,新增索引film(分片5,副本1),建立映射关系dongzuo类,和其它字段(tittle,publishDate,content,director,
  price),可以参考上一节知识;在Java代码中添加测试数据,

7.ElasticSearch简单查询
{
  "properties": {
    "title": {
      "type": "text"
    },
    "publishDate": {
      "type": "date"
    },
    "content": {
      "type": "text"
    },
    "director": {
      "type": "keyword"
    },
    "price": {
      "type": "float"
    }
  }
}
7.ElasticSearch简单查询
添加测试数据:见附件
2,在elasticsearch-head 图形化界面端查询所有,
7.ElasticSearch简单查询
写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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值