According to the Chinese word segmentation address provided on Github, I lead you to operate in the way of page. If you have not configured the environment, you can refer to the link address of the article I wrote before

First, basic use

  • 1. Start the Docker container

  • 2. Enter http://localhost:9100/ in the address box of the browser

  • Create index; create index;

  • 4. Insert data

  • 5, Query all data

    Be sure to write _search and have no content body in the content body

  • 6. Modify data based on the _id

    What data do you put in the content body

  • 7. Fuzzy query data according to conditions

Second, innestjsIn the integrationElasticSearch

  • 1. Refer to the official website

  • 2. Create a NestJS project

  • 3. Install dependency packages

    npm i --save @nestjs/elasticsearch @elastic/elasticsearch
    Copy the code
  • Add ElasticsearchModule to app.module. ts, or create a separate module and add it to the root module

    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { ElasticsearchModule } from '@nestjs/elasticsearch';
    
    @Module({
      imports: [
        ElasticsearchModule.register({
          node: 'http://localhost:9200'})],controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
    Copy the code
  • Add, delete, change, and check the data in the service layer. This is simple to use. It is just to show how to operate in nestJS

    import { Injectable } from '@nestjs/common';
    import { ElasticsearchService } from '@nestjs/elasticsearch';
    
    @Injectable(a)export class AppService {
      constructor(
        private readonly elasticsearchService: ElasticsearchService
      ) {}
    
      // Add, modify, delete data
      async bulk(params: any) {
        return await this.elasticsearchService.bulk(params);
      }
    
      // Query data
      async search(params: any) {
        return await this.elasticsearchService.search(params); }}Copy the code
  • 6. Add data to ElasticSearch

    async bulk() {
        return await this.elasticsearchService.bulk({
          body: [
          	// The specified database is news and the specified Id = 1
            { index: { _index: 'news', _type: 'doc', _id: '1' } }, 
            { Content: 'Simulated data insertion'}
          ]
        });
      }
    Copy the code
  • 7, delete data (by id)

    async bulk() {
      return await this.elasticsearchService.bulk({
        body: [{delete: { _index: 'news'._type: 'doc'._id: 'UZAgiHgBS54NjNlgPg5j'}}},]); }Copy the code
  • 8. Modify data

    async bulk() {
      return await this.elasticsearchService.bulk({
        body: [{update: { _index: 'news'._type: 'doc'._id: 'UpAhiHgBS54NjNlgfA6i'}},// Just modify the field you have changed, the previous field will not be deleted
          { doc: { content: 'I am modified data'}}},]); }Copy the code
  • 9. Query data (fuzzy query)

    async search() {
        return await this.elasticsearchService.search({
          index: 'news'.type: 'doc'.body: {
            query: {
              match: {
                content: 'China'.// Fuzzy query, a bit of match in type re}}}}); }Copy the code
  • 10, paging query data

    async search() {
      const pageSize = 10;
      const pageNumber = 1
      return await this.elasticsearchService.search({
        index: 'news'.type: 'doc'.body: {
          from: (pageNumber - 1) * pageSize,  // Where to start
          size: pageSize, // Query the number of entries
          query: {
            match: {
              content: 'China' // Search the contents of the query}}}}); }Copy the code
  • 11. Query quantity

    async count() {
      return await this.elasticsearchService.count({
        index: 'news'.type: 'doc'.body: {
          query: {
            match: {
              content: 'China'}}}}); }Copy the code
  • 12. Refer to API