<< Lucene 4.4 以后近实时NRT检索 | 首页 | Hue ——一个用于Apache Hadoop大数据分析的Web界面应用 >>

ElasticSearch: Java API | Javalobby

ElasticSearch Java API 官方文档:

http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html

ElasticSearch提供了Java API,因此可通过使用Client对象异步执行所有操作。Client对象可以批量执行操作,累计。 Java的API可以在内部使用,以执行所有的API在ElasticSearch。

在本教程中,我们将考虑如何开展将Java API一些操作用在一个独立的Java应用程序里,类似于那些我们在上一篇文章中做的。

Dependency

ElasticSearch托管于Maven的中央。在你的Maven项目,你可以定义你想要的,如下图所示在你的pom.xml文件使用哪个版本ElasticSearch:

 

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>0.90.3</version>
</dependency>

 

 

Client

使用Client,您可以执行在正在运行的群集管理任务,如一些操作,如标准索引,get,删除和搜索操作,在现有的集群中。另外,还可以启动所有节点。


获得ElasticSearchclient的最常见的方式是建立它就像在一个群集中的节点的嵌入式节点以及请求客户端从该嵌入节点。


另一种方式获得客户端是创建TransportClient(它从远程连接集群通过使用transport组件),其连接到群集的另一种方式。


应该考虑使用客户端和集群的相同版本,当使用Java API时。客户端和群集版本之间的差异可能会导致某些不兼容性。

 

Node Client

The simplest way of getting a client instance is the node based client. 

 

Node node  = nodeBuilder().node();
Client client = node.client();

 

When a node is started, it joins to the "elasticsearch" cluster. You can create different clusters using the cluster.name setting or clusterName method in the/src/main/resources directory and in elasticsearch.yml file in your project:

 

cluster.name: yourclustername

 

Or in Java code:

 

Node node = nodeBuilder().clusterName("yourclustername").node();
Client client = node.client();

 

Creating Index

 

The Index API allows you to type a JSON document into a specific index, and makes it searchable. There are different ways of generating JSON documents. Here we used map, which represents JSON structure very well. 

 

public static Map<String, Object> putJsonDocument(String title, String content, Date postDate, 
                                                      String[] tags, String author){
        
        Map<String, Object> jsonDocument = new HashMap<String, Object>();
        
        jsonDocument.put("title", title);
        jsonDocument.put("conten", content);
        jsonDocument.put("postDate", postDate);
        jsonDocument.put("tags", tags);
        jsonDocument.put("author", author);
        
        return jsonDocument;
    }

 

Node node    = nodeBuilder().node();
Client client   = node.client();
        
client.prepareIndex("kodcucom", "article", "1")
              .setSource(putJsonDocument("ElasticSearch: Java API",
                                         "ElasticSearch provides the Java API, all operations "
                                         + "can be executed asynchronously using a client object.",
                                         new Date(),
                                         new String[]{"elasticsearch"},
                                         "Hüseyin Akdoğan")).execute().actionGet();
                
        node.close();

 

With the above code, we generate an index by the name of kodcucom and a type by the name of article with standard settings and a record (we don’t have to give an ID here) whose ID value of 1 is stored to ElasticSearch. 

 

Getting Document

The Get API allows you to get a typed JSON document, based on the ID, from the index. 

 

GetResponse getResponse = client.prepareGet("kodcucom", "article", "1").execute().actionGet();

Map<String, Object> source = getResponse.getSource();
        
System.out.println("------------------------------");
System.out.println("Index: " + getResponse.getIndex());
System.out.println("Type: " + getResponse.getType());
System.out.println("Id: " + getResponse.getId());
System.out.println("Version: " + getResponse.getVersion());
System.out.println(source);
System.out.println("------------------------------");

 

Search

The Search API allows you to execute a search query and get the matched results. The query can be executed across more than one indices and types. The query can be provided by using query Java API or filter Java API. Below you can see an example whose body of search request is built by using SearchSourceBuilder. 

 

public static void searchDocument(Client client, String index, String type,
                                      String field, String value){
        
        SearchResponse response = client.prepareSearch(index)
                                        .setTypes(type)
                                        .setSearchType(SearchType.QUERY_AND_FETCH)
                                        .setQuery(fieldQuery(field, value))
                                        .setFrom(0).setSize(60).setExplain(true)
                                        .execute()
                                        .actionGet();
        
        SearchHit[] results = response.getHits().getHits();
        
        System.out.println("Current results: " + results.length);
        for (SearchHit hit : results) {
            System.out.println("------------------------------");
            Map<String,Object> result = hit.getSource();   
            System.out.println(result);
        }
        
    }

 

