No. 08 Chapter ElasticSearch Java API

Source: Internet
Author: User
Tags gettext

Chapter Content
Connect to a local or remote Elasticsearch cluster using the client object.
Index documents individually or in batches.
Updates the contents of the document.
Use a variety of Elasticsearch supported query methods.
Handles the error message returned by the Elasticsearch.
Collect cluster state information or perform administrative tasks by sending various management directives.

8.3 Connect to cluster 8.3.10% is a elasticsearch node

The first way to connect to a Elasticsearch node is to treat the application as a node in the Elasticsearch cluster.

Node node=nodeBuilder().clusterName("escluster2").client (true).node();Client client=node.client();
8.3.2 using the transfer Machine connection mode
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name","escluster2").build();TransportClient client=new TransportClient(settings);client.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",9300));

Note that the port number here is not 9200, but 9300. The 9200 port is used to allow the HTTP REST API to access Elasticsearch, while Port 9300 is the default port on which the transport layer listens.
Let's go back and look at the available configurations for the Transportclient class:

    • Client.transport.sniff (Default: false): If set to True, Elasticsearch reads node information from the cluster. Therefore you do not need to provide the network address of all nodes when establishing the Transportclient object. Elasticsearch is smart, it automatically detects active nodes and adds them to the list.
    • Client.transport.ignore_cluster_name (Default: false): If set to true, Elasticsearch ignores the cluster name in the configuration and attempts to connect to a connected cluster, regardless of whether the cluster name matches. This is dangerous and you may first connect to a cluster that you do not want.
    • Client.transport.ping_timeout (default: 5s): This parameter specifies the time-out period for the ping command response. If the network latency between the client and the cluster is large or the connection is unstable, you may need to increase this value.
    • Client.transport.nodes_sampler_interval default value: 5s): This parameter specifies the time interval for checking node availability. Similar to the previous parameter, this value may need to be adjusted if the network latency is large or the network is unstable.
8.3.3 Choosing the Right connection method

The first approach complicates the boot sequence, which means that the client node must be clustered and connected to the other nodes, which takes time and resources. However, the operation can be performed more quickly because all information about the cluster, index, and Shard is visible to the client node.

On the other hand, using the Transportclient object starts faster and requires fewer resources, such as fewer socket connections. However, sending queries and data consumes more resources, and the Transportclient object has no knowledge of the cluster and index topology, so it cannot send the data directly to the correct node, but instead sends the data to an initial transport node first. The rest of the forwarding work is then done by Elasticsearch. In addition, the Transportclient object requires you to provide a list of network addresses for the node to be connected.

Keep in mind that the first approach is not always feasible when choosing the right connection method. For example, when the Elasticsearch cluster to be connected is in another LAN, the only alternative is to use the Transportclient object.

8.4 API Department Analysis
    • Get a specific document record
GetResponse response=client.prepareGet(”library","book",”1").setFields("title",”_source").execute().actionGet();

After the preparation is done, we use the constructor object to create a request (using the request () method) for subsequent use, or to send a query request directly through the Execute () call, where we choose the latter.

The Elasticsearch API is inherently asynchronous. This means that the execute () call does not wait for the result of the elasticsearch response, but instead directly returns control to the code snippet that called it, and the query request executes in the background. In this example we use the Actionget () method, which waits for the query to complete and return the data. This is relatively straightforward, but in more complex systems this is obviously not enough. Next is an example of using an asynchronous API. First, the relevant statement:

ListenableActionFuture<GetResponse> future=client.prepareGet("library","book","1").setFields("title",”_source").execute();future.addListener( new ActionListener<GetResponse>(){  @Override  public void onResponse(GetResponse response){    System.out.println("Document:"+response.getIndex()       +”/“       +response .getType()       +”/"       +response.getId());    }  @Override  public void onFailure(Throwable e){      throw new RuntimeException(e);  }});
8.5 crud Operations 8.5.1 Reading documents
GetResponse response=client.prepareGet("library","book","1").setFields("title","_source").execute().actionGet();
    • Methods of Getrequestbuilder
