ES 6.4 操作记录

1、 创建mapping

      keyword:存储数据时候,不会分词建立索引

     text:存储数据时候,会自动分词,并生成索引

(https://blog.csdn.net/qq_26230421/article/details/81947517)

"author2": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },

    ignore_above 对超过 ignore_above 的字符串,analyzer 不会进行处理;所以就不会索引起来

put book 
{  
      "mappings": {
         "it": {
            "properties": {
               "author": {
                  "type": "keyword"
                },
               "author2": {
                  "type": "keyword"
                },
               "bookid": {
                  "type": "long"
               },
               "bookname": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },
               "country": {
                  "type": "keyword"
                },
               "publishDate": {
                  "type": "date"
               },
               "reader": {
                  "type": "long"
               }
            }
         }
      }
}
扫描二维码关注公众号,回复: 5451820 查看本文章

2、插入数据 

put 插入指定id的数据

post 插入不指定ID的数据,修改数据post  +  _update

put  book/it/1
{
    "bookid":1,
    "country":"中国",
    "bookname":"java 学习",
    "publishDate":"2018-02-02",
    "author":"weidong",
    "author2":"jhon",
    "reader":10

}

put  book/it/2
{
    "bookid":1,
    "country":"中国",
    "bookname":"php 学习",
    "publishDate":"2018-07-02",
    "author":"wd",
    "author2":"jhon",
    "reader":20
}
put  book/it/3
{
    "bookid":1,
    "country":"英国",
    "bookname":"C 学习",
    "publishDate":"2018-04-02",
    "author":"wd",
    "author2":"jhon2",
    "reader":13
}
 
put  book/it/4
{
    "bookid":1,
    "country":1,
    "bookname":"C 学习",
    "publishDate":"2018-04-02",
    "author":"wd",
    "author2":"jhon 2",
    "reader":13
}
put  book/it/5
{
    "bookid":1,
    "country":1,
    "bookname":"C 学习",
    "publishDate":"2018-04-02",
    "author":"wd",
    "author2":"jhon2",
    "reader2":1
}

3、查询

https://blog.csdn.net/g1969119894/article/details/80111067#nested-%E5%B5%8C%E5%A5%97%E6%9F%A5%E8%AF%A2(

Elasticsearch5.6.X 的Query DSL种类汇总

)

 match查询会先对搜索词进行分词,分词完毕后再逐个对分词结果进行匹配,因此相比于term的精确搜索,match是分词匹配搜索

match搜索会先对搜索词进行分词,对于最基本的match搜索来说,只要搜索词的分词集合中的一个或多个存在于文档中即可,例如,当我们搜索中国杭州,搜索词会先分词为中国杭州,只要文档中包含搜索杭州任意一个词,都会被搜索到

列如:

GET  book/it/_search
{
    "query": {
        "match": {
           "author2": "jhon"
        }
    }
}

查询bookname等于C 学习 ,bookname为text类型

短语匹配 - match_phrase

短语匹配会分析文本,并从分析文本中创建短语查询,不会将查询短语拆开。

GET  book/it/_search
{
    "query": {
        "match_phrase": {
           "bookname": "C 学习"
        }
    }
}





//短语前缀匹配 - match_phrase_prefix

GET  book/it/_search
{
    "query": {
        "match_phrase_prefix": {
           "bookname": "C"
        }
    }
}

布尔查询 - bool

将几个查询的返回用布尔结果拼接

GET /_search
{
  "query": {
    "bool": { 
      "should": [  #不一定要满足的条件 如果满足加入评分
        {
          "term": {}
        }
      ],
      "must": [  #下面放必须满足的查询条件
        {
          "match": {}
        },
        {
          "term": {}
        }
      ],
      "must_not": [ #存放要排除的内容
        {
          "term": {}
        }
      ]
    }
  }
}
 

只查询出author2值为jhon的记录

GET  book/it/_search
{
    "query": {
        "match": {
           "bookname": "好好学习"
        }
    }
}

查询出bookname中包含学习的记录,_score为匹配值,按匹配值高低排序,如果bookname 中同时包含 好好  与学习 ,则排前面。

