ELASTICSEARCH NEDİR ? JAVA İSTEMCİ İLE ELASTICSEARCH İŞLEMLERİ -2

ElasticSearch Java istemcisi için Maven kullanacağız. Pom.xml dosyamıza aşağıdaki eklentimizi yapıyoruz.
    
        org.elasticsearch
        elasticsearch
        1.6.0
   

İlk örneğimiz bir Insert işlemi olsun.

public class App
{
public static void main( String[] args )
    {
     Client client = new TransportClient()
    .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

    IndexResponse response = null;
    try {
      response = client.prepareIndex("twitter", "tweet", "3")
          .setSource(XContentFactory.jsonBuilder()
                      .startObject()
                          .field("user", "Burak")
                          .field("postDate", new Date())
                          .field("message", "Yeni Tweet2!")
                      .endObject()
                    )
          .execute()
          .actionGet(); 
    } catch (ElasticsearchException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException io) {
   
        io.printStackTrace();
   
    }

   
    }
}

Insert örneğimiz yukarıdaki gibi. PrepareIndex metodu içerisinde sırasıyla Index-Type-Id tanımları yapılıyor.Source kısmında ise JSON hazırlıyoruz. Bu JSON içeriğinde gayet özgürüz.
Şimdi ki örnekte sorgu gönderelim ;
public class App
{
    public static void main( String[] args )
    {
     Client client = new TransportClient()
    .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
     try {
 SearchResponse response = client.prepareSearch("twitter").setTypes("tweet").setQuery(QueryBuilders.queryStringQuery("user:Burak")).setFrom(0).setSize(60).setExplain(true).execute().actionGet();
    long numHits=response.getHits().totalHits();
 System.out.println(response.toString());
  } catch (ElasticsearchException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
}
}
http://localhost:9200/twitter/tweet/_search?pretty=true&q=user:Burak üstte oluşturduğumuz sorgu aslında karşılığı bu şekilde. Dönen cevap aşağıdaki gibi.

{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_shard" : 4,
      "_node" : "eDsHzJt4R7657fSLL22jYg",
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "3",
      "_score" : 1.0,
      "_source":{"user":"Burak","postDate":"2015-10-12T11:42:42.744Z","message":"Yeni Tweet2!"},
      "_explanation" : {
        "value" : 1.0,
        "description" : "weight(user:burak in 0) [PerFieldSimilarity], result of:",
        "details" : [ {
          "value" : 1.0,
          "description" : "fieldWeight in 0, product of:",
          "details" : [ {
            "value" : 1.0,
            "description" : "tf(freq=1.0), with freq of:",
            "details" : [ {
              "value" : 1.0,
              "description" : "termFreq=1.0"
            } ]
          }, {
            "value" : 1.0,
            "description" : "idf(docFreq=1, maxDocs=2)"
          }, {
            "value" : 1.0,
            "description" : "fieldNorm(doc=0)"
          } ]
        } ]
      }
    }, {
      "_shard" : 3,
      "_node" : "eDsHzJt4R7657fSLL22jYg",
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "2",
      "_score" : 0.30685282,
      "_source":{"user":"Burak","postDate":"2015-10-12T11:14:04.140Z","message":"Yeni Tweet!"},
      "_explanation" : {
        "value" : 0.30685282,
        "description" : "weight(user:burak in 0) [PerFieldSimilarity], result of:",
        "details" : [ {
          "value" : 0.30685282,
          "description" : "fieldWeight in 0, product of:",
          "details" : [ {
            "value" : 1.0,
            "description" : "tf(freq=1.0), with freq of:",
            "details" : [ {
              "value" : 1.0,
              "description" : "termFreq=1.0"
            } ]
          }, {
            "value" : 0.30685282,
            "description" : "idf(docFreq=1, maxDocs=1)"
          }, {
            "value" : 1.0,
            "description" : "fieldNorm(doc=0)"
          } ]
        } ]
      }
    } ]
  }

}

Yorumlar

Bu blogdaki popüler yayınlar

IONIC BAŞLANGIÇ

Cannot resolve the collation conflict between “Turkish_CI_AS” and “SQL_Latin1_General_CP1_CI_AS” in the equal to operation

Golang working with interfaces and functions -3