Elasticsearch

API 정리

재심 2022. 10. 30. 14:44

[CRUD]

GET

$ curl -XGET http://localhost:9200:/classes?pretty
 

PUT (인덱스 생성)

$ curl -XPUT http://localhost:9200:/classes
 

POST (Document 생성)

$ curl -X POST http://localhost:9200:/classes/class/1 -H 'Content-Type: application/json' -d '{"title":"Algorithm","professor":"John"}'

#file
$ curl -X POST http://localhost:9200:/classes/class/1 -H 'Content-Type: application/json'-d @filename.json
 

UPDATE

$ curl -X POST http://localhost:9200:/classes/class/1_update -H 'Content-Type: application/json' d '{"doc": {"unit": 2}}'

$ curl -X POST http://localhost:9200/classes/class/1/_update?pretty -H 'Content-Type: application/json' -d '{"script":"ctx._source.unit+=5"}'
 

BULK POST

$ curl -X POST http://localhost:9200/_bulk?pretty -H 'Content-Type: application/json' --data-binary @classes.json
 
$ classes.json (https://github.com/minsuk-heo/BigData/blob/master/ch01/classes.json)

 

[Search]

https://github.com/minsuk-heo/BigData/tree/master/ch03

 

GitHub - minsuk-heo/BigData

Contribute to minsuk-heo/BigData development by creating an account on GitHub.

github.com

# bulk data insert
$ curl -X POST http://localhost:9200/_bulk?pretty -H 'Content-Type: application/json' --data-binary @simple_basketball.json
 
# Search
$ curl -XGET http://localhost:9200/basketball/record/_search?pretty
 
# Search With url
$ curl -XGET 'http://localhost:9200/basketball/record/_search?q=points:30&pretty'
 
# Search with request body
$ curl -XGET http://localhost:9200/basketball/record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"term":{"points":30}}}'

 

[Aggregation]

Metric Aggregation

$ curl -X POST http://localhost:9200/_bulk?pretty -H 'Content-Type: application/json' --data-binary @simple_basketball.json

https://github.com/minsuk-heo/BigData/blob/master/ch03/simple_basketball.json

 

GitHub - minsuk-heo/BigData

Contribute to minsuk-heo/BigData development by creating an account on GitHub.

github.com

 

https://github.com/minsuk-heo/BigData/blob/master/ch03/

 

GitHub - minsuk-heo/BigData

Contribute to minsuk-heo/BigData development by creating an account on GitHub.

github.com

#avg
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @avg_points_aggs.json
 
#max
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @max_points_aggs.json 
 
#min
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @min_points_aggs.json  
 
#sum
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @sum_points_aggs.json   
 
#stats
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @stats_points_aggs.json

Bucket Aggregation (Group By)

$ curl -XPUT 'http://localhost:9200/basketball'

https://github.com/minsuk-heo/BigData/blob/master/ch04/basketball_mapping.json

 

GitHub - minsuk-heo/BigData

Contribute to minsuk-heo/BigData development by creating an account on GitHub.

github.com

 

#create mapping
$ curl -XPUT 'http://localhost:9200/basketball/record/_mapping?include_type_name=true' -H 'Content-Type: application/json' -d @basketball_mapping.json
 
#insert document
$ curl -XPOST 'http://localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' --data-binary @twoteam_basketball.json
# terms aggs
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @terms_aggs.json
 
# stats group by team
$ curl -XGET 'http://localhost:9200/_search?pretty' -H 'Content-Type: application/json' --data-binary @stats_by_team.json

 

[Mapping (Schema)]

https://raw.githubusercontent.com/minsuk-heo/BigData/master/ch02/classesRating_mapping.json

 

{
    "class" : {
        "properties" : {
            "title" : {
                "type" : "text"
            },
            "professor" : {
                "type" : "text"
            },
            "major" : {
                "type" : "text"
            },
            "semester" : {
                "type" : "text"
            },
            "student_count" : {
                "type" : "integer"
            },
            "unit" : {
                "type" : "integer"
            },
            "rating" : {
                "type" : "integer"
            },
            "submit_date" : {
                "type" : "date",
                "format" : "yyyy-MM-dd"
            },
            "school_location" : {
                "type" : "geo_point"
            }
        }
    }
}
$ curl -XPUT 'http://localhost:9200/classes/class/_mapping?include_type_name=true' -H 'Content-Type: application/json' -d @classesRating_mapping.json

 

'Elasticsearch' 카테고리의 다른 글

로컬에 ELK 환경 구성해보기  (0) 2022.10.30
Elasticsearch 시스템 구성 가이드  (0) 2022.10.30
용어 정리  (0) 2022.10.30