searchDocument(client, "kodcucom", "article", "title", "ElasticSearch");

 

Updating

Below you can see an example of a field update.

 

public static void updateDocument(Client client, String index, String type, 
                                      String id, String field, String newValue){
        
        Map<String, Object> updateObject = new HashMap<String, Object>();
        updateObject.put(field, newValue);
        
        client.prepareUpdate(index, type, id)
              .setScript("ctx._source." + field + "=" + field)
              .setScriptParams(updateObject).execute().actionGet();
    }

 

updateDocument(client, "kodcucom", "article", "1", "tags", "big-data");

 

Deleting

The delete API allows you to delete a document whose ID value is specified. You can see below an example of deleting a document whose index, type and value is specified. 

 

public static void deleteDocument(Client client, String index, String type, String id){
        
        DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
        System.out.println("Information on the deleted document:");
        System.out.println("Index: " + response.getIndex());
        System.out.println("Type: " + response.getType());
        System.out.println("Id: " + response.getId());
        System.out.println("Version: " + response.getVersion());
    }

 

deleteDocument(client, "kodcucom", "article", "1");

See the sample application here.

 

Adding mapping to a type from Java (索引库中索引的字段名及其数据类型进行定义,包括是否存储、分词)

 

 
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
 
import java.io.IOException;
 
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
 
public class MyTestClass {
 
    private static final String ID_NOT_FOUND = "<ID NOT FOUND>";
 
    private static Client getClient() {
        final ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        TransportClient transportClient = new TransportClient(settings);
        transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300))
        .addTransportAddress(new InetSocketTransportAddress("localhost", 9301));
        return transportClient;
    }
 
    public static void main(final String[] args) throws IOException, InterruptedException {
 
        final Client client = getClient();
        // Create Index and set settings and mappings
        final String indexName = "test";
        final String documentType = "tweet";
        final String documentId = "1";
        final String fieldName = "title";
        final String value = "bar";
 
        final IndicesExistsResponse res = client.admin().indices().prepareExists(indexName).execute().actionGet();
        if (res.isExists()) {
            final DeleteIndexRequestBuilder delIdx = client.admin().indices().prepareDelete(indexName);
            delIdx.execute().actionGet();
        }
 
        final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);
 
        // MAPPING GOES HERE
 
//        final XContentBuilder mappingBuilder = jsonBuilder().startObject().startObject(documentType)
//                .startObject("_ttl").field("enabled", "true").field("default", "1s").endObject().endObject()
//                .endObject();
        XContentBuilder mapping = jsonBuilder()  
              .startObject()  
                .startObject(documentType)  
                .startObject("properties")         
                  .startObject("title").field("type", "string").field("store", "yes").endObject()    
                  .startObject("description").field("type", "string").field("index", "not_analyzed").endObject()  
                  .startObject("price").field("type", "double").endObject()  
                  .startObject("onSale").field("type", "boolean").endObject()  
                  .startObject("type").field("type", "integer").endObject()  
                  .startObject("createDate").field("type", "date").endObject()                 
                .endObject()  
               .endObject()  
             .endObject();          
        System.out.println(mapping.string());
        createIndexRequestBuilder.addMapping(documentType, mapping);
 
        // MAPPING DONE
        createIndexRequestBuilder.execute().actionGet();
 
        // Add documents
        final IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, documentType, documentId);
        // build json object
        final XContentBuilder contentBuilder = jsonBuilder().startObject().prettyPrint();
        contentBuilder.field(fieldName, value);
 
        indexRequestBuilder.setSource(contentBuilder);
        indexRequestBuilder.execute().actionGet();
 
        // Get document
        System.out.println(getValue(client, indexName, documentType, documentId, fieldName));
 
        int idx = 0;
        while (true) {
            Thread.sleep(10000L);
            idx++;
            System.out.println(idx * 10 + " seconds passed");
            final String name = getValue(client, indexName, documentType, documentId, fieldName);
            if (ID_NOT_FOUND.equals(name)) {
                break;
            } else {
                // Try again
                System.out.println(name);
            }
        }
        System.out.println("Document was garbage collected");
    }
 
    protected static String getValue(final Client client, final String indexName, final String documentType,
            final String documentId, final String fieldName) {
        final GetRequestBuilder getRequestBuilder = client.prepareGet(indexName, documentType, documentId);
        getRequestBuilder.setFields(new String[] { fieldName });
        final GetResponse response2 = getRequestBuilder.execute().actionGet();
        if (response2.isExists()) {
            final String name = response2.getField(fieldName).getValue().toString();
            return name;
        } else {
            return ID_NOT_FOUND;
        }
    }
 
}

 

阅读全文……

标签 : , ,



发表评论 发送引用通报