setFields(String):setIndex(String) , setType(String) , setId(String):setRouting(String):setParent(String):setPreference(String): 【_local, _primary】setRefresh(Boolean): 【false】setRealtime(Boolean):【true】
    • How to respond to GetResponse
isExists():getindex():getType():getId():getVersion():isSourceEmpty():getSourceXXX():【getSourceAsString(),getSourceAsMap(),getSourceAsBytes()】getField(String):
8.5.2 Index Document
IndexResponae response=client.prepareIndex("library","book","2")  .setSource("{\"title\":\"Mastering ElasticSearch\"}")  .execute().actionGet();
    • The constructor object provides the following methods:
setSource():setlndex(String), setType(String), setId(String):setRouting(String) , setParent(String):setOpType():【index,create】setRefresh(Boolean):【false】setReplicationType():【sync,async,default】setConsistencyLevel():【DEFAULT,ONE,QUORUM,ALL】多少副本活跃才进行该操作setVersion(long):多亏了这个方法。程序可以确保在读取和更新文档期间没有别人更改这个文档。setVersionType(VersionType):setPercolate(String):setTimestamp(String):setTTL(long):超过这个时长后文档会被自动删除getlndex():返回请求的索引名称getType():返回文档类型名称getId():返回被索引文档的IDgetVersion():返回被索引文档的版本号getMatches():返回匹配被索引文档的过滤器查询列表,如果没有匹配的查询,则返回null
8.5.3 Updating documents
Map<String, Object>params=Maps.newHashMap();params.put("ntitle","ElasticSearch Server Book");UpdateResponse response = client.prepareUpdate("library","book","2")  .setScript("ctx._source.title=ntitle")  .setScriptParams(params)  .execute().actionGet();
    • Updaterequestbuilder method
Setindex (String), SetType (String), SetId (String): Setrouting (String), SetParent (String): Setscript (String): Setscriptlang (String): Setscriptparams (Map<string, object>): Addscriptparam (String, Object): Setfields (string ...): setretryonconflict (int): The default value is 0. In Elasticsearch, updating a document means retrieving the old version of the document, modifying its structure, removing the old version from the index, and then re-indexing the new version. This means that the target document may be modified by another program between retrieving the old version and the new version of the person who wrote it. Elasticsearch detects changes by comparing the document version number and returns an error if a modification is found. In addition to returning errors directly, you can also choose to retry this operation. The number of retries can be specified by this method. Setrefresh (Boolean): "false" Setrepliactiontype (): "Sync, async, default". This method is used to control the type of replication during the update process. By default, the update operation is considered successful only after all replicas have been updated, corresponding to the value of sync or an equivalent enumeration value. Another option is to return directly without waiting for the operation on the replica to complete, with the corresponding value being an async or equivalent enumeration value. Another option is to have elasticsearch decide how to operate based on the node configuration, which is the default or equivalent enumeration value. Setconsistencylevel (): "Default,one,quorum,all" This method sets the number of active replicas to be able to perform the update operation. Setpercolate (String): This method causes the index document to undergo a percolator check, whose argument is a query string that restricts the percolator query. A value of ' * ' means that all queries are checked. Setdoc (): This type of method is used to set up document fragments that are merged into documents with the same ID in the index. If script is set through the Setscript () method, the document is ignored. Elasticsearch provides multiple versions of this method, each of which requires input strings, byte arrays, Xcontentbuilder, maps, and many other types of documents. SetSource (): SetdocsasupSERT (Boolean): The default value is False.  When set to True, if the specified document does not exist in the index, the document used by the Setdoc () method will be added to the index as a new document.
    • The response returned by the update request is a Updateresponse object method:
getIndex():getType():getld():getVersion():getMatches():getGetResult():
8.5.4 Deleting a document
DeleteResponse response=client.prepareDelete("library","book","2")    .execute().actionGet();
    • Deleterequestbuilder method
