読者です 読者をやめる 読者になる 読者になる

ElasticSearchのお勉強中

elasticsearchを使ってみた

1. データの登録

POST
http://localhost:9200/twitter/tweet


request body

{
        "tweet":"ほげほげ",
        "time": "2014-05-10T00:00:00Z",
        "name": "hase"
}

response

{
    "_index": "twitter",
    "_type": "tweet",
    "_id": "e79Bx6MEQ_u6XhMz4R4sEg",
    "_version": 1,
    "created": true
}


2. 一件取得

GET
http://localhost:9200/twitter/tweet/e79Bx6MEQ_u6XhMz4R4sEg

{
    "_index": "twitter",
    "_type": "tweet",
    "_id": "e79Bx6MEQ_u6XhMz4R4sEg",
    "_version": 1,
    "found": true,
    "_source": {
        "tweet": "ほげほげ",
        "time": "2014-05-10T00:00:00Z",
        "name": "hase"
    }
}


3. 検索

GET
http://localhost:9200/twitter/tweet/_search

o
http://localhost:9200/*/tweet/_search
http://localhost:9200/_all/tweet/_search
http://localhost:9200/twitte*/tweet/_search
http://localhost:9200/twitte*/tweet1,tweet2/_search

x
http://localhost:9200/_all/*/_search

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "twitter",
                "_type": "tweet",
                "_id": "e79Bx6MEQ_u6XhMz4R4sEg",
                "_score": 1,
                "_source": {
                    "tweet": "ほげほげ",
                    "time": "2014-05-10T00:00:00Z",
                    "name": "hase"
                }
            },
            {
                "_index": "twitter",
                "_type": "tweet",
                "_id": "7plY2ahJTLGkhjF6tG3jOQ",
                "_score": 1,
                "_source": {
                    "tweet": "ほげほげ",
                    "time": "2014-05-10T00:00:00Z",
                    "name": "hase"
                }
            },
            {
                "_index": "twitter",
                "_type": "tweet",
                "_id": "vQNGXh-NR0aA6IkyTDR1Ig",
                "_score": 1,
                "_source": {
                    "tweet": "ほげほげ",
                    "time": "2014-05-10T00:00:00Z",
                    "name": "hase"
                }
            }
        ]
    }
}

query
http://localhost:9200/twitter/tweet1,tweet2/_search

request body

{
  "query": {
    "term": {"name":"hase"}
  }
}


response

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.5945348,
        "hits": [
            {
                "_index": "twitter",
                "_type": "tweet1",
                "_id": "V0wxff00TPOjA-ZkjJp5Nw",
                "_score": 0.5945348,
                "_source": {
                    "tweet": "ほげほげ",
                    "time": "2014-05-10T00:00:00Z",
                    "name": "hase"
                }
            },
            {
                "_index": "twitter",
                "_type": "tweet2",
                "_id": "mp78tH-tRRGD_31cQ7Ij-g",
                "_score": 0.30685282,
                "_source": {
                    "tweet": "ほげほげ",
                    "time": "2014-05-10T00:00:00Z",
                    "name": "hase"
                }
            }
        ]
    }
}

http://localhost:9200/twitter/tweet1,tweet2/_search
from_size

{
  "from": 1, "size":1,
  "query": {
    "term": {"name":"hase"}
  }
}

インデックスをうまく設計するのが重要そう


参考:
https://medium.com/hello-elasticsearch/elasticsearch-api-83760ce1424b