elasticsearch kibana 常用语句

10-1 elasticsearch介绍

 

目前在使用es的大公司: https://www.elastic.co/use-cases

 

Mongodb redis elasticsearch面前就是一个玩笑 哈哈!

 

10-2 elasticsearch安装

 

10-3elasticsearch-head插件以及kibana的安装

 

 

扫描二维码关注公众号,回复: 1353811 查看本文章

要先下载java安装

Java -version 查看是否安装

可以用国内大神二次开发的版本 https://github.com/medcl/elasticsearch-rtf

遇见的错误

  1. Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME

     

    解决方式:

    要添加jdk配置到windows的环境中

    JAVA_HOME    

    C:\Program Files\Java\jdk1.8.0_131

  1. 此时不应有 \elasticsearch-rtf-master\bin\\..\config\jvm.options

     

    解决方式:

    因为的你的路径中有括号,只要去掉括号就可以了

安装head插件:

下载 https://github.com/mobz/elasticsearch-head 

下载 https://nodejs.org/en/ 安装nodejs

安装好nodejs就安装好了npm    可以用npm -v 测试下

网上安装教程很多,就可以安装。

 

推荐使用cnpm 利用的是淘宝的镜像

npm install -g cnpm --registry=https://registry.npm.taobao.org 

百度下就可以看到很多资料,这里不多写了

 

 

整个安装elasticsearch的教程,可以百度都可用

 

 

启动 命令

 

启动elasticsearch 直接 elasticsearch.bat

 

启动 head 直接 cnpm run start

 

启动kibana 直接kibana.bat

 

 

 

 

10-4 elasticsearch的基本概念

如果是同一个索引的,要求是字段名称要相同

 

 

 

 

 

10-5 倒排索引

 

<>中是出现的位置,最后一个是出现的次数

以上的问题其实elasticsearch都已经帮我们做好了

 

 

10-6 elasticsearch 基本的索引和文档CRUD操作

 

10-7 elasticsearchmgetbulk批量操作

 

10-8 elasticsearchmapping映射管理

 

 

10-9 elasticsearch的简单查询 - 1

 

 

 

10-10 elasticsearch的简单查询 - 2

10-11 elasticsearchbool组合查询

GET _search 

 {

  "query": {

    "match_all": {}

  }

}

 

 

 

# es的文档、索引的CRUD操作

 

# 索引初始化操作

# 指定分片和副本的数量

# shards 一旦设置就不能修改了(注意

# 建立5个分片,1个副本

 

PUT lagou

{

  "settings": {

    "index":{

      "number_of_shards":5,

      "number_of_replicas":1

    }

  }

}

 

 

GET lagou/_settings

# 获得所有索引的setting

GET _all/_settings

GET _settings

# 获得指定索引的setting

GET .kibana,lagou/_settings

 

# 修改settings
PUT lagou/_settings
{
"number_of_replicas": 2
}

 

 

# 获取索引信息

GET _all

GET lagou

 

#put插入数据一定要有_id,可以自己指定

PUT lagou/job/1

{

  "name":"ppp",

  "age":12

}

#post插入数据,可以没有_id,他会自己随机生成一个_id

POST lagou/job/

{

  "name":"ppp",

  "age":13

}

 

 

# 只查看某条记录

GET lagou/job/1

# 只查看某条记录的某个字段

GET lagou/job/1?_source=name,age

# put修改文章 只要是_id是一样的,那就会直接覆盖

PUT lagou/job/1

{

  "name":"ppp",

  "age":12,

  "gender":"male"

}

# post 可以指定某个字段做修改,不用用到全部的字段

# 一般都是用post

POST lagou/job/1/_update

{

  "doc": {

    "name2": "hello"

  }

}

# 删除

# 删除某条记录

DELETE lagou/job/1

# 删除索引

DELETE lagou

 

 

# _mget多条查询

GET lagou/job/_mget

{

  "ids":[1,"AWOcnK0u_fyJAeHWwkP7",12]

}

# _bulk用的不多

 

 

 

# 创建索引

PUT lagou22

{

  "mappings": {

    "job":{

      "properties": {

        "name":{

          "type": "keyword"

        },

        "age":{

          "type": "integer"

        },

        "gender":{

          "type": "text"

        }

      }

    }

  }

}

 

 

PUT lagou22/job/1

{

  "name":"pujinxiao",

  "age":34,

  "gender":"male"

  

}

 

 

 

 

 

 

 

# 查询 query

term match 区别

term 不会给查询条件做分词,但是match会的

{

  "query": {

    "term": {

      "title": "火石"

    }

  }

}

 

