หน้าเว็บ

แสดงบทความที่มีป้ายกำกับ elastic search แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ elastic search แสดงบทความทั้งหมด

วันเสาร์ที่ 17 สิงหาคม พ.ศ. 2556

elastic search simple search : java

Continued from elastic search indexing : java

        This example i will show how to use java api for search from elastic search server that i config on this topic install elastic search on ubuntu 12.04. this example is simple search using java api if you need advance you can learn more at http://www.elasticsearch.org/guide/reference/java-api/search/

Create ElasticSearch.java and have the following code.
package com.blogspot.na5cent.elasticsearch;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;

/**
 *
 * @author redcrow
 */
public abstract class ElasticSearch {

    //point to elastic search server
    private TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.34", 9300);

    //implements by another, return client to argument
    public abstract void perform(Client client);

    public void execute() {
        Client client = null;
        try {
            client = new TransportClient().addTransportAddress(transportAddress);
            perform(client);
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }
}
Search.java

elastic search indexing : java

        before you must install elastic search server, can learn at : http://na5cent.blogspot.com/2013/08/install-elastic-search.html


maven project
add dependencies (pom.xml)
note : dependencies version must same elastic search server version
<dependencies>
    <!-- elastic search @ http://www.elasticsearch.org/guide/reference/java-api/ -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>0.90.3</version>
    </dependency>
    <!-- elastic search ******************************************************** -->

    ...
    ...
    ...
</dependencies>
ExlasticIndexing.java

install elastic search on ubuntu 12.04

system evironment
    server : ubuntu 12.04
    user : elastic
    user group : elastic

elastic search
    version : 0.90.3
2. download elasticsearch จาก https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.3.tar.gz
$ cd ~
$ mkdir Download
$ cd Download
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.3.tar.gz
3. copy to /opt/  and change name to elasticsearch
# copy folder and sub folder (-r) into /opt/
$ sudo cp -r elasticsearch-0.90.3.tar.gz /opt/ 

# extract file
$ sudo tar xzf elasticsearch-0.90.3.tar.gz

# rename folder
$ sudo mv elasticsearch-0.90.3 elasticsearch 

# remove .tar.gz file
$ sudo rm -r elasticsearch-0.90.3.tar.gz
4. change group and authorize
# owner can read write and execute, but other can read only
$ sudo chmod 744 -R elasticsearch

#change group to folder and sub folder (-R)
$ sudo chown -R elastic:elastic elasticsearch
5. start elastic search
...
# run elasticsearch
$ ./elasticsearch/bin/elasticsearch 
...
6. check working
# download json from localhost:9200 not save file (show output console only)
$ wget -qO- localhost:9200

# response
{ 
  "ok" : true,
  "status" : 200,
  "name" : "Fantastic Four",
  "version" : {
    "number" : "0.90.3",
    "build_hash" : "5c38d6076448b899d758f29443329571e2522410",
    "build_timestamp" : "2013-08-06T13:18:31Z",
    "build_snapshot" : false,
    "lucene_version" : "4.4"
  },
  "tagline" : "You Know, for Search"
}

running elastic search as service