Elasticsearch study notes

Install Elasticsearch
1: Unzip the downloaded installation package elasticsearch-1.7.2.zip and
modify cluster.name: es (the cluster state name is the same)
2: After downloading the plugin at https://github.com/elasticsearch/elasticsearch-servicewrapper, unpack compression. Copy the service directory to the bin directory of the elasticsearch directory.
3: Install the es-head plugin
Enter the elasticsearch/bin directory and enter the command ./plugin --install mobz/elasticsearch-head to install the head plugin
4: scp Elasticsearch to other machines
5: Start Elasticsearch
in the linux environment and enter the bin of the ES directory /service directory, as shown in the figure below, run the command sh elasticsearch start to access the address http://hadoop1:9200/_plugin/head/




Elasticsearch JavaAPI

to add Elasticsearch maven dependency
<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>1.7.2</version>
</dependency>


package test;

import java.io.IOException;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import entity.User;
/*
 * return json data
 */
public class JsonUtil {
	public static String obj2JsonData(User user){
        String jsonData = null;
        try {
            //Create json data using XContentBuilder
            XContentBuilder jsonBuild = XContentFactory.jsonBuilder();
            jsonBuild.startObject ()
            .field("id",user.getId())
            .field("name", user.getName())
            .field("pwd",user.getPwd())
            .endObject();
            jsonData = jsonBuild.string();
            System.out.println(jsonData);
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return jsonData;
    }
}

package test;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import entity.User;

public class ElasticsearchTest {
	customer customer;
  /*
   * Initialize client
   */
	public ElasticsearchTest() {
		client = new TransportClient()
				.addTransportAddress(new InetSocketTransportAddress("hadoop1",9300));
	}
    /*
     * Add to
     */
	public IndexResponse createIndexResponse(String indexname, String type,
			String jsondata) {
		IndexResponse response = client.prepareIndex(indexname, type)
				.setSource(jsondata).execute().actionGet();
		return response;
	}

	/**
	 * perform search
	 *
	 * @param queryBuilder
	 * @param indexname
	 * @param type
	 * @return
	 */
	public List<User> searcher(QueryBuilder queryBuilder, String indexname,
			String type) {
		List<User> list = new ArrayList<User>();
		SearchResponse searchResponse = client.prepareSearch(indexname)
				.setTypes(type).setQuery(queryBuilder).execute().actionGet();
		SearchHits hits = searchResponse.getHits();
		System.out.println("Number of records queried=" + hits.getTotalHits());
		SearchHit[] searchHists = hits.getHits();
		if (searchHists.length > 0) {
			for (SearchHit hit : searchHists) {
				Integer id = (Integer) hit.getSource().get("id");
				String name = (String) hit.getSource().get("name");
				String function = (String) hit.getSource().get("pwd");
				list.add(new User(id, name, function));
			}
		}
		return list;
	}

	public static void main(String[] args) {

		ElasticsearchTest es = new ElasticsearchTest();
//		User user = new User();
//		user.setId(1);
// user.setName("Zhang San");
//		user.setPwd("123");
//		String jsondata = JsonUtil.obj2JsonData(user);
		String indexname = "indexdemo";
		String type = "typedemo";
		// es.createIndexResponse(indexname, type, jsondata);
		// Query conditions
		QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "张");
		/*
		 * QueryBuilder queryBuilder = QueryBuilders.boolQuery()
		 * .must(QueryBuilders.termQuery("id", 1));
		 */
		List<User> result = es.searcher(queryBuilder, indexname, type);
        for (User us : result) {
			System.out.println(us.getName());
		}
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326356787&siteId=291194637