{

  "query": {

    "match": {

      "title": "火石"

    }

  }

}

 

# 只要满足列表中的一个就能被查询出来

get map_news/index/_search

{

  "query": {

    "terms": {

      "title": ["火石","python"]

    }

  }

}

 

 

# 控制查询返回的数量

get map_news/index/_search

{

  "query": {

    "term": {

      "title": ["火石","python"]

    }

  },

  "from":0,

  "size":2

}

 

 

 

# match_phrase查询

# 短语查询

#他会把查询的内容分词,slop就是分词后的中间的长度,你可以自己控制

GET map_news/index/_search

{

  "query": {

    "match_phrase": {

      "title": {

        "query": "火石创造"

"slop":6

      }

    }

  }

}

 

 

# multi_match

# 指定多个字段查询,

GET map_news/index/_search

{

  "query": {

    "multi_match": {

      "query": "火石",

      "fields": ["title","summary"]

      }

    }

}

 

# 搜索的权重是titlesummary的三倍

 "fields": ["title^3","summary"]

 

 

 

# 指定返回的字段

# _source 指定返回的字段,用excludes 是排除该字段

GET map_news/index/_search

{

  "_source": {

    "includes": ["title"]

  },

  "query": {

    "match": {

      "title": "火石"

      }

    }

}

 

 

 

 

# 通过sort把结果排序

GET map_news/index/_search

{

  "query": {

    "match_all": {}

  },

  "sort": [

    {

      "update_time": {

        "order": "desc"

      }

    }

  ]

}

 

 

 

# 查询范围

# range查询

GET map_news/index/_search

{

  "query": {

    "range": {

      "publish_timestamp": {

        "gte": "2016-08-24 18:58:00.0",

        "lte": "2016-08-24 19:58:00.0"

      }

    }

  }

}

 

 

 

 

# wildcard查询

# 支持通配符查询

GET map_news/index/_search

{

  "_source": {

    "includes": ["title"]

  },

  "query": {

    "wildcard": {

      "title": {

        "value": "pyth*n"

      }

    }

  }

}

 

 

 

 

 

 

 

# # 组合查询

 

# bool查询

# bool 包括 must should must_not filter 来完成,格式如下

bool:{

"filter":[],  字段的过滤 不参与打分

"must":[],   必须全部都满足

"should":[],  只要满足一个就可以

"must_not":{}, 必须一个都不能满足

}

 

 

 

# 最简单的filter查询

# select * from testjob where salary=20

# 薪资为20的工作

# term 换为 terms 就可以[ ] 查询多个值

get lagou/testjob/_search

{

  "query": {

    "bool": {

      "filter": {

        "term": {

          "salary": 20

        }

      }

    }

  }

}

 

 

 

 

# 查看分词器解析的结果  ik_max_word ik_smart 2种分词方法

GET _analyze    # 分成 python 网络 开发 工程师  工程 师

{

  "analyzer": "ik_max_word",

  "text": "python网络开发工程师"

}

 

 

GET _analyze  # 分成 python 网络 开发 工程师

{

  "analyzer": "ik_smart",

  "text": "python网络开发工程师"

}

 

 

 

 

# bool过滤查询,可以做组合查询

# select * from testjob where (salary=20 or title=python) and (salary!=30)

#查询薪资等于20k或者是工作为python的工作,排除价格为30k

{

  "query": {

    "bool": {

      "should": [

        {"term": {

          "salary": {

            "value": "20"

          }

        }},

        {"term": {

          "title": {

            "value": "python"

          }

        }}

      ],

      "must_not": [

        {"term": {

          "salary": {

            "value": "30"

          }

        }}

      ]

    }

  }

}

 

# 嵌套查询

{

  "query": {

    "bool": {

      "should": [

        {

          "term": {

            "salary": {

              "value": "20"

            }

          }

        },

        {"bool": {

          "must": [

            {"term": {

              "title": {

                "value": "python"

              }

            }},

            {

              "term": {

                "salary": {

                  "value": "30"

                }

              }

            }

          ]

        }}

      ],

      "must_not": [

        {

          "term": {

            "salary": {

              "value": "30"

            }

          }

        }

      ]

    }

  }

}

 

# 查询该字段为null的情况

GET map_news/index/_search

{

  "query": {

    "bool": {

      "must_not": {

        "exists": {

          "field": "content"

        }

      }

    }

  }

} 

 

10-12 scrapy写入数据到elasticsearch- 1

10-13 scrapy写入数据到elasticsearch- 2

 Elasticsearch-dsl 

 

 

