精华内容
下载资源
问答
  • 如何用es5实现继承

    2018-12-14 13:19:26
    extend (继承) 如何用 es5 实现继承 Father.prototype = { eat:function(){ console.log(this.name+' eat something.') } } function Father(name) { this.name = name this.attr = "father's attr....

    extend (继承)

    如何用 es5 实现继承

    Father.prototype = {
        eat:function(){
            console.log(this.name+' eat something.')
        }
    }
    
    function Father(name) {
        this.name = name
        this.attr = "father's attr."
    }
    
    function Super() {
        this.constructor = Child
    }
    
    Super.prototype = Father.prototype
    Child.prototype = new Super() // 继承父类的原型方法
    
    function Child() {
        Father.apply(this, arguments) // 继承父类的属性
        this.attr = "child's attr"
    }
    复制代码

    测试

    var child = new Child('Foo')
    
    console.log(child,child.attr)
    
    console.log(child instanceof Child, child instanceof Father)
    
    child.eat()
    
    console.log(child.newAttr)
    
    Father.prototype.newAttr = '123'
    
    console.log(child.newAttr)
    
    console.log(Child.prototype.constructor === Child)
    复制代码

    结果

    Child { name: 'Foo', attr: 'child\'s attr' } 'child\'s attr'
    true true
    Foo eat something.
    undefined
    123
    true
    复制代码

    原文链接

    转载于:https://juejin.im/post/5c13ad836fb9a049f23ca4e6

    展开全文
  • 如何用 es5 写 const 和 let

    千次阅读 2019-04-04 06:49:54
    便是如何用 es5 的方式实现一个 const。我也懵逼了,我去面试肯定也是答不出来的。仔细一想不禁觉得这个题目很妙,于是整理一下如何用 es5 实现一个 const 和 let 实现 let let 大家应该用的非常熟悉了,定义一个仅...
    前言

    无意逛某论坛,看到某位博主当面试官的经历,说是准备一道简单的面试题,可是面试者几乎没有一个答出来的。我看了下。便是如何用 es5 的方式实现一个 const。我也懵逼了,我去面试肯定也是答不出来的。仔细一想不禁觉得这个题目很妙,于是整理一下如何用 es5 实现一个 const 和 let

    实现 let

    let 大家应该用的非常熟悉了,定义一个仅作用于该代码块的变量。首先想到的是去 babel 上面在线转换一下。可是得到的结果竟然是 var。这很不科学,于是我们自己倒腾一下。在 es6 出现以前我们一般使用无限接近闭包的形式或者立即执行函数的形式来定义不会被污染的变量。我们这也可以做类似的操作。

    (function(){var a = 1;console.log(a)})();console.log(a)
    复制代码

    效果不错,这大概也是使用 es6 的方便之处吧。

    实现 const

    进入这次的主题,const 该怎么实现呢?const 声明一个只读的常量。一旦声明,常量的值就不能改变。有什么方法是可以限制一个值不能发生改变的呢?是的,需要用到 Object.defineProperty。其中有一个属性是这样的。writable:当前对象元素的值是否可修改。于是一切都好说的.于 ES5 环境没有 block 的概念,所以是无法百分百实现 const,只能是挂载到某个对象下,要么是全局的 window,要么就是自定义一个 object 来当容器

    var __const = function __const(data, value) {
      window.data = value // 把要定义的data挂载到window下,并赋值value
      Object.defineProperty(window, data, { // 利用Object.defineProperty的能力劫持当前对象,并修改其属性描述符
        enumerable: false,
        configurable: false,
        get: function () {
          return value
        },
        set: function (data) {
          if (data !== value) { // 当要对当前属性进行赋值时,则抛出错误!
            throw new TypeError('Assignment to constant variable.')
          } else {
            return value
          }
        }
      })
    }
    __const('a', 10)
    console.log(a)
    delete a
    console.log(a)
    for (let item in window) { // 因为const定义的属性在global下也是不存在的,所以用到了enumerable: false来模拟这一功能
      if (item === 'a') { // 因为不可枚举,所以不执行
        console.log(window[item])
      }
    }
    a = 20 // 报错
    复制代码

    代码有点略长,但是还算通透。那。。还有没有更简单一点的实现方法呢?至少不要这么长的代码demo了。答案是肯定的,只不过没有上面例子那么透彻。我们这次将用到es5的Object.freeze();

    var f = Object.freeze({'name':'admin'});
    f.name = 'hello'; // 严格模式下是会报错的
    f.name; // 打印出admin ,值没有被改变
    复制代码

    想要的效果还是出来了,可以大概的定义一个常量了。那么一个新的问题来了,const 真的定义的一定是一个常量嘛?一定是不可变的嘛?我们看看代码的结果

    const a ={};
    a.name='admin';
    a.name // admin;
    复制代码

    const实际上保证的,并不是变量的值不得改动,而是变量指向的那个内存地址所保存的数据不得改动。对于简单类型的数据(数值、字符串、布尔值),值就保存在变量指向的那个内存地址,因此等同于常量。但对于复合类型的数据(主要是对象和数组),变量指向的内存地址,保存的只是一个指向实际数据的指针,const只能保证这个指针是固定的(即总是指向另一个固定的地址),至于它指向的数据结构是不是可变的,就完全不能控制了。因此,将一个对象声明为常量必须非常小心。

    带来的新特性

    let,const问世之前,js是没有块级作用域这种特性的,只有函数作用域,导致了很多稀奇古怪的bug。这里就要理解两个的区别,函数作用域好理解,就是函数{}包裹的区域,其实这里使用var,let,const都一样,外部是不能调用函数内部的变量的。但是块级就有区别了,最明显的就是if(){}。在函数内使用条件判断,花括号内使用let,const,那么条件外是访问不到条件内的变量的,代码就会更清晰一点。 ,

    总结

    生活中真的很少看到这种清新的题目,发生在身边却从来没有注意到。或许这就是别人为什么是大牛,而我还在这里写着业务代码的原因吧。善于思考与发挥,然后总结于身,这就是我们现在都缺失的吧。。

    原文链接:tech.gtxlab.com/const.html


    *作者简介: 张栓,人和未来大数据前端工程师,专注于 html/css/js 的学习与开发。

    转载于:https://juejin.im/post/5ca5a6ef518825440c48c695

    展开全文
  • 如何用es5实现const

    2020-08-06 15:13:00
    —— ES5实现const, const – es6中定义制只读常量的关键字。 const PI = 3.141592653 1、对象属性 先看一下这个对象 var student ={}; student.name = 'cxx'; student.age = '16'; 然后我们修改一下 ...

    最近看到一个面试题
    在这里插入图片描述
    第一题就把我撂倒了
    —— 用ES5实现const,

    const – es6中定义制只读常量的关键字。

    const PI = 3.141592653
    

    1、对象属性

    先看一下这个对象

     var student ={};
        student.name = 'cxx';
        student.age = '16';
    

    然后我们修改一下

     student.age = '16';
    

    我们修改了属性age的值,我们可以修改属性age的值,也可以删除属性age,这些操作称为对象属性的性质。所以如果我们想要设置成const ,把这个属性设置成不能修改的不就可以了吗,所以了解一下属性的性质的修改方法。

    2、认识Object.defineProperty()方法。

    Object.defineProperty()方法会在一个对象上定义一个新的属性,或者修改一个对象的现有属性,并返回此对象。

    需要注意的是:应当直接在Object构造器对象上调用此方法,而不是再任意一个Object类型的实例上调用。

    语法如下:

    Object.defineProperty(obj, prop, descriptor)

    obj
    要定义属性的对象
    prop
    要定义或修改属性名称或symbol
    descriptor
    要定义或修改属性描述符

    Object.defineProperty() 方法可以定义对象属性的数据描述和存储描述,这里我们只讲数据描述符,不对存储描述符讲解,数据描述符有以下选项:

    属性描述符 说明
    value 该属性对应的值。可以是任何有效的 JavaScript 值(数值,对象,函数等)。默认为 undefined
    get 一个给属性提供 getter 的方法,如果没有 getter 则为 undefined .默认为undefined
    set 一个给属性提供 setter 的方法,如果没有 setter 则为 undefined。当属性值修改时,触发执行该方法 默认为undefined
    writable 当且仅当该属性的writable为true时,value才能被赋值运算符改变。默认为 false
    enumerable enumerable定义了对象的属性是否可以在 for…in 循环和 Object.keys() 中被枚举
    Configurable configurable特性表示对象的属性是否可以被删除,以及除value和writable特性外的其他特性是否可以被修改

    对位const不可修改的属性,我们可以用writable属性来实现

    function _const(key, value) {
            const desc = {
                value,
                writable:false
            };
    
            Object.defineProperty(window, key, desc);
    
        }
    
        _const('obj', {a: 1});   //定义obj
        obj.b = 2;               //可以正常给obj的属性赋值
        obj = {}                //抛出错误,提示对象read-only
    

    3、实现const定义

          var __const = function __const (data, value) {
            window.data = value // 把要定义的data挂载到window下,并赋值value
            Object.defineProperty(window, data, { // 利用Object.defineProperty的能力劫持当前对象,并修改其属性描述符
              enumerable: false,
              configurable: false,
              get: function () {
                return value
              },
              set: function (data) {
                if (data !== value) { // 当要对当前属性进行赋值时,则抛出错误!
                  throw new TypeError('Assignment to constant variable.')
                } else {
                  return value
                }
              }
            })
          }
          __const('a', 10)
          console.log(a)
          delete a
          console.log(a)
          for (let item in window) { // 因为const定义的属性在global下也是不存在的,所以用到了enumerable: false来模拟这一功能
            if (item === 'a') { // 因为不可枚举,所以不执行
              console.log(window[item])
            }
          }
          a = 20 // 报错
    
    

    参考
    1、https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty 2、https://juejin.im/post/6844903745562607623

    展开全文
  • django中如何用esby Adam Wattis 通过亚当·沃蒂斯(Adam Wattis) 用Django轻松搜索ElasticSearch (ElasticSearch with Django the easy way) A while back I was working on a Django project and wanted to ...

    django中如何用es

    by Adam Wattis

    通过亚当·沃蒂斯(Adam Wattis)

    用Django轻松搜索ElasticSearch (ElasticSearch with Django the easy way)

    A while back I was working on a Django project and wanted to implement fast free text search. Instead of using a regular database for this search function — such as MySQL or PostgreSQL — I decided to use a NoSQL database. That is when I discovered ElasticSearch.

    前一段时间,我在Django项目上工作,想实现快速的自由文本搜索。 我决定使用NoSQL数据库,而不是使用常规数据库来执行此搜索功能(例如MySQL或PostgreSQL)。 那就是我发现ElasticSearch的时候

    ElasticSearch indexes documents for your data instead of using data tables like a regular relational database does. This speeds up search, and offers a lot of other benefits that you don’t get with a regular database. I kept a regular relational database as well for storing user details, logins, and other data that ElasticSearch didn’t need to index.

    ElasticSearch为您的数据索引文档,而不是像常规关系数据库那样使用数据表。 这可以加快搜索速度,并提供其他常规数据库无法获得的其他好处。 我还保留了一个常规的关系数据库,用于存储用户详细信息,登录名和其他不需要ElasticSearch索引的数据。

    After searching for a long time on how to properly implement ElasticSearch with Django, I didn’t really find any satisfying answers. Some guides or tutorials were convoluted and seemed to be taking unnecessary steps in order to index the data into ElasticSearch. There was quite a bit of information on how to perform searching, but not as much about how the indexing should be done. I felt like there must be a simpler solution out there, so I decided to give it a try myself.

    在搜索了如何使用Django正确实现ElasticSearch的很长时间之后,我并没有真正找到令人满意的答案。 一些指南或教程令人费解,似乎正在采取不必要的步骤来将数据索引到ElasticSearch中。 关于如何执行搜索的信息很多,但是关于如何完成索引的信息却不多。 我觉得那里肯定有一个更简单的解决方案,所以我决定自己尝试一下。

    I wanted to keep it as simple as possible, because simple solutions tend to be the best ones in my opinion. KISS (Keep It Simple Stupid), Less is More and all of that stuff is something that resonates with me a lot, especially when every other solution out there is complex. I decided to use Honza Král’s example in this video to have something to base my code on. I recommend watching it, although it is a bit outdated at this point.

    我想使它尽可能简单,因为在我看来,简单的解决方案往往是最好的解决方案。 KISS(保持简单愚蠢),少即是多,所有这些东西都引起了我的共鸣,特别是当其他解决方案非常复杂时。 我决定在此视频中使用HonzaKrál的示例来为我的代码提供基础。 我建议您观看,尽管此时它已过时。

    Since I was using Django — which is written in Python — it was easy to interact with ElasticSearch. There are two client libraries to interact with ElasticSearch with Python. There’s elasticsearch-py, which is the official low-level client. And there’s elasticsearch-dsl, which is build upon the former but gives a higher-level abstraction with a bit less functionality.

    由于我使用的是用Python编写的Django,因此与ElasticSearch进行交互非常容易。 有两个客户端库可通过Python与ElasticSearch进行交互。 有elasticsearch-py ,这是官方的低级客户端。 还有elasticsearch-dsl ,它是在前者的基础上构建的,但是它提供了更高层次的抽象,但功能却有所减少。

    We will get into some example soon, but first I need to clarify what we want to accomplish:

    我们将很快讨论一些示例,但首先我需要阐明我们要完成的工作:

    • Setting up ElasticSearch on our local machine and ensuring it works properly

      在我们的本地计算机上设置ElasticSearch并确保其正常运行
    • Setting up a new Django project

      设置一个新的Django项目
    • Bulk indexing of data that is already in the database

      批量索引数据库中已经存在的数据
    • Indexing of every new instance that a user saves to the database

      用户保存到数据库的每个新实例的索引
    • A basic search example

      基本搜索示例

    All right, that seems simple enough. Lets get started by installing ElasticSearch on our machine. Also, all the code will be available on my GitHub so that you can easily follow the examples.

    好吧,这似乎很简单。 让我们开始在我们的机器上安装ElasticSearch。 另外,所有代码都将在我的GitHub上可用,因此您可以轻松地遵循示例。

    安装ElasticSearch (Installing ElasticSearch)

    Since ElasticSearch runs on Java you must ensure you have an updated JVM version. Check what version you have with java -version in the terminal. Then you run the following commands to create a new directory, download, extract and start ElasticSearch:

    由于ElasticSearch在Java上运行,因此必须确保您具有更新的JVM版本。 在终端中使用java -version检查您拥有的版本。 然后运行以下命令来创建新目录,下载,解压缩并启动ElasticSearch:

    mkdir elasticsearch-example
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.tar.gz
    tar -xzf elasticsearch-5.1.1.tar.gz
    ./elasticsearch-5.1.1/bin/elasticsearch

    When ElasticSearch starts up there should be a lot of output printed to the terminal window. To check that its up and running correctly open up a new terminal window and run this curl command:

    当ElasticSearch启动时,应该在终端窗口上打印很多输出。 要检查其启动和运行是否正确,请打开一个新的终端窗口并运行以下curl命令:

    curl -XGET http://localhost:9200

    The response should be something like this:

    响应应该是这样的:

    {  "name" : "6xIrzqq",  "cluster_name" : "elasticsearch",  "cluster_uuid" : "eUH9REKyQOy4RKPzkuRI1g",  "version" : {    "number" : "5.1.1",    "build_hash" : "5395e21",    "build_date" : "2016-12-06T12:36:15.409Z",    "build_snapshot" : false,    "lucene_version" : "6.3.0"  },  "tagline" : "You Know, for Search"

    Great, you now have ElasticSearch running on your local machine! It’s time to set up your Django project.

    太好了,您现在已经在本地计算机上运行了ElasticSearch! 现在该设置您的Django项目了。

    设置Django项目 (Setting up a Django project)

    First you create a virtual environment with virtualenv venv and enter it with source venv/bin/activate in order to keep everything contained. Then you install some packages:

    首先,您使用virtualenv venv创建一个虚拟环境,并使用source venv/bin/activate输入它,以保留所有内容。 然后安装一些软件包:

    pip install djangopip install elasticsearch-dsl

    To start a new Django project you run:

    要启动一个新的Django项目,请运行:

    django-admin startproject elasticsearchprojectcd elasticsearchprojectpython manage.py startapp elasticsearchapp

    After you created your new Django projects you need to create a model that you will use. For this guide I chose to go with a good old fashioned blog post example. In models.py you place the following code:

    创建新的Django项目后,您需要创建一个将要使用的模型。 在本指南中,我选择了一个很好的老式博客文章示例。 在models.py ,放置以下代码:

    from django.db import modelsfrom django.utils import timezonefrom django.contrib.auth.models import User# Create your models here.# Blogpost to be indexed into ElasticSearchclass BlogPost(models.Model):   author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blogpost')   posted_date = models.DateField(default=timezone.now)   title = models.CharField(max_length=200)   text = models.TextField(max_length=1000)

    Pretty straight forward, so far. Don’t forget to add elasticsearchapp to INSTALLED_APPS in settings.py and register your new BlogPost model in admin.py like this:

    到目前为止,还挺简单的。 不要忘记添加elasticsearchappINSTALLED_APPS中的settings.py和注册新的博客帖子模型admin.py是这样的:

    from django.contrib import adminfrom .models import BlogPost# Register your models here.# Need to register my BlogPost so it shows up in the adminadmin.site.register(BlogPost)

    You must also python manage.py makemigrations, python manage.py migrate and python manage.py createsuperuser to create the database and an admin account. Now, python manage.py runserver, go to http://localhost:8000/admin/ and login. You should now be able to see your Blog posts model there. Go ahead and create your first blog post in the admin.

    您还必须python manage.py makemigrationspython manage.py migratepython manage.py createsuperuser创建数据库和管理员帐户。 现在,运行python manage.py runserver ,转到http://localhost:8000/admin/并登录。 现在,您应该可以在那里看到您的Blog帖子模型。 继续并在管理员中创建您的第一篇博客文章。

    Congratulations, you now have a functioning Django project! It’s finally time to get into the fun stuff — connecting ElasticSearch.

    恭喜,您现在有了一个可正常运行的Django项目! 终于是时候玩有趣的东西了–连接ElasticSearch。

    将ElasticSearch与Django连接 (Connecting ElasticSearch with Django)

    You begin by creating a new file called search.py in our elasticsearchapp directory. This is where the ElasticSearch code will live. The first thing you need to do here is to create a connection from your Django application to ElasticSearch. You do this in your search.py file:

    首先,在我们的elasticsearchapp目录中创建一个名为search.py的新文件。 这是ElasticSearch代码的所在地。 您要做的第一件事是创建从Django应用程序到ElasticSearch的连接。 您可以在search.py文件中执行此操作:

    from elasticsearch_dsl.connections import connectionsconnections.create_connection()

    Now that you have a global connection to your ElasticSearch set-up you need to define what you want to index into it. Write this code:

    现在,您已经与ElasticSearch设置建立了全局连接,您需要定义要索引到其中的内容。 编写此代码:

    from elasticsearch_dsl.connections import connectionsfrom elasticsearch_dsl import DocType, Text, Dateconnections.create_connection()class BlogPostIndex(DocType):    author = Text()    posted_date = Date()    title = Text()    text = Text()    class Meta:        index = 'blogpost-index'

    It looks pretty similar to your model, right? The DocType works as a wrapper to enable you to write an index like a model, and the Text and Date are the fields so that they get the correct format when they get indexed.

    它看起来与您的模型非常相似,对吧? DocType用作包装器,使您能够像模型一样编写索引,而TextDate是字段,以便在建立索引时它们具有正确的格式。

    Inside the Meta you tell ElasticSearch what you want the index to be named. This will be a point of reference for ElasticSearch so that it knows what index it’s dealing with when initializing it in the database and saving each new object instance created.

    在Meta内部,您告诉ElasticSearch您希望索引被命名为什么。 这将是ElasticSearch的参考点,以便当在数据库中初始化索引并保存每个创建的新对象实例时,它知道要处理的索引。

    Now you need to actually create the mapping of your newly created BlogPostIndex in ElasticSearch. You can do this and also create a way to do the bulk indexing at the same time — how convenient right?

    现在,您需要在BlogPostIndex中实际创建新创建的BlogPostIndex的映射。 您可以执行此操作,还可以创建一种同时进行批量索引的方法-多么方便?

    数据批量索引 (Bulk indexing of data)

    The bulk command is located in elasticsearch.helpers which is included when you installed elasticsearch_dsl since it is built on top of that library. Do the following in search.py:

    bulk命令位于elasticsearch.helpers ,该命令是在该库之上构建的,因此在安装elasticsearch_dsl时会包含该命令。 在search.py执行以下操作:

    ...from elasticsearch.helpers import bulkfrom elasticsearch import Elasticsearchfrom . import models...
    ...def bulk_indexing():    BlogPostIndex.init()    es = Elasticsearch()    bulk(client=es, actions=(b.indexing() for b in models.BlogPost.objects.all().iterator()))

    “What is going on here?” you might be thinking. It’s not that complicated, actually.

    “这里发生了什么?” 你可能在想。 实际上,它并不那么复杂。

    Since you only want to do bulk indexing whenever you change something in our model you init() the model which maps it into ElasticSearch. Then, you use the bulk and pass it an instance of Elasticsearch() which will create a connection to ElasticSearch. You then pass a generator to actions= and iterate over all the BlogPost objects you have in your regular database and call the .indexing() method on each object. Why a generator? Because if you had a lot of objects to iterate over a generator would not have to first load them into memory.

    由于只要在模型中进行更改,您就只想进行批量索引编制,因此您可以使用init()模型将其映射到ElasticSearch中。 然后,您使用bulk并将其传递给Elasticsearch()实例,该实例将创建到ElasticSearch的连接。 然后,您将生成器传递给actions=并遍历常规数据库中所有的BlogPost对象,并在每个对象上调用.indexing()方法。 为什么要使用发电机? 因为如果要在生成器上迭代的对象很多,则不必先将它们加载到内存中。

    There is just one problem with the above code. You don’t have an .indexing() method on your model yet. Lets fix that:

    上面的代码只有一个问题。 您的模型上还没有.indexing()方法。 让我们修复:

    ...from .search import BlogPostIndex...
    ...# Add indexing method to BlogPostdef indexing(self):   obj = BlogPostIndex(      meta={'id': self.id},      author=self.author.username,      posted_date=self.posted_date,      title=self.title,      text=self.text   )   obj.save()   return obj.to_dict(include_meta=True)

    You add the indexing method to the BlogPost model. It returns a BlogPostIndex and gets saved to ElasticSearch.

    您将索引方法添加到BlogPost模型。 它返回BlogPostIndex并保存到ElasticSearch。

    Lets try this out now and see if you can bulk index the blog post you previously created. By running python manage.py shell you go into the Django shell and import your search.py with from elasticsearchapp.search import * and then run bulk_indexing() to index all the blog posts in your database. To see if it worked you run the following curl command:

    现在让我们尝试一下,看看是否可以对以前创建的博客文章进行批量索引。 通过运行python manage.py shell您将进入Django shell,并from elasticsearchapp.search import * bulk_indexing() from elasticsearchapp.search import *导入search.py ,然后运行bulk_indexing()为数据库中的所有博客文章建立索引。 要查看它是否起作用,请运行以下curl命令:

    curl -XGET 'localhost:9200/blogpost-index/blog_post_index/1?pretty'

    You should get back your first blog post in the terminal.

    您应该在终端上找回第一篇博客文章。

    索引新保存的实例 (Indexing of newly saved instance)

    Next you need to add a signal that fires the .indexing() on each new instance that is saved every time a user saves a new blog post. In elasticsearchapp create a new file called signals.py and add this code:

    接下来,您需要添加一个信号,以在用户每次保存新博客帖子时保存的每个新实例上触发.indexing() 。 在elasticsearchapp创建一个名为signals.py的新文件,并添加以下代码:

    from .models import BlogPostfrom django.db.models.signals import post_savefrom django.dispatch import receiver@receiver(post_save, sender=BlogPost)def index_post(sender, instance, **kwargs):    instance.indexing()

    The post_save signal will ensure that the saved instance will get indexed with the .indexing() method after it is saved.

    post_save信号将确保保存后的实例在保存后将通过.indexing()方法建立索引。

    In order for this to work we also need to register Django that we’re using signals. We do this opening apps.py and adding the following code:

    为了使它起作用,我们还需要注册使用信号的Django。 我们这样做是打开apps.py并添加以下代码:

    from django.apps import AppConfigclass ElasticsearchappConfig(AppConfig):    name = 'elasticsearchapp'    def ready(self):        import elasticsearchapp.signals

    To to complete this we also need to tell Django that we’re using this new configuration. We do this inside the __init__.py inside our elasticsearchapp directory by adding:

    要完成此操作,我们还需要告诉Django我们正在使用此新配置。 我们通过添加以下内容在我们的elasticsearchapp目录中的__init__.py进行此操作:

    default_app_config = 'elasticsearchapp.apps.ElasticsearchappConfig'

    Now the post_save signal is registered with Django and is ready to listen for whenever a new blogpost is being saved.

    现在, post_save信号已在Django中注册,并且随时可以在保存新博客文章时进行监听。

    Try it our by going into the Django admin again and saving a new blogpost. Then check with a curl command if it was successfully indexed into ElasticSearch.

    再次进入Django管理员并保存一个新博客,尝试一下。 然后使用curl命令检查它是否已成功索引到ElasticSearch中。

    Now lets make a simple search function in search.py to find all posts filtered by author:

    现在,让我们在search.py创建一个简单的搜索功能,以查找按作者过滤的所有帖子:

    ...from elasticsearch_dsl import DocType, Text, Date, Search...
    ...def search(author):    s = Search().filter('term', author=author)    response = s.execute()    return response

    Lets try the search out. In the shell: from elasticsearchapp.search import * and run print(search(author="<author name&gt;")) :

    让我们尝试搜索。 在外壳中: from elasticsearchapp.search import *并运行print(search(author="<author name& gt;”)):

    >>> print(search(author="home"))<Response: [<Result(blogpost-index/blog_post_index/1): {'text': 'Hello world, this is my first blog post', 'title':...}>]>

    There you have it! You have now successfully indexed all your instances into ElasticSearch, created a post_save signal that indexes each newly saved instance, and created a function to search our ElasticSearch database for your data.

    你有它! 现在,您已成功将所有实例索引到ElasticSearch中,创建了一个post_save信号来索引每个新保存的实例,并创建了一个函数在我们的ElasticSearch数据库中搜索您的数据。

    结论 (Conclusion)

    This was a quite lengthy article but I hope it is written simple enough for even the beginner to be able to understand.

    这是一篇冗长的文章,但我希望它写得足够​​简单,即使是初学者也可以理解。

    I explained how to connect a Django model to ElasticSearch for indexing and searching, but there is so much more that ElasticSearch can do. I recommend reading on their website and exploring what other possibilities exist, such as spatial operations and full text search with intelligent highlighting. Its a great tool and I will be sure to use it in future projects!

    我解释了如何将Django模型连接到ElasticSearch进行索引和搜索,但是ElasticSearch可以做很多事情。 我建议在他们的网站上阅读并探索还有其他可能性,例如空间操作和带有智能突出显示的全文本搜索。 它是一个很棒的工具,我一定会在以后的项目中使用它!

    If you liked this article or have a comment or suggestion, please feel free to leave a message below. And stay tuned for more interesting stuff!

    如果您喜欢本文或有任何评论或建议,请随时在下面留言。 敬请期待更多有趣的东西!

    翻译自: https://www.freecodecamp.org/news/elasticsearch-with-django-the-easy-way-909375bc16cb/

    django中如何用es

    展开全文
  • es5原生模仿-es6Promise异步处理,不过在处理异常的时候有点小bug不是很完美,不过多级then 是没问题的和resolve,rejec 正常调用和异常处理调用是没问题的。本帖属于原创,转载请出名出处。es5原生模仿-es6...
  • 只实现 then 和 catchfunction promise(fn) {var state = 'pending';// 声明函数var nowResolve = function (arg) { return arg };var nowReject = function (arg) { return arg };var nextResolve = function (arg)...
  • resolve把状态从pending变成resolve,reject把状态从pending变成reject 一般我们promise的时候是包在一个函数中,在需要的时候去调用运行它: functionAsync(){var test = new Promise(function(resolve,reject){...
  • 如何删除es索引

    2020-07-22 10:07:35
    chrome 这个插件 elasticsearch head,连接数据库 index相当于数据库 type相当于数据库中的表 参考 https://blog.csdn.net/bsh_csn/article/details/53908406 https://www.cnblogs.com/xuwenjin/p/8792919.html
  • 前言Elasticsearch(ES)是近年来炙手可热的开源分布式搜索分析引擎,通过简单部署,它可以轻松实现日志实时分析、全文检索、结构化数据分析等多重诉求,并将挖掘数据价值的成本大幅降低。ES在腾讯内部和腾讯云用户中...
  • ![图片说明]... 如果所示,A、B代表两个矩形,如何在绘制这两个矩形时,达到上述效果,绘制这两个矩形的shader应该怎么什么原理呢? PS:FBO就不用说了,如果10000个图片,1000个矩形还不得慢死。。
  • TermVectorsRequest request = new TermVectorsRequest(index, indexType, id);request.setFields("content");request.setFieldStatistics(true);request.setTermStatistics(true);request.setPositions(true);...
  • 京东到家订单中心系统业务中,无论是外部商家的订单生产,或是内部上下游系统的依赖,订单查询的调用量都非常大,造成了订单数据读多写少的情况。...如果大家对于学习Java有任何问题(学习方法,学习效率,如何就...
  • 3.1.2 画出你的流程 流程图的目的是一种简明扼要的方法看清楚流程中的每个步骤,关于流程图的画法网上有很多种不同的版本,但万法归宗,总之是要能画出能够让业务方、开发等不同角色的人能够看懂的流程图,本文...
  • 首先,什么是可选链(optional-chaining),这是在ES2020中引入的新特性,在typescript v3.7中也已经支持。 要知道,你不能只在代码中写foo.bar.baz(),而不检查foo是否存在,然后检查foo.bar是否存在,再检查foo.
  • 其实很简单.自己就是个搬运工而已。。其实连搬运都谈不上了,就是 copy...... 翻译一下: http://www.scottlu.com/2008/04/fast-2d-graphics-wopengl-es.html 关键字: Pbuffer, glTexSubImage2D, glDr...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,170
精华内容 468
关键字:

如何用es