结果为:

{
   "took": 35,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "skipped": 0,
      "failed": 0
   },
   "hits": {
      "total": 5,
      "max_score": 0.5753642,
      "hits": [
         {
            "_index": "book",
            "_type": "it",
            "_id": "5",
            "_score": 0.5753642,
            "_source": {
               "bookid": 1,
               "country": 1,
               "bookname": "C 学习",
               "publishDate": "2018-04-02",
               "author": "wd",
               "author2": "jhon2",
               "reader2": 1
            }
         },
         {
            "_index": "book",
            "_type": "it",
            "_id": "1",
            "_score": 0.5753642,
            "_source": {
               "bookid": 1,
               "country": "中国",
               "bookname": "java 学习",
               "publishDate": "2018-02-02",
               "author": "weidong",
               "author2": "jhon",
               "reader": 10
            }
         },
         {
            "_index": "book",
            "_type": "it",
            "_id": "3",
            "_score": 0.5753642,
            "_source": {
               "bookid": 1,
               "country": "英国",
               "bookname": "C 学习",
               "publishDate": "2018-04-02",
               "author": "wd",
               "author2": "jhon2",
               "reader": 13
            }
         },
         {
            "_index": "book",
            "_type": "it",
            "_id": "2",
            "_score": 0.36464313,
            "_source": {
               "bookid": 1,
               "country": "中国",
               "bookname": "php 学习",
               "publishDate": "2018-07-02",
               "author": "wd",
               "author2": "jhon",
               "reader": 20
            }
         },
         {
            "_index": "book",
            "_type": "it",
            "_id": "4",
            "_score": 0.36464313,
            "_source": {
               "bookid": 1,
               "country": 1,
               "bookname": "C 学习",
               "publishDate": "2018-04-02",
               "author": "wd",
               "author2": "jhon 2",
               "reader": 13
            }
         }
      ]
   }
}

term 代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词

  • term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这些查询通常用于数字,日期和枚举等结构化数据,而不是全文本字段
//无结果
GET  book/it/_search
{
    "query": {
        "term": {
           "bookname": "学习"
        }
    }
}


//有结果
GET  book/it/_search
{
    "query": {
        "term": {
           "bookname.keyword": "C 学习"
        }
    }
}

4指标聚合

PUT my-index/person/5
{
  "name":"程裕强",
  "age":28,
  "salary":10000
}
PUT my-index/person/6
{
  "name":"hadron",
  "age":19,
  "salary":5000
}
 
GET my-index/_search
{  
  
  "size": 0, 
  "aggs": {
    "avg-salary": {
      "avg": {"field": "salary"}
    },
    "avg-age": {
      "avg": {"field": "age"}
    },
     "sum_salary": {
      "sum": {"field": "salary"}
    }
  }
}
 
GET my-index/_search
{
    "size" : 0, 
    "aggs" : {  
        "popular_colors" : {  
            "terms" : {  
              "field" : "name.keyword" 
            },
             "aggs": {
    "avg-salary": {
      "avg": {"field": "salary"}
    },
    "avg-age": {
      "avg": {"field": "age"}
    },
     "sum_salary": {
      "sum": {"field": "salary"}
    }
  }
            
        }
    }
     
}
 

cardinality 用于计算每个 model 的颜色数,在 Elasticsearch 中我们需要使用一个指标类聚合 Cardinality ,进行不同值计数

https://blog.csdn.net/laoyang360/article/details/82932516(通透理解Elasticsearch聚合)

https://blog.csdn.net/qq_34646817/article/details/82594726(Elasticsearch 聚合分析详解)

https://elasticsearch.cn/article/629

