编程知识 cdmana.com

Two practical problems and interpretation of elasticsearch Online

Online combat issues 1

1、 Knowledge point

Use of scripts

2、 Problem description :

Hello , excuse me , stay ES I want to find out that the values of the two fields are the same .

But one of the fields is in a dictionary , How should I write ?

{
  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "source": "doc['user_id']= doc['music.sec_uid']",
            "lang": "painless"
          }
        }
      }
    }
  }
}

such as : I want to check user_id and sec_uid The same data , however sec_uid Is in music In the dictionary .

What can I do with it ?

3、 Problem analysis

demand The core yes : Compare two fields , Take out the data with the same value in different fields .

This is the time , To think of the traditional precision matching search or full-text search can not solve the problem .

You need a higher level of search , At this point, you have to go through the document quickly , Of course, it can also be combined with viewing documents .

Position the document step by step :

Read the official Demo, You can basically get the answer to the question .

Here will extend a very important knowledge point , It is also the most important point to pay attention to in the retrieval performance optimization of actual business scenarios :

Using scripts may slow down the search .

As the official documents read :

  • Scripts cannot be used Elasticsearch Inverted index structure or related optimization of . Sometimes this can slow down the search .

  • If you often use scripts to transform index data , It can be done by ( Pass data before writing Ingest Pipeline mode ) Make these changes to speed up the search . however , This usually means slower indexing speed ( Data write speed ).

4、 Practical solution

PUT test_002
{
  "mappings": {
    "properties": {
      "user_id": {
        "type": "keyword"
      },
      "music": {
        "properties": {
          "sec_uid": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

GET test_002/_mapping

POST test_002/_bulk
{"index":{"_id":1}}
{"user_id":333,"music.sec_uid":444}
{"index":{"_id":2}}
{"user_id":333,"music.sec_uid":333}
{"index":{"_id":3}}
{"user_id":333,"music.sec_uid":555}


POST test_002/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "script": {
            "script": {
              "source":"doc['user_id']==doc['music.sec_uid']",
              "lang":"painless"
            }
          }
        }
      ]
    }
  }
}

Online combat issues 2

1、 Knowledge point

update_by_query,  ingest Data preprocessing + painless Use of scripts

2、 Problem description

update Script , A time field time, All are 2020-08-10 xx:xx:xx, How to replace all the values of this field with 2020-10-24 xx:xx:xx, Just change the date , Don't change the time, the minute, the second .

How to write it ? Please give me some advice

3、 Problem analysis

The core of demand is :

  • Batch update

In my mind, there is a reflection of :update_by_query

  • A partial update based on a specific value

In my mind, Ma showed out :painless Script processing

  • Script implementation selection

The selection 1: direct update_by_query combination painless

The selection 2:update_by_query combination inges t combination painless Script processing

I personally tend to ingest, I feel that grammar is relatively friendly .

Position the document step by step :

Read the official Demo, Combined with the needs of disassembly , You can basically get the answer to the question .

Be careful :Ingest It's a new feature in later versions , But in the 7.X edition , It's been a long time . People use less , But it's really important , It is suggested to use more 、 Often new !

4、 Practical solution ( Non optimal solution )

Be careful : Here is an example DSL.

PUT my_index
{
  "mappings": {
      "properties": {
        "date": {
          "type": "keyword" 
        }
    }
  }
}

PUT my_index/_doc/2
{
  "date": "2015-01-01T12:10:30Z"
}



PUT _ingest/pipeline/my_pipeline
{
  "description": "use index:my-index",
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": "ctx.data_new = ctx.date.replace('2015-01-01', '2020-01-01')"
      }
    }
  ]
}

POST my_index/_update_by_query?pipeline=my_pipeline
{
  "query":{
    "match_all":{}
  }
}

GET my_index/_search

Summary

Don't panic in case of problems ,

Dismantle the problem to help .

After dismantling, find the document ,

Combine documentation and disassembly requirements ,

The problem is solved naturally .

By dismantling the problem , It's more important than anything to get a strategy and methodology for dealing with similar problems !

People have different opinions or fancy solutions to the problem , Welcome to leave a message .

More recommendations :

blockbuster | screwing Elasticsearch Methodological cognitive checklist (2020 National day update )

If you can get a driver's license, you can pass Elastic Certification examination !

more short time more Learn quickly more More dry !

China  40%+ Elastic Certified engineers come from !

And global  800+ Elastic Enthusiasts fight together Elasticsearch!

版权声明
本文为[Mingyi world]所创,转载请带上原文链接,感谢

Scroll to Top