詳しくはspringの中でElasticsearchのコードを使って実現します。


Elasticsearchを使う前に、まず干物について話します。
1.      ESとソロは全文検索エンジンとして登場する。全部Luceneによる検索サーバーです。
2.   ESは信頼できるストレージシステムではなく、データベースではなく、データを失うリスクがあります。
3.  ESはリアルタイムシステムではなく、データの書き込みが成功したのはtrans logs(MySQLのbin logsに似ている)だけで、書き込みが成功したらすぐに確認できないのが正常です。データは今メモリの中にあるかもしれません。エンジンに入るのではありません。同じように、一つのデータを削除してもすぐに消えるわけではありません。書き込みはいつ確認できますか?ES内部にはバックグラウンドスレッドがあり、メモリ内のデータを一時的にメモリエンジンに書き込み、その後データが表示される。デフォルトのバックグラウンドスレッドは一秒に一回実行されます。このスレッドは頻繁に動作するほど、書き込み性能が低下します。運転の周波数が低いほど、書き込みの性能が高くなります。
4.    現在知られている単一ESクラスタはPBレベルのデータを記憶することができますが、これは大変です。TBレベルのデータは圧力がない。
5.    ES公式提供のjarパッケージを使用する場合は、JDK 1.7以上が必要です。
6.    対応バージョンを使ってES serverにアクセスします。ES server端のバージョンが1.7なら、ES 1.7のclientを使ってください。ES serverが2.1なら、2.1のclientを使ってください。
7.     ESインデックスはLinuxサーバのファイルシステム上に存在します。
8.     ES Java clientはスレッドの安全で、大域的に一つを構築すれば読み書きの需要を満たすことができます。毎回ES clientを作成しないでください。ESにアクセスするたびに、新しいes clientを構築します。
9.    ESを使用した動的識別と作成の仕組みは、多くの場合、あなたが必要とするものではないからです。オススメの作り方は、データを書く前に丁寧にmappingを作成することです。
10.   ESでの深さのページ使用は強く推奨されません。クラスタが利用できなくなる可能性があります。
11.    ESは静的スライスであり、スライス数がインデックス作成時に決定されると、その後は変更できない。
12.    ESではtypeが提供されています。多くの人はtypeを物理テーブルと考えています。一つのtypeのデータは独立して保存されています。しかしES内部ではそうではなく、typeはES内部ではフィールドだけです。多くのデータが独立indexに分けられる場合、一つのindexに入れてtypeで分けないでください。入れ子類と親子類の場合のみ、typeを使うのが合理的です。
13.    ESは原生の中国語分詞の能力を提供していません。第三者の中国語の単語を分けるプラグインがあります。例えばikなどです。Ikはtoy分詞器で、厳粛な分詞需要がある場合は、ESを使用する前に独立した分詞器で単語を区切ってESに書き込んでください。
14.    ES中のindexは、まずスライスを行います。スライス毎のデータは通常自分のレプリカデータがあります。ES割り当てスライスのポリシーは同じスライスデータと自分のレプリカが同じノードに割り当てられないことを保証します。クラスタ内のあるノードあたごの後、ESのマスターはノードをpingする時、ある戦略によってノードが生存していないことを発見する。ESの回復過程が始まります。
15.    ESはアップデートの能力がない。すべてのudateは古い文書を削除するというマークを付けて、新しい文書を再insertします。
じゃ、本題に戻ります。
まず、
私達のspringの配置を増加します。

<bean id="client" factory-bean="esClientBuilder" factory-method="init" destroy-method="close"/> 
 
  <bean id="esClientBuilder" class="com.***.EsClientBuilder"> 
    <property name="clusterName" value="    " /> 
    <property name="nodeIpInfo" value="    " /> 
  </bean> 
次:
私達のEsClient Builder類を編纂して私達のESパラメーターを初期化します。

package ***; 
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.util.HashMap; 
import java.util.Map; 
import org.elasticsearch.client.Client; 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.common.transport.InetSocketTransportAddress; 
public class EsClientBuilder { 
 
 
  private String clusterName; 
  private String nodeIpInfo; 
  private TransportClient client; 
 
