日期:2017年12月21日


ElasticSearch增删改查日记


  public static void main(String[] args) {
  TransportClient client = null;
  try {
  client = new PreBuiltTransportClient(Settings.EMPTY)
  .addTransportAddress(new
  InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
  SearchResponse response = client.prepareSearch()
  .setQuery(QueryBuilders.boolQuery()
  .should(QueryBuilders.rangeQuery("tax_paid").from("452588.45").to("452588.45"))
  .should(QueryBuilders.rangeQuery("cash").from("17573086.76").to("17573086.76"))
  .should(QueryBuilders.wildcardQuery("company_name", "*生物*"))
  )
  .get();
  System.out.println(response.getHits().totalHits);
  for (SearchHit hit : response.getHits().getHits()) {
  System.out.print("index:" + hit.getIndex() + "----");
  System.out.print("type:" + hit.getType() + "----");
  System.out.print("value:" + hit.getSource().get("company_id"));
  System.out.println();
  }
  } catch (UnknownHostException e) {
  e.printStackTrace();
  } finally {
  if (client != null) {
  client.close();
  }
  }
  }

/*
 * 批量操作
 */ 
  public static void main(String[] args) { TransportClient client = null; try {
   String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," +
   "\"message\":\"trying out sadfsdafasdf\"" + "}"; String json2 = "{" +
   "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-31\"," +
  "\"message\":\"trying out aaaaa\"" + "}"; client = new
   PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new
   InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
   BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
   bulkRequestBuilder.add(client.prepareIndex("company", "company_profile", "2")
   .setSource(json)); bulkRequestBuilder.add(client.prepareIndex("company",
   "company_profile", "3").setSource(json2)); BulkResponse responses =
   bulkRequestBuilder.get(); if (responses.hasFailures()) {
   System.out.println(responses.toString()); } } catch (UnknownHostException e)
   { e.printStackTrace(); } finally { if (client != null) { client.close(); } }
   }
  

/*
 * 删除数据
 *
 */
  public static void main(String[] args) {
  TransportClient client = null;
  try {
  client = new PreBuiltTransportClient(Settings.EMPTY)
  .addTransportAddress(new
  InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
  DeleteResponse response = client.prepareDelete("company", "company_profile",
  "1").get();
  System.out.println(response.status());
  } catch (UnknownHostException e) {
  e.printStackTrace();
  } finally {
  if (client != null) {
  client.close();
  }
  }
  }

/*
 * 插入数据
*/
 public static void main(String[] args) { TransportClient client = null;
   try { client = new PreBuiltTransportClient(Settings.EMPTY)
  .addTransportAddress(new
   InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); String
   json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," +
   "\"message\":\"trying out Elasticsearch\"" + "}"; IndexResponse response =
   client.prepareIndex("company", "company_profile").setSource(json,
   XContentType.JSON).get(); String _index = response.getIndex(); String _type =
   response.getType(); String _id = response.getId(); long _version =
   response.getVersion(); System.out.println("index:" + _index + "\ntype:" +
   _type + "\nid:" + _id + "\nversion:" + _version); } catch
   (UnknownHostException e) { e.printStackTrace(); } finally { if (client !=
   null) { client.close(); } } }
 

Java 正则匹配全中文字段


// 按指定模式在字符串查找
String line = "全中文a";
String pattern = "^[\u4e00-\u9fa5]+$";

// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);

// 现在创建 matcher 对象
Matcher m = r.matcher(line);
System.out.println(m.matches());