Elasticsearch整理笔记(二)

Stella981
• 阅读 516

安装

  • docker部署
    • 搜索镜像

      docker search elasticsearch

    • Elasticsearch整理笔记(二)

    • 查看镜像&运行容器

      docker images

      Elasticsearch整理笔记(二)

    • docker run -d --name es2 -p 9200``:``9200 -p 9300``:``9300 -e "discovery.type=single-node" 5acf0e8da90b

    • elasticsearch-head插件查看ElasticSearch内部信息

      • 谷歌浏览器提供了插件:ElasticSearchHead

        Elasticsearch整理笔记(二)

      Elasticsearch整理笔记(二)

使用

  • 概念

    • 索引:相当于database 一个数据库。

    • 类型:相当于table,一个表。 

    • 文档:相当于一行记录。

  • 常用API

    • 创建索引

      • PUT http://localhost:9200/order_server

        {

        "settings"``: {

        "index"``: {

        "number_of_shards"``: "1"``,

        "number_of_replicas"``: "5"

        }

        }

        }

        {

        "acknowledged"``: true``,

        "shards_acknowledged"``: true``,

        "index"``: "order_server"

        }

    • 添加mapping

    • 查看mapping

      • GET http://localhost:9200/order_server/_mapping

        {

        "order_server"``: {

        "mappings"``: {

        "properties"``: {

        "context"``: {

        "type"``: "text"``,

        "index"``: false

        },

        "extras"``: {

        "properties"``: {

        "class"``: {

        "type"``: "text"``,

        "fields"``: {

        "keyword"``: {

        "type"``: "keyword"``,

        "ignore_above"``: 256

        }

        }

        },

        "grade"``: {

        "type"``: "text"``,

        "fields"``: {

        "keyword"``: {

        "type"``: "keyword"``,

        "ignore_above"``: 256

        }

        }

        },

        "school_name"``: {

        "type"``: "text"``,

        "fields"``: {

        "keyword"``: {

        "type"``: "keyword"``,

        "ignore_above"``: 256

        }

        }

        },

        "student_name"``: {

        "type"``: "text"``,

        "fields"``: {

        "keyword"``: {

        "type"``: "keyword"``,

        "ignore_above"``: 256

        }

        }

        }

        }

        },

        "status"``: {

        "type"``: "long"

        }

        }

        }

        }

        }

    • 插入数据

      • POST http://localhost:9200/order_server/order/10009

        {

        "extras"``: {

        "school_name"``:``"佳木斯第一中学"``,

        "grade"``:``"一年级"``,

        "class"``:``"二班"``,

        "student_name"``:``"张三"

        },

        "status"``: 1      

        }

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_version"``: 3``,

        "result"``: "created"  //如果_id相同,会更新原数据,该字段返回"updated",

        "_shards"``: {

        "total"``: 6``,

        "successful"``: 1``,

        "failed"``: 0

        },

        "_seq_no"``: 7``,

        "_primary_term"``: 1

        }

    • 修改数据

      • PUT http://localhost:9200/order_server/order/10001

        {

        "extras"``: {

        "school_name"``:``"佳木斯第一中学"``,

        "grade"``:``"一年级"``,

        "class"``:``"二班"``,

        "student_name"``:``"张三"

        },

        "status"``: 1      

        }

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_version"``: 4``,

        "result"``: "updated"``,

        "_shards"``: {

        "total"``: 6``,

        "successful"``: 1``,

        "failed"``: 0

        },

        "_seq_no"``: 9``,

        "_primary_term"``: 1

        }

    • 删除数据

      • DELETE http://localhost:9200/order_server/order/10001

        {

        }

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_version"``: 2``,

        "result"``: "deleted"``,

        "_shards"``: {

        "total"``: 6``,

        "successful"``: 1``,

        "failed"``: 0

        },

        "_seq_no"``: 4``,

        "_primary_term"``: 1

        }

    • id查询

      • GET  http://localhost:9200/order_server/order/10001

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_version"``: 4``,

        "_seq_no"``: 9``,

        "_primary_term"``: 1``,

        "found"``: true``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "张三"

        },

        "status"``: 1

        }

        }

    • 条件查询

      • POST http://localhost:9200/order_server/order/_search

        {

        "query"``:{

        "match"``:{

        "extras.student_name"``:``"张三"

        }

        }

        }

        {

        "took"``: 26``,

        "timed_out"``: false``,

        "_shards"``: {

        "total"``: 1``,

        "successful"``: 1``,

        "skipped"``: 0``,

        "failed"``: 0

        },

        "hits"``: {

        "total"``: {

        "value"``: 1``,

        "relation"``: "eq"

        },

        "max_score"``: 1.9616582``,

        "hits"``: [

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_score"``: 1.9616582``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "张三"

        },

        "status"``: 1

        }

        }

        ]

        }

        }

    • 精准查询

      • POST http://localhost:9200/order_server/order/_search

        {

        "query"``:{

        "term"``:{

        "status"``:``1

        }

        }

        }

        {

        "took"``: 4``,

        "timed_out"``: false``,

        "_shards"``: {

        "total"``: 1``,

        "successful"``: 1``,

        "skipped"``: 0``,

        "failed"``: 0

        },

        "hits"``: {

        "total"``: {

        "value"``: 4``,

        "relation"``: "eq"

        },

        "max_score"``: 1.0``,

        "hits"``: [

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10002"``,

        "_score"``: 1.0``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "李四"

        },

        "status"``: 1

        }

        },

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10009"``,

        "_score"``: 1.0``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "张三"

        },

        "status"``: 1

        }

        },

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_score"``: 1.0``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "张三"

        },

        "status"``: 1

        }

        },

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10008"``,

        "_score"``: 1.0``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第二中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "老王"

        },

        "status"``: 1

        }

        }

        ]

        }

        }

    • 模糊查询

      • POST http://localhost:9200/order_server/order/_search

        {

        "query"``: {

        "match"``: {

        "extras.school_name"``: {

        "query"``:``"佳木斯 中学"``,

        "fuzziness"``: "AUTO"``,

        "operator"``: "and"

        }

        }

        }

        }

        {

        "took"``: 34``,

        "timed_out"``: false``,

        "_shards"``: {

        "total"``: 1``,

        "successful"``: 1``,

        "skipped"``: 0``,

        "failed"``: 0

        },

        "hits"``: {

        "total"``: {

        "value"``: 2``,

        "relation"``: "eq"

        },

        "max_score"``: 0.9116078``,

        "hits"``: [

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10002"``,

        "_score"``: 0.9116078``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "李四"

        },

        "status"``: 1

        }

        },

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10001"``,

        "_score"``: 0.9116078``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第一中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "张三"

        },

        "status"``: 1

        }

        }

        ]

        }

        }

    • 跨字段查询

      • POST http://localhost:9200/order_server/order/_search

        {

        "query"``: {

        "multi_match"``: {

        "query"``: "佳 二"``,

        "operator"``:``"and"

        }

        }

        }

        {

        "took"``: 1626``,

        "timed_out"``: false``,

        "_shards"``: {

        "total"``: 1``,

        "successful"``: 1``,

        "skipped"``: 0``,

        "failed"``: 0

        },

        "hits"``: {

        "total"``: {

        "value"``: 1``,

        "relation"``: "eq"

        },

        "max_score"``: 1.3093333``,

        "hits"``: [

        {

        "_index"``: "order_server"``,

        "_type"``: "order"``,

        "_id"``: "10008"``,

        "_score"``: 1.3093333``,

        "_source"``: {

        "extras"``: {

        "school_name"``: "佳木斯第二中学"``,

        "grade"``: "一年级"``,

        "class"``: "二班"``,

        "student_name"``: "老王"

        },

        "status"``: 1

        }

        }

        ]

        }

        }

    • 更多API   Elasticsearch文档   Elasticsearch文档(旧)