  public Client init(){ 
    //        
    Settings settings = Settings.settingsBuilder() 
        .put("client.transport.sniff", false) 
        .put("cluster.name", clusterName) 
        .build(); 
    //    client          
    client = TransportClient.builder().settings(settings).build(); 
    Map<String, Integer> nodeMap = parseNodeIpInfo(); 
    for (Map.Entry<String,Integer> entry : nodeMap.entrySet()){ 
      try { 
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(entry.getKey()), entry.getValue())); 
      } catch (UnknownHostException e) { 
        e.printStackTrace(); 
      } 
    } 
 
    return client; 
  } 
 
  /** 
   *     IP  ,         ,IP         
   * 
   * @return 
   */ 
  private Map<String, Integer> parseNodeIpInfo(){ 
    String[] nodeIpInfoArr = nodeIpInfo.split(","); 
    Map<String, Integer> map = new HashMap<String, Integer>(nodeIpInfoArr.length); 
    for (String ipInfo : nodeIpInfoArr){ 
      String[] ipInfoArr = ipInfo.split(":"); 
      map.put(ipInfoArr[0], Integer.parseInt(ipInfoArr[1])); 
    } 
    return map; 
  } 
 
  public String getClusterName() { 
    return clusterName; 
  } 
 
  public void setClusterName(String clusterName) { 
    this.clusterName = clusterName; 
  } 
 
  public String getNodeIpInfo() { 
    return nodeIpInfo; 
  } 
 
  public void setNodeIpInfo(String nodeIpInfo) { 
    this.nodeIpInfo = nodeIpInfo; 
  } 
} 

最後:
以下は自分のservice類を書くことができます。このようなことはesの生アプリを通じてes(ここで示した2.Xバージョンの)を操作することができます。
indexNameはデータベース名に相当し、typeNameは表名に相当します。
Es ServiceImpl.Javaファイルを参照してください。

package ***; 
 
@Service("esService") 
public class EsServiceImpl{ 
 
  @Autowired 
  private Client client; 
 
