ElasticSearch 7基本操作


関連内容:ElasticSearch 7全文検索、キーワードハイライトを実現
本編概要:
  • 1. 基礎操作
  • 1.1インデックス作成;
  • 1.2挿入;
  • 1.3修正;
  • 1.4削除;

  • 2. クエリー;
  • 2.1単純クエリー、条件クエリー、集約クエリー;
  • 2.2高度なクエリー;
  • 2.2.1サブ条件クエリー;
  • 2.2.2複合条件クエリー;



  • 1.基礎操作;
    1.1インデックスの作成
    #       :        ,mappings   {}
    
    #      :
    # type     text:1.    ,      ,       2.     、     3.      
    # type     keyword:1.      ,    ,        2.     、     3.     
    curl -H 'Content-Type: application/json' \
    -XPUT "http://localhost:9201/book/?pretty" -d '{
    	"settings": {
    		"refresh_interval": "20s",
    		"number_of_shards": 1,
    		"number_of_replicas": 0
    	},
    	"mappings": {
    		"properties": {
    			"title": {"type": "text"},
    			"author": {"type": "keyword"},
    			"word_count": {"type": "integer"},
    			"publish_date": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
    			"type": {"type": "text"}
    		}
    	}
    }'
    

    1.2挿入;
    #      id   
    curl -H 'Content-Type: application/json' \
    -XPUT "http://localhost:9201/book/_doc/1?pretty" -d '{
    	"title": "xxx",
    	"author": "unknown",
    	"publish_date": "2019-12-13",
    	"word_count": 10000
    }'
    
    #        id   
    curl -H 'Content-Type: application/json' \
    -XPOST "http://localhost:9201/book/_doc?pretty" -d '{
    	"title": "yyy",
    	"author": "unknown",
    	"publish_date": "2019-12-13",
    	"word_count": 20000
    }'
    

    1.3修正;
    #     :   
    
    #       
    curl -H 'Content-Type: application/json' \
    -XPOST "http://localhost:9201/book/_doc/1/_update?pretty" -d '{
    	"doc" : {"title": "xxx1"}
    }'
    
    #       
    #      :script
    #        lang:   painless / js / python
    #       :inline
    # es    :ctx
    # es     :_source
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_update_by_query' -d '{
    	"query": {
    		"match": { "title": "xxx" }
    	},
    	"script": {
    		"lang": "painless",
    		// "inline": "ctx._source.word_count += 10",
    		"inline": "ctx._source.word_count = params.word_count",
    		"params": { "word_count" : 100000 }
    	}
    }'
    

    1.4削除
    #     
    curl -H 'Content-Type: application/json' \
    -XDELETE 'http://localhost:9200/book/_doc/1'
    
    #     
    curl -H 'Content-Type: application/json' \
    -XDELETE 'http://localhost:9200/book'
    
    #     
    curl -H 'Content-Type: application/json' -u elastic -XPOST \ 'http://localhost:9200/book/_doc/_update_by_query?pretty' -d '{
         "script":{
         "lang":"painless","inline":"ctx._source.remove(\"word_count\")"}}
    

    2.照会;
    2.1単純クエリー、条件クエリー、集約クエリー;
    # 1.     
    # 1.1     
    curl -H 'Content-Type: application/json' \
    -XGET 'http://localhost:9200/book/_doc/1?pretty'
    
    # 1.2     、     
    #         _search         ID。
    #       hits                
    #             10    
    #      、           q=
    curl -H 'Content-Type: application/json' \
    -XGET 'http://localhost:9200/book/_doc/_search?q=title:xxx&pretty'
    
    # 2.     
    #    DSL     
    # 2.1       
    # size       
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"match_all": {}
    	},
    	"from": 1,	
    	"size": 1
    }'
    
    # 2.2      
    # match        
    # sort          (      ,    _score   null)
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"match": { "title" : "xxx" }
    	},
    	"sort": [
    		{ "publish_date": { "order" : "desc" } }
    	]
    }'
    
    # 3.     
    # 3.1        :group_by_word_count,        
    # field        
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"aggs": {
    		"group_by_word_count": {
    			"term": {
    				"field": "word_count"
    			}
    		},
    		"group_by_publish_date" :{
    			"term": {
    				"field": "publish_date"
    			}
    		}
    	}
    }'
    
    # 3.2   
    # stats        
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"aggs": {
    		"grades_word_count": {
    			"stats": {
    				"field": "word_count"
    			}
    		}
    	}
    }'
    

    2.2高級クエリー;
    2.2.1サブ条件クエリ;
    特定フィールドクエリが指す特定の値は、Query contextとFilter contextに分けられます.
  • Query context:クエリー中に、ドキュメントがクエリー条件を満たしているかどうかを判断するほか、ElasticSearchは1つの_を計算します.scoreは、ターゲットドキュメントとクエリー条件の一致がどれほど良いかを判断するために、一致の程度を識別します.一般的なクエリーは次のとおりです.
  • 全文このクエリ:テキストタイプデータ
  • フィールドレベルクエリー:数値、日付などの構造化データの
  • # 1.      ,     、    、        、    
    # 1.1     ,    match
    #     highlight,  
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"match": { "title": "xxx" }
    	},
    	"highlight": {
            "fields" : {
                "title" : {}
            }
        }
    }'
    
    # 1.2     ,    match_phrase
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"match_phrase": { "title": "xxx" }
    	}
    }'
    
    # 1.3         ,    multi_match
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"multi_match": {
    			"query": "xxx",
    			"fields": ["author", "title"]
    		}
    	}
    }'
    
    # 1.4     :               ,
    #         ,     、    、    、     
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"query_string": {
    			// 1.4.1
    			"query": "xxx AND yyy"
    			// 1.4.2
    			// "query": "(xxx AND yyy) OR zzz"
    			// 1.4.3
    			// "query": "xxx OR zzz"
    			// "fields": ["author", "title"]
    		}
    	}
    }'
    
    # 2.       
    # 2.1 
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"term": {
    			"word_count": 1000,
    		}
    	}
    }'
    
    # 2.2       
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"range": {
    			"word_count": {
    				"gte" : 1000,
    				"lte" : 2000
    				// "gte" : "2019-12-01",
    				// "lte" : "now"
    			}
    		}
    	}
    }'
    
  • Filter context:クエリー中にドキュメントが条件を満たしているかどうかを判断するのは、YESまたはNO
  • のみです.
    # filter        ,ElasticSearch         
    #    query    ,   bool     
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"bool": {
    			"filter": {
    				"term": {
    					"word_count": 1000
    				}
    			}
    		}
    	}
    }'
    

    2.2.2複合条件照会;
    一定の論理組合せサブ条件でクエリー
  • 固定スコアクエリ
  • #     
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"match": { "title": "xxx" }
    	}
    }'
    
    #       
    #     match,    filter
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"constant_score": {
    			"filter": {
    				"match": { "title": "xxx" }
    			},
    			"boost": 2
    		}
    	}
    }'
    
    
  • ブールクエリ
  • #     should,“ ”   ,         
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"bool": {
    			"should": [
    				{ "match": { "author": "xxx" } },
    				{ "match": { "title": "yyy" } }
    			]
    		}
    	}
    }'
    
    #     must,      
    #     filter   
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"bool": {
    			"must": [
    				{ "match": { "author": "xxx" } },
    				{ "match": { "title": "yyy" } }	
    			],
    			"filter": [
    				{ "term": { "word_count": 1000 } }
    			]
    		}
    	}
    }'
    
    #     must_not
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
    	"query": {
    		"bool": {
    			"must_not": {
    				"term": { "author": "xxx" }
    			}
    		}
    	}
    }'
    
    #   
    curl -H 'Content-Type: application/json' \
    -XPOST 'http://localhost:9200/book/_doc/_search?pretty' -d '{
         
    	"query": {
         
    		"bool": {
         
    			"must": [
    				{
          "match": {
          "author": "xxx" } }, 
    				{
          "match": {
          "title": "yyy" } }
    			],
    			"filter": [{
         
    				"range": {
         
    					"publish_date": {
         
    						"gte": 1577203200,
    						"lte": 1577203203
    					}
    				}
    			}]
    		}
    	},
    	"_source": "publish_date",
    	"size": 1000,
    	"sort": [{
         
    		"publish_date": {
         
    			"order": "asc"
    		}
    	}]
    }