POST _bulk
{"index":{"_index":"cars","_type":"doc","_id":"1"}}
{"model":"A","color":"red"}
{"index":{"_index":"cars","_type":"doc","_id":"2"}}
{"model":"A","color":"white"}
{"index":{"_index":"cars","_type":"doc","_id":"3"}}
{"model":"A","color":"black"}
{"index":{"_index":"cars","_type":"doc","_id":"4"}}
{"model":"A","color":"yellow"}
{"index":{"_index":"cars","_type":"doc","_id":"5"}}
{"model":"B","color":"red"}
{"index":{"_index":"cars","_type":"doc","_id":"6"}}
{"model":"B","color":"white"}
{"index":{"_index":"cars","_type":"doc","_id":"7"}}
{"model":"C","color":"black"}
{"index":{"_index":"cars","_type":"doc","_id":"8"}}
{"model":"C","color":"red"}
{"index":{"_index":"cars","_type":"doc","_id":"9"}}
{"model":"C","color":"white"}
{"index":{"_index":"cars","_type":"doc","_id":"10"}}
{"model":"C","color":"yellow"}
{"index":{"_index":"cars","_type":"doc","_id":"11"}}
{"model":"C","color":"blue"}
{"index":{"_index":"cars","_type":"doc","_id":"12"}}
{"model":"D","color":"red"}
{"index":{"_index":"cars","_type":"doc","_id":"13"}}
{"model":"A","color":"red"}


GET  /cars/_search
{
    "size":0,
    "aggs":{
        "model_type":{
            "terms":{
                "field":"model.keyword"
            },
            "aggs":{
                "color_count":{
                    "cardinality":
                    {"field":"color.keyword"}
                    
                }
                
                
            }
        }
    }
}


//先对model分组统计组内不同颜色,过滤颜色种类大于1

GET cars/_search
{
  "size": 0,
  "aggs": {
    "models": {
      "terms": {
        "field": "model.keyword"
      },
      "aggs": {
        "color_count": {
          "cardinality": {
            "field": "color.keyword"
          }
        },
        "color_count_filter": {
          "bucket_selector": {
            "buckets_path": {
              "colorCount": "color_count"
            },
            "script": "params.colorCount1>"
          }
        }
      }
    }
  }
}



GET cars/_search
{
  "size": 0,
  "aggs": {
    "models": {
      "terms": {
        "field": "model.keyword"
      },
      "aggs": {
        "color_count": {
          "cardinality": {
            "field": "color.keyword"
          }
        },
        "color_count_filter": {
          "bucket_selector": {
            "buckets_path": {
              "colorCount": "color_count"
            },
            "script": "params.colorCount>1"
          }
        },
        "color_count_sort": {
          "bucket_sort": {
            "sort": {
              "color_count": "desc"
            },
            "size": 2
          }
        }
      }
    }
  }
}
POST _bulk
{"index":{"_index":"cars","_type":"doc","_id":"1"}}
{"name":"bmw","date":"2017-06-01", "color":"red", "price":30000}
{"index":{"_index":"cars","_type":"doc","_id":"2"}}
{"name":"bmw","date":"2017-06-30", "color":"blue", "price":50000}
{"index":{"_index":"cars","_type":"doc","_id":"3"}}
{"name":"bmw","date":"2017-08-11", "color":"red", "price":90000}
{"index":{"_index":"cars","_type":"doc","_id":"4"}}
{"name":"ford","date":"2017-07-15", "color":"red", "price":20000}
{"index":{"_index":"cars","_type":"doc","_id":"5"}}
{"name":"ford","date":"2017-07-01", "color":"blue", "price":40000}
{"index":{"_index":"cars","_type":"doc","_id":"6"}}
{"name":"bmw","date":"2017-08-01", "color":"green", "price":10000}
{"index":{"_index":"cars","_type":"doc","_id":"7"}}
{"name":"jeep","date":"2017-07-08", "color":"red", "price":110000}
{"index":{"_index":"cars","_type":"doc","_id":"8"}}
{"name":"jeep","date":"2017-08-25", "color":"red", "price":230000}

//按月统计销售额,并统计出总销售额大于200000的月份信息。




POST /cars/_search
{
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "date",
        "interval": "month"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "price"
          }
        },
        "sales_bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "totalSales": "total_sales"
            },
            "script": "params.totalSales > 200000"
          }
        }
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_34207444/article/details/87123113
6.4