mongo-connector同步

  • mongo-connector 是一个 python 编写的,用来复制 mongodb 中数据到各种搜索数据库的工具,支持 elasticsearch。依赖python

  • mongo-connector工具创建一个从MongoDB簇到一个或多个目标系统的管道,目标系统包括:Solr,Elasticsearch,或MongoDB簇。
    该工具在MongoDB与目标系统间同步数据,并跟踪MongoDB的oplog,保持操作与MongoDB的实时同步。

  • mongo-connector要求mongo运行在replica-set模式,且需要 elastic2_doc_manager将数据写入ES。

  • mongo-connector supports Python 3.4+ and MongoDB versions 3.4 and 3.6

  • docker 安装 

    #构建自定义镜像

    docker run -it --name mongo-connector python:``3.6 bash

    #安装mongo-connector

    pip install 'mongo-connector[elastic5]'

    #安装pymongo

    pip install pymongo

    #安装docmanager

    pip install 'elastic2-doc-manager[elastic5]'

  • 设置副本集

    #启动mongod

    sudo ./mongod --replSet "rs0"

    #另一个终端运行mongo

    sudo ./mongo

    #副本集初始化

    rs.initiate()

    ""``"返回

    {

    "info2" : "no configuration specified. Using a default configuration for the set"``,

    "me" : "localhost:27017"``,

    "ok" : 1``,

    "operationTime" : Timestamp(``1599449053``, 1``),

    "$clusterTime" : {

    "clusterTime" : Timestamp(``1599449053``, 1``),

    "signature" : {

    "hash" : BinData(``0``,``"AAAAAAAAAAAAAAAAAAAAAAAAAAA="``),

    "keyId" : NumberLong(``0``)

    }

    }

    }

    ""``"

    #验证初始化副本集的配置

    rs.conf()

    ""``"返回

    {

    "_id" : "rs0"``,

    "version" : 1``,

    "protocolVersion" : NumberLong(``1``),

    "writeConcernMajorityJournalDefault" : true``,

    "members" : [

    {

    "_id" : 0``,

    "host" : "localhost:27017"``,

    "arbiterOnly" : false``,

    "buildIndexes" : true``,

    "hidden" : false``,

    "priority" : 1``,

    "tags" : {

    },

    "slaveDelay" : NumberLong(``0``),

    "votes" : 1

    }

    ],

    "settings" : {

    "chainingAllowed" : true``,

    "heartbeatIntervalMillis" : 2000``,

    "heartbeatTimeoutSecs" : 10``,

    "electionTimeoutMillis" : 10000``,

    "catchUpTimeoutMillis" : -``1``,

    "catchUpTakeoverDelayMillis" : 30000``,

    "getLastErrorModes" : {

    },

    "getLastErrorDefaults" : {

    "w" : 1``,

    "wtimeout" : 0

    },

    "replicaSetId" : ObjectId(``"5f55a7dd29ddc8eeb2ffcca8"``)

    }

    }

    ""``"

    #验证副本集的状态

    rs.status()

    ""``"返回

    {

    "set" : "rs0"``,

    "date" : ISODate(``"2020-09-07T03:37:53.317Z"``),

    "myState" : 1``,

    "term" : NumberLong(``1``),

    "syncingTo" : ""``,

    "syncSourceHost" : ""``,

    "syncSourceId" : -``1``,

    "heartbeatIntervalMillis" : NumberLong(``2000``),

    "optimes" : {

    "lastCommittedOpTime" : {

    "ts" : Timestamp(``1599449865``, 1``),

    "t" : NumberLong(``1``)

    },

    "readConcernMajorityOpTime" : {

    "ts" : Timestamp(``1599449865``, 1``),

    "t" : NumberLong(``1``)

    },

    "appliedOpTime" : {

    "ts" : Timestamp(``1599449865``, 1``),

    "t" : NumberLong(``1``)

    },

    "durableOpTime" : {

    "ts" : Timestamp(``1599449865``, 1``),

    "t" : NumberLong(``1``)

    }

    },

    "lastStableCheckpointTimestamp" : Timestamp(``1599449835``, 1``),

    "members" : [

    {

    "_id" : 0``,

    "name" : "localhost:27017"``,

    "health" : 1``,

    "state" : 1``,

    "stateStr" : "PRIMARY"``,

    "uptime" : 920``,

    "optime" : {

    "ts" : Timestamp(``1599449865``, 1``),

    "t" : NumberLong(``1``)

    },

    "optimeDate" : ISODate(``"2020-09-07T03:37:45Z"``),

    "syncingTo" : ""``,

    "syncSourceHost" : ""``,

    "syncSourceId" : -``1``,

    "infoMessage" : ""``,

    "electionTime" : Timestamp(``1599449053``, 2``),

    "electionDate" : ISODate(``"2020-09-07T03:24:13Z"``),

    "configVersion" : 1``,

    "self" : true``,

    "lastHeartbeatMessage" : ""

    }

    ],

    "ok" : 1``,

    "operationTime" : Timestamp(``1599449865``, 1``),

    "$clusterTime" : {

    "clusterTime" : Timestamp(``1599449865``, 1``),

    "signature" : {

    "hash" : BinData(``0``,``"AAAAAAAAAAAAAAAAAAAAAAAAAAA="``),

    "keyId" : NumberLong(``0``)

    }

    }

    }

    ""``"

    #ES端同步操作

    mongo-connector -m localhost:``27017 -t localhost:``9200 -n runoob.student -d elastic2_doc_manager

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
2星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
6个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这