setIndex(String), setType(String), setId(String):setRouting(String), setParent(String):setRefresh(Boolean):setVersion(long):本方法指定索引时被删除文档的版本号。如果指定ID的文档不 存在,或者版本号不匹配,则删除操作会失败。这个方法确保了程序中没有别人更改这个文档。 setVersionType(VersionType):本方法告知ElasticSearch使用哪个版本类型。setRepliactionType():【sync, async和default】setConsistencyLevel():本方法设定有多少个活跃副本时才能够执行更新操作。【DEFAULT,ONE,QUORUM,ALL】
    • The response of the delete operation Deleteresponse class provides the following methods:
getIndex():返回请求的索引名称。getType():返回文档类别名称。getId():返回被索引文档的ID。getVersion():返回被索引文档的版本号。isNotFound():如果请求未找到待删除文档,则返回true
8.6 elasticsearch Query 8.6.1 prepare query request
SearchResponse response=client.prepareSearch("library"    .addFields(‘title","_source")    .execute().actionGet();for(SearchHit hit:response.getHits().getHits()){  System.out.println(hit.getId());  if (hit.getFields().containsKey("title"}){      System.out.println("field.title: "                            +hit.getFields().get("title").getValue())    }      System.out.println("source.title: "                             +hit.getSource().get("title"));     }
8.6.2 structure Query
QueryBuilder queryBuilder=QueryBuilders    .disMaxQuery()      .add(QueryBuilders.termQuery("title","Elastic"))      .add(QueryBuilders.prefixQuery("title","el"));System.out.println(queryBuilder.toString());SearchResponse response=client.prepareSearch("library")  .setQuery(queryBuilder)  .execute().actionGet();

Match query

queryBuilder=QueryBuilders    .matchQuery("message","a quick brown fox")    .operator(Operator.AND)    .zeroTermsQuery(ZeroTermsQuery.ALL);

Use geolocation queries

queryBuilder = QueryBuilders.geoShapeQuery("location",  ShapeBuilder.newRectangle()      .topLeft(13, 53)      .bottomRight(14, 52)    .build());
8.6.3 Sub-page
SearchResponse response=client.prepareSearch("library")  .setQuery(QueryBuilders.matchAllQuery())    .setFrom(10)    .setSize(20)    .execute().actionGet();
8.6.4 Sort
SearchResponse response=client.prepareSearch("library")  .setQuery(QueryBuilders.matchAllQuery())  .addSort(SortBuilders.fieldSort("title"))  .addsort("_score",SortOrder .DESC)  .execute().actionGet();

In addition to the above sorting methods, ELASTICSEARCB also provides a script-based sorting method: and scriptSort(String, String) a method based on spatial distance sorting:geoDistanceSort(String).

8.6.5 Filtration
FilterBuilder filterBuilder=FilterBuilders  .andFilter(     FilterBuilders .existsFilter("title").filterName("exist”),     FilterBuilders.termFilter("title","elastic")  );SearchResponse response=client.prepareSearch("library")  .setFilter(filterBuilder)  .execute().actionGet();
Calculation of 8.6.6 Facets
FacetSBuilder facetBuilder=FacetBuilders  .filterFacet("test")  .filter(FilterBuilders .termFilter("title","elastic"));SearchResponse response=client.prepareSearch("library")  .addFacet(facetBuilder)  .execute()actionGet();
8.6.7 Highlight
SearchResponse response=client.preparesearch("wikipedia")  .addHighlightedField("title")  .setQuery(QueryBuilders.termQuery("title","actress"))  .setHighlighterPreTags("<1>", "<2>")  .setHighlighterPostTags("</1>"," </2>").execute().actionGet();
    • Highlight Processing
for(SearchHit hit:response.getHits().getHits()){  HighlightField hField = hit.getHighlightFields()get("title");  for (Text t:hField.fragments()){    System.out.println(t.string());  }}
8.6.8 Query Suggestions
SearchResponse response=client.prepareSearch("wikipedia")  .setQuery(QueryBuilders.matchAllQuery())  .addSuggestion(new TermSuggestionBuilder("first_suggestion")      .text("graphics designer")      .field("_all")  )  .execute().actionGet()
    • Result processing
for(Entry<? extends Option> entry: response.getSuggest()  .getSuggestion("first_suggestion").getEntries()){    System.out.println("Check for: "      +entry.getText()      +". Options:");  for(Option option: entry.getOptions()){    System.out.println("\t"+option.getText());  }
8.6.9 Count

Sometimes, we don't care about which documents are returned, but just the number of results that match. In this case, we need to use a count request because it performs better: there is no need to sort, and you do not need to take the document out of the index.

CountResponse response=client.prepareCount("library")  .setQuery(QueryBuilders.termQuery("title","elastic"))  .execute().actionGet();
8.6.10 scrolling

To get a large collection of documents using the scrolling feature of the Elasticsearch Java API:

SearchResponse responseSearch=client.prepareSearch("library" )  .setScroll("1m")  .setSearchType(SearchType.SCAN)  .execute().actionGet();String scrolled=responseSearch.getScrollId();SearchResponse response = client.prepareSearchScroll(scrollld)  .execute().actionGet(),

There are two requests: The first request specifies the query criteria and the scrolling properties, such as the effective duration of the scroll (using the Setscroll () method); The second request specifies the type of the query, as here we switch to scan mode. In scan mode, sorting is ignored and only the take-out document operation is performed.

8.7 Performing multiple operations 8.7.1 bulk operations in bulk
BulkResponse response=client.prepareBulk()    .add(client.prepareIndex("library", "book","5")      .setSource("{\"title\":\"Solr Cookbook\"}"  .request()  .add(client.prepareDelete("library", "book", "2").request()).execute().actionGet();

The request adds a document (ID 5) to the boo that is indexed by iibrary, and removes a document from the index (ID 2). Once the response is received, an array of Org.elasticsearch.action.bulk.BulkItemResponse objects can be obtained through the GetItems () method.

8.7.2 Delete a document based on a query
DeleteByQueryResponse response=client.prepareDeleteByQuery("library")  .setQuery(QueryBuilders.termQuery("title","ElasticSearch"))  .execute().actionGet();
8.7.3 Multi GET
MultiGetResponse response = client.prepareMultiGet()    .add("library", "book", "1","2")    .execute().actionGet();

The response object for this request contains a getresponses () method, and the method returns an org. An array of Elasticsearch.action.get.MultiGetItemResponse objects.

8.7.4 Multi Search
MultiSearchResponse response=client.prepareMultiSearch()  .add(client.prepareSearch("library","book").request())  .add(client.prepareSearch("news").  .setFilter(FilterBuilders.termFilter("tags", "important")))  .execute().actionGet();

This operation consists of a getresponses () method. The method returns an array of Elasticsearch.action.search.MultiSearchResponse.Item objects.

8.8 Percolator

Percolator is the inverse process of the query. We can find all the queries that match it based on a document. Given a PRC ltr index, we can use the following code to index a query to the prcltr type of the _percolator index:

client.prepareIndex(”_percolator","prcltr","query:1")  .setSource(XContentFactory.jsonBuilder()      .startObject()      .field("query",              QueryBuilders .termQuery("test","abc"))              .endObject())      .execute().actionGet();

In this example, we define a query with the ID "query:1" to check if the test field contains the value ABC. Now that Percolator is ready, you can send a document to it using the following code snippet:

PercolateResponse response =client.preparePercolate("prcltr","type")    .setSource(XContentFactory.jsonBuilder()      .startObject()        .startObject("doc")        .field("test").value("abc")        .endObject()      .endObject())    .execute().actionGet();

The document we send should be matched to the query stored in percolator, which can be checked by the Getmatches () method. You can use the following code:

for (String match:response.getMatches()){  System.out.println("Match:"+match);}
8.9 explain API

The last API for querying Elasticsearch is the explain API. The Explain API can help check for issues related to relevance and indicate the basis of whether or not a document is matched. Take a look at the following code:

ExplainResponse response=client  .prepareExplain(“library","book","1")      .setQuery(QueryBuilders.termQuery("title","elastic"))      .execute().actionGet();
8.11 Management API

Elasticsearch divides management operations into two categories: Cluster management and index management. Let's first look at the first class.

8.11.1 Cluster Management API
ClusterAdminClient cluster=client.admin().cluster()

Cluster and index health status API

ClusterHealthResponse response=client.admin().cluster()  .prepareHealth("library")  .execute().actionGet();

In the response, you can read information such as the cluster state, number of allocated shards, total number of slices, number of copies of a particular index, and so on.

Cluster Status API
The cluster status API allows us to obtain information about the cluster, such as routing, Shard allocation, and mapping.

ClusterStateResponse response=client.admin().cluster()    .prepareState()    .execute().actionGet();

Setting up the Update API
Set the update API to set the cluster-wide configuration parameters.

Map<String, Object> map=Maps.newHashMap()map.put("indices.ttl.interval","10m");ClusterUpdateSettingsResponse response=client.admin().cluster()  .prepareUpdateSettings()  .setTransientSettings(map)  .execute().actionGet();

Rerouting API
The rerouting API can move shards between nodes and cancel or force shard allocation behavior.

ClusterRerouteResponse response=client .admin().cluster()  .prepareReroute()      .setDryRun(true)      .add(        new MoveAllocationCommand(new ShardId("library",3),                              "G3czOt4HQbKZT1RhpPCULw",                                PvHtEMuRSJ6rLJ27AW3U6w"),        new CancelAllocationCommand(new ShardId("‘library", 2),                              "G3czOt4HQbKZT1RhpPCULw",                               true))  .execute().actionGet();

Node Information API
The node information (nodes information) API provides information for one or more specific nodes. The API outputs information that covers the Java virtual machine,
Operating systems and networks (such as IP addresses or LAN addresses, plug-in information, etc.).

NodesInfoResponse response = client.admin().cluster()    .prepareNodesInfo()      .setNetwork(true)      .setPlugin(true)    .execute().actionGet();

Node Statistics API
The node statistics API is similar to the node information API, except that it outputs information about elasticsearch usage, such as index statistics, file systems, HTTP modules, Java virtual machines, and so on.

NodesStatsResponse response=client.admin().cluster()    .prepareNodesStats()    .all()    .execute().actionGet();

Node Hotspot thread API
The node Hotspot thread API is used to check node state when Elasticsearch fails or CPU usage exceeds normal.

NodesHotThreadsResponse response=client.admin().cluster()  .prepareNodesHotThreads()  .execute().actionGet();

Node Shutdown API
Node shutdown APL is relatively simple, it allows us to close the specified node (or all nodes). If necessary, you can also set the shutdown delay time. For example, the following code will immediately close the entire cluster:

NodeaShutdownResponse response=client.admin().cluster()  .prepareNodesShutdown()  .execute().actionGet();

Querying the Shard API
The query Shard (Searchshard) API is also relatively simple, which allows us to check which nodes will be used to process the query. It can also set routing parameters, so it is especially handy when using custom routes.

ClusterSearchShardsResponse response=client.admin().cluster()    .prepareSearchShards()      .setIndices("library")      .setRouting("12")    .execute().actionGet();
8.11.2 Index management API
IndicesAdminClient cluster = client.admin().indices();

Index exists API

Type exists API

Index Statistics API
The index statistics (INDICESSTATS) API provides information about indexes, documents, storage, and operations such as fetch, query, index, preheater, merge process, empty buffer, refresh, and so on.

Index status

Index Segment Information API

Create INDEX API

Delete Index API

Close Index API

Open Index API

Refresh API

Emptying the buffer API

Index Optimization API

Setting up the Mapping API

Delete Mapping API

Alias API

Get the alias API

Alias Presence API

Emptying the Cache API

Update Settings API

Analytics API

Setting up the template API

Remove Template API

Query Validation API

Set up the Preheater API

Remove the Preheater API

No. 08 Chapter ElasticSearch Java API

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.