# 取出html标签

from w3lib.html import remove_tags

remove_tags(u'<span>1000</span>')

 

 

 

 

 

# 删除# 删除某条记录DELETE lagou/job/1# 删除索引DELETE lagou

# _mget多条查询GET lagou/job/_mget{  "ids":[1,"AWOcnK0u_fyJAeHWwkP7",12]}# _bulk用的不多


# 创建索引PUT lagou22{  "mappings": {    "job":{      "properties": {        "name":{          "type": "keyword"        },        "age":{          "type": "integer"        },        "gender":{          "type": "text"        }      }    }  }}

PUT lagou22/job/1{  "name":"pujinxiao",  "age":34,  "gender":"male"  }






# 查询 queryterm 和 match 区别term 不会给查询条件做分词,但是match会的{  "query": {    "term": {      "title": "火石"    }  }}
{  "query": {    "match": {      "title": "火石"    }  }}
# 只要满足列表中的一个就能被查询出来get map_news/index/_search{  "query": {    "terms": {      "title": ["火石","python"]    }  }}

# 控制查询返回的数量get map_news/index/_search{  "query": {    "term": {      "title": ["火石","python"]    }  },  "from":0,  "size":2}


# match_phrase查询# 短语查询#他会把查询的内容分词,slop就是分词后的中间的长度,你可以自己控制GET map_news/index/_search{  "query": {    "match_phrase": {      "title": {        "query": "火石创造","slop":6      }    }  }}

# multi_match# 指定多个字段查询,GET map_news/index/_search{  "query": {    "multi_match": {      "query": "火石",      "fields": ["title","summary"]      }    }}
# 搜索的权重是title是summary的三倍 "fields": ["title^3","summary"]
  # 指定返回的字段# _source 指定返回的字段,用excludes 是排除该字段GET map_news/index/_search{  "_source": {    "includes": ["title"]  },   "query": {    "match": {      "title": "火石"      }    }}



# 通过sort把结果排序GET map_news/index/_search{  "query": {    "match_all": {}  },  "sort": [    {      "update_time": {        "order": "desc"      }    }  ]}

 # 查询范围# range查询GET map_news/index/_search{  "query": {    "range": {      "publish_timestamp": {        "gte": "2016-08-24 18:58:00.0",        "lte": "2016-08-24 19:58:00.0"      }    }  }}



# wildcard查询# 支持通配符查询GET map_news/index/_search{  "_source": {    "includes": ["title"]  },   "query": {    "wildcard": {      "title": {        "value": "pyth*n"      }    }  }}






# # 组合查询
# bool查询# 用bool 包括 must should must_not filter 来完成,格式如下bool:{"filter":[],  字段的过滤 不参与打分"must":[],   必须全部都满足"should":[],  只要满足一个就可以"must_not":{}, 必须一个都不能满足}


# 最简单的filter查询# select * from testjob where salary=20# 薪资为20的工作# term 换为 terms 就可以[ ] 查询多个值get lagou/testjob/_search{  "query": {    "bool": {      "filter": {        "term": {          "salary": 20        }       }    }  }} 



# 查看分词器解析的结果  ik_max_word 和 ik_smart 2种分词方法GET _analyze    # 分成 python 网络 开发 工程师  工程 师{  "analyzer": "ik_max_word",  "text": "python网络开发工程师"}

GET _analyze  # 分成 python 网络 开发 工程师{  "analyzer": "ik_smart",  "text": "python网络开发工程师"}



# bool过滤查询,可以做组合查询# select * from testjob where (salary=20 or title=python) and (salary!=30)#查询薪资等于20k或者是工作为python的工作,排除价格为30k的{  "query": {    "bool": {      "should": [        {"term": {          "salary": {            "value": "20"          }        }},        {"term": {          "title": {            "value": "python"          }        }}      ],      "must_not": [        {"term": {          "salary": {            "value": "30"          }        }}      ]    }  }} 
# 嵌套查询 {  "query": {    "bool": {      "should": [        {          "term": {            "salary": {              "value": "20"            }          }        },        {"bool": {          "must": [            {"term": {              "title": {                "value": "python"              }            }},            {              "term": {                "salary": {                  "value": "30"                }              }            }          ]        }}      ],      "must_not": [        {          "term": {            "salary": {              "value": "30"            }          }        }      ]    }  }} 
# 查询该字段为null的情况GET map_news/index/_search{  "query": {    "bool": {      "must_not": {        "exists": {          "field": "content"        }      }    }  }}

猜你喜欢

转载自www.cnblogs.com/jinxiao-pu/p/9123526.html