首页 > 其他 > 详细

ElasticSearch的一些使用笔记

时间:2020-03-16 00:40:02      阅读:97      评论:0      收藏:0      [点我收藏+]

1、首先批量插入一批数据

POST bank/account/_bulk
{你要执行的命令}

银行数据的字段
{
    "account_number": 0,
    "balance": 16623,
    "firstname": "Bradshaw",
    "lastname": "Mckenzie",
    "age": 29,
    "gender": "F",
    "address": "244 Columbus Place",
    "employer": "Euron",
    "email": "bradshawmckenzie@euron.com",
    "city": "Hobucken",
    "state": "CO"
}

https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true 导入测试数据

2.日常的一些使用json数据

GET bank/_search
{
  "query": {
    "match_all": {}
  }
}

#查看所有节点
GET _cat/nodes
#查看健康状态
GET _cat/health
#查看主节点
GET _cat/master
#查看所有索引
GET _cat/indices

##查询所有查询
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "_source": [
    "account_number",
    "balance"
    ]
}


#全文检索,match的字段如果是一个文本,自动的进行分词,进行模糊匹配。
#倒排索引?
GET bank/_search
{
  "query": {
    "match": {
      "address": "502 Baycliff Terrace"
    }
  }
}

#精确匹配,不进行分词模糊,keyword
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "502 Baycliff Terrace"
    }
  }
}

#精确匹配,不进行分词模糊,match_phrase
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "502 Baycliff Terrace"
    }
  }
}

#多字段匹配
GET /bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill ak",
      "fields": ["address","state"]
    }
  }
}

#复合查询;多个查询条件组合起来进行查询
#查询address里面包含Mill,并且gender是M,如果年龄在20-30之间最好,并且state必须不是IL
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "address": "mill"
          }
        },{
          "match_phrase": {
            "gender": "M"
          }
        }
      ],
      "must_not": [
        {
          "match": {
          "state": "AK"
          }
        }
      ],
      "should": [
        {
          "range": {
            "age": {
              "gte": 20,
              "lte": 30
            }
          }
        }
      ]
    }
  }
}

GET  bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "gender": "M"
          }
        }
      ]
    }
  }
}

#过滤查询;只过滤满足的结果,不评分。
#以后除了全文检索需要评分。剩下查询用过滤效果是一模一样,不用评分。query比filter,因为query要评分
GET bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "match": {
            "gender": "M"
          }
      }
    }
  }
}

#term查询;对于一些精确至的查询可以用term。
#term用于哪些类型不是文本的。age:22,status:true
#name:"";字符串不用来term。term某个字段必须匹配某个值;terms用来某个字段可以匹配多个值

#每一个是text类的字段,都有一个 属性.keyword字段,代表非text类型。可以用来精确匹配
GET bank/_search
{
  "query": {
    "terms": {
      "age": [
        "22",
        "25"
      ]
    }
  },
  "_source": [
    "age"
    ]
}

GET bank/_search
{
  "query": {
    "term": {
      "gender.keyword": "M"
    }
  }
}

#聚合:用来分析数据
GET bank/_search

#计算平均年龄
GET bank/_search
{
  "aggs": {
    "all_avg_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}


#搜索address中包含mill的所有人的年龄分布以及平均年龄。

GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "age_count": {
      "terms": {
        "field": "age",
        "size": 20
      }
    },
    "all_avg_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}

#按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "age_count": {
      "terms": {
        "field": "age"
      },
      "aggs": {
        "avg_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}


#查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_counnt": {
      "terms": {
        "field": "age"
      },
      "aggs": {
        "gender_count": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "avg_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

#查出bank中所有高于平均薪资的这些男生以及他们的最高薪资和最低薪资,女生也一样。
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "avg_balance": {
      "avg": {
        "field": "balance"
      }
    },
    "gender_count":{
      "terms": {
        "field": "gender.keyword"
      },
      "aggs": {
        "max_balance": {
          "max": {
            "field": "balance"
          }
        },
        "min_balance": {
          "min": {
            "field": "balance"
          }
        }
      }
    }
  }
}


GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "avg_balance": {
      "avg": {
        "field": "balance"
      }
    },
    "gender_count": {
      "terms": {
        "field": "gender.keyword",
        "size": 10
      },
      "aggs": {
        "max_balance": {
          "max": {
            "field": "balance"
          }
        },
        "min_balance": {
          "min": {
            "field": "balance"
          }
        },
        "filter_xxx":{
          "filter": {
            "range": {
              "balance": {
                "gte": 10000,
                "lte": 20000
              }
            }
          }
        }
      }
    }
  }
}




#查看映射
GET bank/_mapping


#修改某个索引的映射,必须在第一次创建索引的时候指定,如果这个索引下已有数据就没办法了。
PUT my_bank
{
  "mappings": {
    "account":{
      "properties": {
          "account_number": {
            "type": "long"
          },
          "address":{
            "type": "text",
            "fields": {
              "keyword":{
                "type": "keyword", 
                "ignore_above": 256
              }
            }
          },
          "age": {
            "type": "long"
          },
          "balance": {
            "type": "long"
          },
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "email": {
            "type": "keyword"
          },
          "employer": {
            "type": "text",
            "analyzer": "ik_smart"
          },
          "firstname": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "gender": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "lastname": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "state": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
      }
    }
  }
}


GET bank/_analyze
{  
  "text": "我是中国人"
}

GET bank/_analyze
{  
  "text": "我是中国人",
  "analyzer": "ik_max_word"
}


POST customer/external/_bulk
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }


GET customer/_search
{
  "query": {"match_all": {}}
}

GET user/_search
{
  "query": 
  {"match_all": {}}
}

 

ElasticSearch的一些使用笔记

原文:https://www.cnblogs.com/amaocc/p/12501161.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!