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

ElasticSearch indexlenmiş metin aramaya dayalı çok miktarda veri ile çalışmak üzere tasarlanmış Java da geliştirilmiş lucene tabanlı bir platformdur. Restfull API ile sistemle konuşulabilinir. Veri saklama türü ise JSON dur.

https://www.elastic.co/downloads adresinden ElasticSearch u indirebilirsiniz. Bu tarihteki versiyonunu kullanmak için Java 8 Update 20 ve üzeri versiyonları sitesinde tavsiye edilmektedir.(https://www.elastic.co/guide/en/elasticsearch/hadoop/current/requirements.html)

ElasticSearch u indirdiniz ekstra bir kuruluma gerek kalmadan portable kullanabiliyorsunuz. Windows için /bin klasörü içerisinden “elasticsearch.bat” ile run ettiğinizde uygulama ayağa kalkacaktır. Uygulama Restfull olduğu için browser üzerinden direk erişilebilinir olacak. http://localhost:9200 uygulamanın erişim adresi.
Bu adresi browserdan çağırdığınızda aşağıdaki gibi çıktı alırsınız. Geri dönen veri tipi JSON olacaktır. Aslında hep JSON verisi dönüş olarak gelecek.
{
  "status" : 200,
  "name" : "Dracula",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.5.2",
    "build_hash" : "62ff9868b4c8a0c45860bebb259e21980778ab1c",
    "build_timestamp" : "2015-04-27T09:21:06Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

Indexleme mantığına geçelim. ElasticSearch e veri girişi yaparken JSON unuza en az bir index belirtmek zorundasınız. Bunun yanında type ve id veriyoruz.  Önce index ile başlayalım.
Bunun için giriş kısmına geçmeden önce var olan indexlerimizi nasıl sorgulayacağımıza bakalım. http://localhost:9200/_cat/indices?v adresini çağırarak kaç adet index oluşturulduğunu görebilirsiniz.



Bende daha oluşturulan indexlerden dolayı 3 farklı index görülüyor. Örnek bir index sorgu gönderelim.
http://localhost:9200/twitter/_search?pretty=true Bu şekilde twitter indexindeki tüm kayıtlara gidebiliriz.
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"user":"john","postDate":"2015-07-08T08:05:37.255Z","message":"who dont it work"}
    }, {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "2",
      "_score" : 1.0,
      "_source":{"user":"Burak","postDate":"2015-10-12T11:14:04.140Z","message":"Yeni Tweet!"}
    }, {
      "_index" : "twitter",
      "_type" : "tweets",
      "_id" : "5588035d045d1038b246c543",
      "_score" : 1.0,
      "_source":{"created_at":"Sat Apr 25 16:17:26 +0000 2015","id":5.9199946551238246E+17,"id_str":"591999465512382464","text":"[CALENDAR] Barça have 5 league games left, 2 #UCL semi-final games, and the Spanish Cup final: http://t.co/mWKOzNEWFo http://t.co/cyN1ZZNsSx","source":"Hootsuite","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":95.0,"favorite_count":82.0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"possibly_sensitive_appealable":false,"lang":"fr"}
    }, {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "3",
      "_score" : 1.0,
      "_source":{"user":"Burak","postDate":"2015-10-12T11:42:42.744Z","message":"Yeni Tweet2!"}
    } ]
  }
}

Görüldüğü üzere 3 kayıt var.  İlk iki kaydın type ları aynı ve  “tweet” olarak kayıtlı ancak son kayıt “tweets”. “Tweet” type lı kayıtlar ile devam ediyoruz.  
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"user":"john","postDate":"2015-07-08T08:05:37.255Z","message":"who dont it work"}
    }, {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "2",
      "_score" : 1.0,
      "_source":{"user":"Burak","postDate":"2015-10-12T11:14:04.140Z","message":"Yeni Tweet!"}
    }, {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "3",
      "_score" : 1.0,
      "_source":{"user":"Burak","postDate":"2015-10-12T11:42:42.744Z","message":"Yeni Tweet2!"}
    } ]
  }
}
 
Kayıtlardan “Tweets” type lı olanı sorgu sonucundan çıkmış oldu. Id yi 1 yapalım.
 
http://localhost:9200/twitter/tweet/1
 
 
{"_index":"twitter","_type":"tweet","_id":"1","_version":3,"found":true,"_source":{"user":"john","postDate":"2015-07-08T08:05:37.255Z","message":"who dont it work"}}
 
Tek kayıt oldu böylece. Sorgu göndererek bulalım aynı kaydı.
 
http://localhost:9200/twitter/_search?pretty=true&q=user:john
 
 
 
Kullanıcı john olan kaydı buldurarak direk index içerisinde arama yaptırdık.
 
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "1",
      "_score" : 0.30685282,
      "_source":{"user":"john","postDate":"2015-07-08T08:05:37.255Z","message":"who dont it work"}
    } ]
  }
}
 

Yukardaki kayda sorgu ile gittiğimiz için “took” sorgu cevap süresi milisaniye cinsinden alıyoruz. 2.bölümde istemci ile devam edeceğiz. 

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