  /** 
   *  docId  document 
   * @param indexName 
   * @param typeName 
   * @param docId 
   */ 
  private static void getWithId(String indexName, String typeName, String docId) { 
    //get with id 
    GetResponse gResponse = client.prepareGet(indexName, typeName, docId).execute().actionGet(); 
    System.out.println(gResponse.getIndex()); 
    System.out.println(gResponse.getType()); 
    System.out.println(gResponse.getVersion()); 
    System.out.println(gResponse.isExists()); 
    Map<String, Object> results = gResponse.getSource(); 
    if(results != null) { 
      for(String key : results.keySet()) { 
        Object field = results.get(key); 
        System.out.println(key); 
        System.out.println(field); 
      } 
    } 
  } 
  private static void indexWithBulk(String index, String type) { 
        //      ,type   documentId(documentId  ,          )  document 
        IndexRequest ir1 = new IndexRequest(); 
        String source1 = "{" + "\"user\":\"kimchy\"," + "\"price\":\"6.3\"," + "\"tid\":\"20001\"," + "\"message\":\"Elasticsearch\"" + "}"; 
        ir1.index(index).type(type).id("100").source(source1); 
        IndexRequest ir2 = new IndexRequest(); 
        String source2 = "{" + "\"user\":\"kimchy2\"," + "\"price\":\"7.3\"," + "\"tid\":\"20002\"," + "\"message\":\"Elasticsearch\"" + "}"; 
        ir2.index(index).type(type).id("102").source(source2); 
        IndexRequest ir3 = new IndexRequest(); 
        String source3 = "{" + "\"user\":\"kimchy3\"," + "\"price\":\"8.3\"," + "\"tid\":\"20003\"," + "\"message\":\"Elasticsearch\"" + "}"; 
        ir3.index(index).type(type).id("103").source(source3); 
        BulkResponse response = client.prepareBulk().add(ir1).add(ir2).add(ir3).execute().actionGet(); 
        BulkItemResponse[] responses = response.getItems(); 
        if(responses != null && responses.length > 0) { 
          for(BulkItemResponse r : responses) { 
            String i = r.getIndex(); 
            String t = r.getType(); 
            System.out.println(i+","+t); 
          } 
        } 
     
  } 
  private static void sumCountSearch(String indexName, String typeName, 
    String sumField, String countField, String searchField, String searchValue) { 
    SumBuilder sb = AggregationBuilders.sum("sumPrice").field(sumField); 
    TermQueryBuilder tb = QueryBuilders.termQuery(searchField, searchValue); 
    SearchResponse sResponse = client.prepareSearch(indexName).setTypes(typeName).setQuery(tb).addAggregation(sb).execute().actionGet(); 
    Map<String, Aggregation> aggMap = sResponse.getAggregations().asMap(); 
    if(aggMap != null && aggMap.size() > 0) { 
      for(String key : aggMap.keySet()) { 
        if("sumPrice".equals(key)) { 
          Sum s = (Sum)aggMap.get(key); 
          System.out.println(key + "," + s.getValue());   
        } 
        else if("countTid".equals(key)) { 
          StatsBuilder c = (StatsBuilder)aggMap.get(key); 
          System.out.println(key + "," + c.toString()); 
        } 
      } 
    } 
  } 
  private static void updateDoc(String indexName, String typeName, String id) throws IOException, InterruptedException, ExecutionException { 
    UpdateRequest updateRequest = new UpdateRequest(); 
    updateRequest.index(indexName); 
    updateRequest.type(typeName); 
    updateRequest.id(id); 
    updateRequest.doc(jsonBuilder().startObject().field("gender", "male").endObject()); 
    UpdateResponse resp = client.update(updateRequest).get(); 
    resp.getClass(); 
  } 
  private static void scrollSearch(String indexName, String typeName, String... ids) { 
    IdsQueryBuilder qb = QueryBuilders.idsQuery().addIds(ids); 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        .setSearchType(SearchType.SCAN) 
        .setQuery(qb) 
        .setScroll(new TimeValue(100)) 
        .setSize(50) 
        .execute() 
        .actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards+","+timeCost+","+sShards); 
     
