Online combat problem 1

1. Knowledge points

Use of scripts

2. Problem Description:

Hello, I would like to ask, IN ES, I want to check 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"
          }
        }
      }
    }
  }
}
Copy the code

For example, I want to query user_id for the same value as sec_uid, but sec_uid is in the music dictionary.

How do I deal with it?

3. Problem analysis

The core requirement is: compare two fields, take out the data with the same value of different fields.

At this time, we should think of the traditional accurate matching search or full-text search can not solve the problem.

It requires a higher level of searching, a quick mental review of the document, and of course, a combination of looking at the document.

Step by step to locate the document:

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

A very important knowledge point will be extended here, which is also the point to pay special attention to the optimization of retrieval performance with the most feedback in practical business scenarios:

The use of scripts may slow down the search.

As the official document reads:

  • Scripts cannot take advantage of Elasticsearch’s inverted index structure or related optimizations. Sometimes this can lead to slower searches.
  • If you often use scripts to transform index data, you can speed up your search by making these changes during ingestion (piped through Ingest before data is written). However, this usually means slower indexing (data write speed).

4. Practical solutions

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"}}}]}}Copy the code

Online combat problem 2

1. Knowledge points

Update_by_query, ingest data preprocessing + painless script use

2. Problem description

Update script (2020-08-10 xx:xx:xx) update script (2020-10-24 xx:xx:xx) update script (2020-08-10 xx:xx:xx) update script (2020-10-24 xx:xx:xx)

How to write? Ask the bosses for advice

3. Problem analysis

The core requirements are:

  • Batch update

The mental map is update_by_query

  • A partial update based on a specific value

“Painless script processing” immediately came to mind

  • Script implementation selection

Model 1: Direct update_by_query with Painless

Model 2: Update_BY_query combined with INGes T combined with Painless script processing

I personally prefer ingest, personally feel the grammar is relatively friendly.

Step by step to locate the document:

Read the official Demo, combined with the needs of dismantling, basically can get the answer to the question.

Note: Ingest was a new feature in later releases, but by 7.x, it was a long time ago. We use less, but it is really very important, the suggestion should be multi-purpose, commonly used often new!

4. Practical solutions (non-optimal solutions)

Note: The following 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
Copy the code

summary

Don’t panic when you encounter problems.

Break down the problem to help.

After disassembling, looking for documents,

Combined with documentation and disassembly requirements,

The problem will solve itself.

It is more important to get strategies and methodologies for similar problems by breaking them down!

If you have different opinions or fancy solutions, please leave a comment.

More recommendations:

Blockbuster | into Elasticsearch methodology cognitive listing \ (National Day update edition 2020)

You can pass the Elastic certification exam with a driver’s license!

Learn more in less time, faster! \

40%+ Elastic certified engineers in China are here!

Play Elasticsearch with 800+ Elastic fans around the world!