ویرگول
ورودثبت نام
Ali Zeiynali
Ali Zeiynali
خواندن ۴ دقیقه·۱۰ ماه پیش

هر آنچه باید در مورد دیتابیس الاستیک سرچ بدانید - 5

در این پست کمی دیگر به مبحث اسکریپت ها می پردازیم و Update ها، متغییر ها و...را تعریف میکنیم.


آپدیت

فیلد

برای آپدیت کردن یک doc توسط اسکریپت ها از این کد استفاده می شود.

PUT my-index/_doc/1
{ &quotcounter&quot: 1, &quottags&quot: [
&quotred&quot ]}
POST my-index/_update/1
{
&quotscript&quot: {
&quotsource&quot: &quotctx._source.counter += params.count&quot,
&quotlang&quot: &quotpainless&quot,
&quotparams&quot: {
&quotcount&quot: 4
}
}
}

در این کد یک doc با id 1 و با یک لیست color و یک counter 1 میسازیم.

در کد Update با زبان پینلس یک اسکریپت میزنیم و یک پارامتر کانت 4 به آن می پاسیم : دی

در کد، ctx به معنای کل doc است، _source فیلد های درون است و counter هم که فیلد است.
در اینجا پارامتر کانت را به کانتر در داک اضافه کردیم.

نتیجه: کانتر برابر 5 می شود.

آرایه

POST my-index/_update/1
{ &quotscript&quot: {
&quotsource&quot: &quotctx._source.tags.add(params['tag'])&quot,
&quotlang&quot: &quotpainless&quot,
&quotparams&quot: {
&quottag&quot: &quotblue&quot
}}}

در این کد به آرایه colors مقدار blue اضافه می شود، add برای افزودن مقدار به آرایه در java و painless استفاده می شود.

برای حذف تگ نیز از این کد استفاده کنید:
ctx._source.tags.remove(ctx._source.tags.indexOf(params['tag']))
برای دیگر کد ها میتوانید سرچ کنید یا به داکیومنت java و painless مراجعه کنید

برای افزودن یک فیلد به داک نیز از اسکریپت زیر استفاده کنید.

POST my-index/_update/1
{ &quotscript&quot : &quotctx._source.new_field = 'value_of_new_field'&quot}

در اینجا یک فیلد new_field با مقدار value_of_new_field افزوده شده است.
برای حذف هم: "ctx._source.remove('new_field')

و یک کد پیشرفته تر:

if (ctx._source.tags.contains(params['tag'])) { ctx.op = 'delete' } else { ctx.op = 'none' }

در این کد در صورتی که tag ها شامل پارامتر تگ بشوند، ctx.op که مخفف oprationاست، را به delete تغییر می دهیم، یعنی عملیات دیلیت را بر روی آن چیز انجام میدهیم.
یا در غیر این صورت هیچ کاری نمی کنیم.

متغیر ها

برای تعریف یک متغیر در painless به این صورت عمل کنید.

POST my-index/_update/1
{ &quotscript&quot: {
&quotsource&quot: &quotString newVar = 'HI!'; ctx._source.message = newVar + params[tag]&quot,
&quotlang&quot: &quotpainless&quot,
&quotparams&quot: { &quottag&quot: &quotblue&quot }}}

اول dataType را مینویسیم و سپس مقدار را به آن می دهیم.

بررسی چند اسکریپت

اسکریپت اول

POST mainindex/_update_by_query
{
&quotscript&quot: {
&quotsource&quot: &quotctx._source.allwords = ctx._source.allwords.replace('–', ';');ctx._source.allwords = ctx._source.allwords.replace('–', ';');ctx._source.allwords = ctx._source.allwords.replace('،', ';');ctx._source.allwords = ctx._source.allwords.replace(',', ';');&quot,
&quotlang&quot: &quotpainless&quot
},
&quotquery&quot: {
&quotbool&quot: {
&quotmust&quot: [{
&quotexists&quot: {
&quotfield&quot:&quotallwords&quot}}],
&quotmust_not&quot: [
{
&quotbool&quot: {
&quotshould&quot: [
{
&quotterm&quot: {
&quotallwords.keyword&quot: {
&quotvalue&quot: &quot&quot }}},{
&quotterm&quot: {
&quotallwords.keyword&quot: {
&quotvalue&quot: &quot&quot }}}]}}]}}}

ممکن است درک این کد کمی سخت باشد، اما با چند بار خواندن و بررسی کردن، میتوان به خوبی آن را درک کرد.

اول:

کوئری میگوید هر جا که فیلد allwords خالی نباشد و allwords.keyword نیز وجود داشته باشد و مساوی با ; نباشد، این کد یک کد مخصوص برنامه نویسی داده است، در این کد هدف ما جایگزینی ویرگول، کاما و خط فاصله با ویرگول است تا هنگام پردازش دچار مشکل نشویم.

در این پروژه قبلا پردازش هایی صورت گرفته بود و ممکن بود فیلد allword.keyword مساوی با ; شده باشد

اسکریپت دوم

POST my-index/_update_by_query
{
&quotscript&quot: {
&quotsource&quot: &quotctx._source.color.add('pink') &quot,
&quotlang&quot: &quotpainless&quot
},
&quotquery&quot: {
&quotbool&quot: {
&quotmust&quot: [
{
&quotexists&quot: {
&quotfield&quot: &quotcolor&quot }}]}}}

اسکریپت سوم

POST my-index/1/_update_by_query
{
&quotscript&quot: {
&quotsource&quot: &quotctx._source.array.add(['entity_id' :1, 'entity_title' : 'Title,'realation_type' : 'realation_type']); &quot,
&quotlang&quot: &quotpainless&quot
}
}

این هم یک آرایه از آبجکت ها ایجاد میکند.

اسکریپت چهارم

POST my-index/1/_update_by_query
{
&quotscript&quot: {
&quotsource&quot: &quottry {String year = ''; if(ctx._source.eb_year != null) year =ctx._source.eb_year.trim().replace('/', '').replace('هـ', ''); int d = Integer.parseInt(year);ctx._source.eb_year = d;} catch (NumberFormatException nfe) {ctx._source.remove('eb_year'); }&quot,
&quotlang&quot: &quotpainless&quot }}

این اسکریپت در صورتی که فیلد eb_year int نباشد آن را حذف میکند.

و آخرین اسکریپت

POST mainindex/_update_by_query
{
&quotscript&quot: {
&quotsource&quot: &quot String allwords = ctx._source.allwords; def a_chars = allwords.toCharArray(); int chars_len = a_chars.length; ArrayList result = new ArrayList(); int last_dot = chars_len; for(int i = chars_len -1; i>=-1; i--) { if (i == -1 || a_chars[i] == (char) ';' ){ String t1 = allwords.substring(i+ 1, last_dot).trim(); if(t1 != '') result.add(t1); last_dot = i; } } ctx._source.tags = result; &quot,
&quotlang&quot: &quotpainless&quot
},
&quotquery&quot: {
&quotbool&quot: {
&quotmust&quot: [
{
&quotexists&quot: {
&quotfield&quot:&quotallwords&quot }}],
&quotmust_not&quot: [
{
&quotbool&quot: {
&quotshould&quot: [
{
&quotterm&quot: {
&quotallwords.keyword&quot: {
&quotvalue&quot: &quot&quot }}},{
&quotterm&quot: {
&quotallwords.keyword&quot: {
&quotvalue&quot: &quot&quot }}}]}}]}}}

اگر فیلد allword (دوباره :دی) وجود داشته باشد این کد اجرا می شود.

String allwords = ctx._source.allwords; def a_chars = allwords.toCharArray(); int chars_len = a_chars.length;

این کد chars_len را برابر با تعداد کاراکتر های allwords میکند، دیگر آن را توضیح نمیدهم، میتوانید به داکیومنت آن مراجعه کنید.

کل این کد allwords را میخواند و با ; اسپلیت می کند و در آرایه result ذخیره میکند.


جلسه ی بعد

https://virgool.io/@Ali_Z/%D9%87%D8%B1-%D8%A2%D9%86%DA%86%D9%87-%D8%A8%D8%A7%DB%8C%D8%AF-%D8%AF%D8%B1-%D9%85%D9%88%D8%B1%D8%AF-%D8%AF%DB%8C%D8%AA%D8%A7%D8%A8%DB%8C%D8%B3-%D8%A7%D9%84%D8%A7%D8%B3%D8%AA%DB%8C%DA%A9-%D8%B3%D8%B1%DA%86-%D8%A8%D8%AF%D8%A7%D9%86%DB%8C%D8%AF-%D9%82%D8%B3%D9%85%D8%AA-%D8%A2%D8%AE%D8%B1-z1qkhawvbo3n





❤️💬

پ.ن: اگر کد ها اذیتتون کردن واقعا عذر می خوام، نحوه نمایش کد ها اصلا درست نیست - البته تقصیر منم نیست و تقصیر ویرگوله!
برنامه نویسیپایگاه دادهالاستیک سرچelesticsearchدیتابیس
یک برنامه نویس (نه چندان) نویسنده؟ https://coffeete.ir/Azeiynali
شاید از این پست‌ها خوشتان بیاید