    while (true) { 
      SearchHits hits = sResponse.getHits(); 
      SearchHit[] hitArray = hits.getHits(); 
      for(int i = 0; i < hitArray.length; i++) { 
        SearchHit hit = hitArray[i]; 
        Map<String, Object> fields = hit.getSource(); 
        for(String key : fields.keySet()) { 
          System.out.println(key); 
          System.out.println(fields.get(key)); 
        } 
      } 
      sResponse = client.prepareSearchScroll(sResponse.getScrollId()).setScroll(new TimeValue(100)).execute().actionGet(); 
      if (sResponse.getHits().getHits().length == 0) { 
        break; 
      } 
    } 
  } 
  private static void deleteDocuments(String string, String string2) { 
    SearchResponse sResponse = client.prepareSearch(string) 
        .setTypes(string2) 
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        .setQuery(QueryBuilders.matchAllQuery()) 
        .setFrom(0).setSize(60) 
        .execute() 
        .actionGet(); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    List<String> ids = new ArrayList<String>(hitArray.length); 
    for(int i = 0; i < count; i++) { 
      System.out.println("=================================="); 
      SearchHit hit = hitArray[i]; 
      ids.add(hit.getId()); 
       
    } 
    for(String id : ids) { 
      DeleteResponse response = client.prepareDelete(string, string2, id).execute().actionGet(); 
    }   
  } 
  private static void dateRangeSearch(String indexName, String typeName, 
      String termName, String from, String to) { 
    //   range query 
    //2015-08-20 12:27:11 
        QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(from).to(to); 
        SearchResponse sResponse = client.prepareSearch(indexName) 
            .setTypes(typeName) 
            //   search type 
            //   search type :query_then_fetch 
            // query_then_fetch        ,      node        
            .setSearchType(SearchType.QUERY_THEN_FETCH) 
            //    termName termvalue 
            .setQuery(qb) 
            //     field 
            .addSort(termName, SortOrder.DESC) 
            //      
            .setFrom(0).setSize(60).execute().actionGet(); 
        int tShards = sResponse.getTotalShards(); 
        long timeCost = sResponse.getTookInMillis(); 
        int sShards = sResponse.getSuccessfulShards(); 
        System.out.println(tShards + "," + timeCost + "," + sShards); 
        SearchHits hits = sResponse.getHits(); 
        long count = hits.getTotalHits(); 
        SearchHit[] hitArray = hits.getHits(); 
        for (int i = 0; i < count; i++) { 
          SearchHit hit = hitArray[i]; 
          Map<String, Object> fields = hit.getSource(); 
          for (String key : fields.keySet()) { 
            System.out.println(key); 
            System.out.println(fields.get(key)); 
          } 
        } 
  } 
  private static void dateRangeSearch2(String indexName, String typeName, 
      String termName, String from, String to) { 
    //   range query 
      QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(from).to(to); 
      SearchResponse sResponse = client.prepareSearch(indexName) 
          .setTypes(typeName) 
          //   search type 
          //   search type :query_then_fetch 
          // query_then_fetch        ,      node        
          .setSearchType(SearchType.QUERY_THEN_FETCH) 
          //    termName termvalue 
          .setQuery(qb) 
          //     field 
          .addSort(termName, SortOrder.DESC) 
          //      
          .setFrom(0).setSize(60).execute().actionGet(); 
      int tShards = sResponse.getTotalShards(); 
      long timeCost = sResponse.getTookInMillis(); 
      int sShards = sResponse.getSuccessfulShards(); 
      System.out.println(tShards + "," + timeCost + "," + sShards); 
      SearchHits hits = sResponse.getHits(); 
      long count = hits.getTotalHits(); 
      SearchHit[] hitArray = hits.getHits(); 
      for (int i = 0; i < count; i++) { 
        SearchHit hit = hitArray[i]; 
        Map<String, Object> fields = hit.getSource(); 
        for (String key : fields.keySet()) { 
          System.out.println(key); 
          System.out.println(fields.get(key)); 
        } 
      } 
  } 
  private static void countWithQuery(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) { 
    //search result get source 
        CountResponse cResponse = client.prepareCount(indexName) 
            .setTypes(typeName) 
            .setQuery(QueryBuilders.termQuery(termName, termValue)) 
            .execute() 
            .actionGet(); 
        int tShards = cResponse.getTotalShards(); 
        int sShards = cResponse.getSuccessfulShards(); 
        System.out.println(tShards+","+sShards); 
        long count = cResponse.getCount();    
  } 
  private static void rangeSearchWithOtherSearch(String indexName, String typeName, 
      String termName, String min, String max, String termQueryField) { 
    //   range query 
        QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(min).to(max); 
        TermQueryBuilder tb = QueryBuilders.termQuery(termName, termQueryField); 
        BoolQueryBuilder bq = boolQuery().must(qb).must(tb); 
        SearchResponse sResponse = client.prepareSearch(indexName) 
            .setTypes(typeName) 
            //   search type 
            //   search type :query_then_fetch 
            // query_then_fetch        ,      node        
            .setSearchType(SearchType.QUERY_THEN_FETCH) 
            //    termName termvalue 
            .setQuery(bq) 
            //     field 
            .addSort(termName, SortOrder.DESC) 
            //      
            .setFrom(0).setSize(60).execute().actionGet(); 
        int tShards = sResponse.getTotalShards(); 
        long timeCost = sResponse.getTookInMillis(); 
        int sShards = sResponse.getSuccessfulShards(); 
        System.out.println(tShards + "," + timeCost + "," + sShards); 
        SearchHits hits = sResponse.getHits(); 
        long count = hits.getTotalHits(); 
        SearchHit[] hitArray = hits.getHits(); 
        for (int i = 0; i < count; i++) { 
          SearchHit hit = hitArray[i]; 
          Map<String, Object> fields = hit.getSource(); 
          for (String key : fields.keySet()) { 
            System.out.println(key); 
            System.out.println(fields.get(key)); 
          } 
        } 
  } 
  private static void termRangeSearch(String indexName, String typeName, 
    String termName, String min, String max, String highlightField) { 
    QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(min).to(max); 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //   search type 
        //   search type :query_then_fetch 
        // query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //    termName termvalue 
        .setQuery(qb) 
        //     field 
        .addSort(termName, SortOrder.DESC) 
        //    field 
        .addHighlightedField(highlightField) 
        //      
        .setFrom(0).setSize(60).execute().actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards + "," + timeCost + "," + sShards); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for (int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for (String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
  private static void sumOneField(String indexName, String typeName, String fieldName) { 
    SumBuilder sb = AggregationBuilders.sum("sum").field(fieldName); 
    //search result get source 
    SearchResponse sResponse = client.prepareSearch(indexName).setTypes(typeName).addAggregation(sb).execute().actionGet(); 
    Map<String, Aggregation> aggMap = sResponse.getAggregations().asMap(); 
    if(aggMap != null && aggMap.size() > 0) { 
      for(String key : aggMap.keySet()) { 
        Sum s = (Sum)aggMap.get(key); 
        System.out.println(s.getValue()); 
      } 
    } 
  } 
  private static void searchWithTermQueryAndRetureSpecifiedFields(String indexName, String typeName, String termName,String termValue, String sortField, String highlightField,String... fields) { 
     SearchRequestBuilder sb = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //   search type 
        //   search type :query_then_fetch 
        // query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //    termName termvalue 
        .setQuery(QueryBuilders.termQuery(termName, termValue)) 
        //     field 
        .addSort(sortField, SortOrder.DESC) 
        //     field 
        .addHighlightedField(highlightField) 
        //      
        .setFrom(0).setSize(60); 
    for (String field : fields) { 
      sb.addField(field); 
    } 
    SearchResponse sResponse = sb.execute().actionGet(); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for (int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, SearchHitField> fm = hit.getFields(); 
      for (String key : fm.keySet()) { 
        SearchHitField f = fm.get(key); 
        System.out.println(f.getName()); 
        System.out.println(f.getValue()); 
      } 
    } 
  } 
  private static void searchWithIds(String indexName, String typeName, String sortField, String highlightField, String... ids) { 
    IdsQueryBuilder qb = QueryBuilders.idsQuery().addIds(ids); 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //  search type 
        //  search type :query_then_fetch 
        //query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //   termName termvalue 
        .setQuery(qb) 
        //    field 
        .addSort(sortField, SortOrder.DESC) 
        //    field 
        .addHighlightedField(highlightField) 
        //     
        .setFrom(0).setSize(60) 
        .execute() 
        .actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards+","+timeCost+","+sShards); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for(int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for(String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
 
  /** 
   *  index:indexName, type:typeName        
   * @param indexName 
   * @param typeName 
   * @param termName 
   * @param termValue 
   * @param sortField 
   * @param highlightField 
   */ 
  private static void wildcardSearch(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) { 
    QueryBuilder qb = QueryBuilders.wildcardQuery(termName, termValue); 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //  search type 
        //  search type :query_then_fetch 
        //query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //   termName termvalue 
        .setQuery(qb) 
        //    field 
//       .addSort(sortField, SortOrder.DESC) 
        //    field 
//       .addHighlightedField(highlightField) 
        //     
        .setFrom(0).setSize(60) 
        .execute() 
        .actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards+","+timeCost+","+sShards); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for(int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for(String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
 
  /** 
   *  index:indexName, type:typeName       
   * @param indexName 
   * @param typeName 
   * @param termName 
   * @param termValue 
   * @param sortField 
   * @param highlightField 
   */ 
  private static void fuzzySearch(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) { 
     QueryBuilder qb = QueryBuilders.fuzzyQuery(termName, termValue); 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //  search type 
        //  search type :query_then_fetch 
        //query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //   termName termvalue 
        .setQuery(qb) 
        //    field 
        .addSort(sortField, SortOrder.DESC) 
        //    field 
        .addHighlightedField(highlightField) 
        //     
        .setFrom(0).setSize(60) 
        .execute() 
        .actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards+","+timeCost+","+sShards); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for(int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for(String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
  /** 
   *  index:indexName, type:typeName       
   * @param indexName 
   * @param typeName 
   * @param termName 
   * @param min 
   * @param max 
   * @param highlightField 
   */ 
  private static void numericRangeSearch(String indexName, String typeName, 
      String termName, double min, double max, String highlightField) { 
    //   range query 
    QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(min).to(max); 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //   search type 
        //   search type :query_then_fetch 
        // query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //    termName termvalue 
        .setQuery(qb) 
        //     field 
        .addSort(termName, SortOrder.DESC) 
        //    field 
        .addHighlightedField(highlightField) 
        //      
        .setFrom(0).setSize(60).execute().actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards + "," + timeCost + "," + sShards); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for (int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for (String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
  /** 
   *    indexName, type typeName     term:term1(termName1, termValue1) term2(termName2, termValue2) 
   * @param indexName 
   * @param typeName 
   * @param termName1 
   * @param termValue1 
   * @param termName2 
   * @param termValue2 
   * @param sortField 
   * @param highlightField 
   */ 
  private static void searchWithBooleanQuery(String indexName, String typeName, String termName1, String termValue1,  
       String termName2, String termValue2, String sortField, String highlightField) { 
    //  boolean query 
    BoolQueryBuilder bq = boolQuery().must(termQuery(termName1, termValue1)).must(termQuery(termName2, termValue2));   
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //  search type 
        //  search type :query_then_fetch 
        //query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //   termName termvalue 
        .setQuery(bq) 
        //    field 
        .addSort(sortField, SortOrder.DESC) 
        //    field 
        .addHighlightedField(highlightField) 
        //     
        .setFrom(0).setSize(60) 
        .execute() 
        .actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    System.out.println(tShards+","+timeCost+","+sShards); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for(int i = 0; i < count; i++) { 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for(String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
  /** 
   *    indexName, type typeName   term(termName, termValue) 
   * @param indexName 
   * @param typeName 
   * @param termName 
   * @param termValue 
   * @param sortField 
   * @param highlightField 
   */ 
  private static void searchWithTermQuery(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) { 
    SearchResponse sResponse = client.prepareSearch(indexName) 
        .setTypes(typeName) 
        //  search type 
        //  search type :query_then_fetch 
        //query_then_fetch        ,      node        
        .setSearchType(SearchType.QUERY_THEN_FETCH) 
        //   termName termvalue 
        .setQuery(QueryBuilders.termQuery(termName, termValue)) 
        //    field 
//       .addSort(sortField, SortOrder.DESC) 
        //    field 
//       .addHighlightedField(highlightField) 
        //     
        .setFrom(0).setSize(60) 
        .execute() 
        .actionGet(); 
    int tShards = sResponse.getTotalShards(); 
    long timeCost = sResponse.getTookInMillis(); 
    int sShards = sResponse.getSuccessfulShards(); 
    SearchHits hits = sResponse.getHits(); 
    long count = hits.getTotalHits(); 
    SearchHit[] hitArray = hits.getHits(); 
    for(int i = 0; i < count; i++) { 
      System.out.println("=================================="); 
      SearchHit hit = hitArray[i]; 
      Map<String, Object> fields = hit.getSource(); 
      for(String key : fields.keySet()) { 
        System.out.println(key); 
        System.out.println(fields.get(key)); 
      } 
    } 
  } 
  /** 
   *  java map  document 
   */ 
  private static void indexWithMap(String indexName, String typeName) { 
    Map<String, Object> json = new HashMap<String, Object>(); 
    //  document field 
    json.put("user","kimchy2"); 
    json.put("postDate",new Date()); 
    json.put("price",6.4); 
    json.put("message","Elasticsearch"); 
    json.put("tid","10002"); 
    json.put("endTime","2015-08-25 09:00:00"); 
    //      ,type   documentId(documentId  ,          )  document 
    IndexResponse response = client.prepareIndex(indexName, typeName, "2").setSource(json).execute().actionGet(); 
    //response       ,type  ,doc Id      
    String index = response.getIndex(); 
    String type = response.getType(); 
    String id = response.getId(); 
    long version = response.getVersion(); 
    boolean created = response.isCreated(); 
    System.out.println(index+","+type+","+id+","+version+","+created); 
  } 
 
  /** 
   *  java     document 
   */ 
  private static void indexWithStr(String indexName, String typeName) { 
    //    json    
    // document  user, postData message  field 
    String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"price\":\"6.3\"," + "\"tid\":\"10001\"," + "}"; 
    //      ,type   documentId(documentId  ,          )  document 
    IndexResponse response = client.prepareIndex(indexName, typeName, "1") 
        .setSource(json) 
        .execute() 
        .actionGet(); 
    //response       ,type  ,doc Id      
    String index = response.getIndex(); 
    String type = response.getType(); 
    String id = response.getId(); 
    long version = response.getVersion(); 
    boolean created = response.isCreated(); 
    System.out.println(index+","+type+","+id+","+version+","+created); 
  } 
   
  private static void deleteDocWithId(String indexName, String typeName, String docId) { 
    DeleteResponse dResponse = client.prepareDelete(indexName, typeName, docId).execute().actionGet(); 
    String index = dResponse.getIndex(); 
    String type = dResponse.getType(); 
    String id = dResponse.getId(); 
    long version = dResponse.getVersion(); 
    System.out.println(index+","+type+","+id+","+version); 
  } 
   
  /** 
   *      
   *   :        es   owner   index 
   * @param client 
   * @param indexName 
   * @param documentType 
   * @throws IOException 
   */ 
  private static void createIndex(String indexName, String documentType) throws IOException { 
    final IndicesExistsResponse iRes = client.admin().indices().prepareExists(indexName).execute().actionGet(); 
    if (iRes.isExists()) { 
      client.admin().indices().prepareDelete(indexName).execute().actionGet(); 
    } 
    client.admin().indices().prepareCreate(indexName).setSettings(Settings.settingsBuilder().put("number_of_shards", 1).put("number_of_replicas", "0")).execute().actionGet(); 
    XContentBuilder mapping = jsonBuilder() 
        .startObject() 
           .startObject(documentType) 
//           .startObject("_routing").field("path","tid").field("required", "true").endObject() 
           .startObject("_source").field("enabled", "true").endObject() 
           .startObject("_all").field("enabled", "false").endObject() 
             .startObject("properties") 
               .startObject("user") 
                 .field("store", true) 
                 .field("type", "string") 
                 .field("index", "not_analyzed") 
                .endObject() 
                .startObject("message") 
                 .field("store", true) 
                 .field("type","string") 
                 .field("index", "analyzed") 
                 .field("analyzer", "standard") 
                .endObject() 
                .startObject("price") 
                 .field("store", true) 
                 .field("type", "float") 
                .endObject() 
                .startObject("nv1") 
                 .field("store", true) 
                 .field("type", "integer") 
                 .field("index", "no") 
                 .field("null_value", 0) 
                .endObject() 
                .startObject("nv2") 
                 .field("store", true) 
                 .field("type", "integer") 
                 .field("index", "not_analyzed") 
                 .field("null_value", 10) 
                .endObject() 
                .startObject("tid") 
                 .field("store", true) 
                 .field("type", "string") 
                 .field("index", "not_analyzed") 
                .endObject() 
                .startObject("endTime") 
                 .field("type", "date") 
                 .field("store", true) 
                 .field("index", "not_analyzed") 
                 .field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSSZ") 
                .endObject() 
                .startObject("date") 
                 .field("type", "date") 
                .endObject() 
             .endObject() 
           .endObject() 
          .endObject(); 
    client.admin().indices() 
        .preparePutMapping(indexName) 
        .setType(documentType) 
        .setSource(mapping) 
        .execute().actionGet(); 
  } 
} 
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。