How to add OR condition in Elasticsearch ruby query? - ruby

In my use case, I would like to add an OR condition in Elasticsearch query. Here is my query,
query_body = {
'query' => {
'bool' => {
'must' => [{ 'range' => {'#timestamp' => { 'from' => stream_filters[:first_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ"), 'to' => stream_filters[:second_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ") } } }, {'term' => {"#timeout" => true} }, {'term' => {"#dest" => dest} }, {'term' => {"#source" => source} } ]
}
}, 'facets' => facets
}
I would like to add 'term' => {"#dest" => ' '} empty check for #dest along with 'term' => {"#dest" => dest}
I tried to add an or condition, But it is not working.
query_body = {
'query' => {
'bool' => {
'must' => [{ 'range' => {'#timestamp' => { 'from' => stream_filters[:first_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ"), 'to' => stream_filters[:second_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ") } } }, {'term' => {"#timeout" => true} }, {'term' => {"#source" => source} } ],
'filter' => {
'or' => [{
'term' => { "#dest" => dest }
'term' => { "#dest" => ' ' }
}]
}
}
}, 'facets' => facets
}
Could someone help me with this?

It seems like a syntax error in your filter clause. Please try with the correct syntax as below :
'filter' => {
'or' => [
{
'term' => { "#dest" => dest }
},
{
'term' => { "#dest" => ' ' }
}
]
}

Related

Default value for select in custom Modifier

I have a select field from a form defined like bellow. As you can see that select has two values, Yes and No. I'm looking to set a default value for this select.
"children" => [
"bc_offer_is_duration_count_fixed" => [
"arguments" => [
"data" => [
"config" => [
"dataType" => "select",
"formElement" => "select",
"visible" => "1",
"required" => "1",
"validation" => [
'required-entry' => "1"
],
"default" => null,
"label" => __('Is duration count fixed'),
"scopeLabel" => __('[GLOBAL]'),
"code" => "bc_offer_offer_durations",
"source" => "content",
"globalScope" => true,
"sortOrder" => 10,
"componentType" => "field",
"component" => "Project_OfferProducts/js/form/element/offer-is-duration-count-fixed",
'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']]
]
]
]
]
]
It's part of the modifyData method from a class defined here
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier as CatalogAbstractModifier;
abstract class AbstractModifier extends CatalogAbstractModifier
Obviously I already tried to set "default" => "Yes" and "default" => "1" and "default" => 1
I also have an offer-js-duration-count-fixed.js file with that content
define([
'Magento_Ui/js/form/element/select',
'Project_OfferProducts/js/model/offer-configuration/context'
], function (Select, context) {
'use strict';
return Select.extend({
updatingDurationFromField: false,
initialize: function () {
this._super();
context.isDurationCountFixed.subscribe(function(newValue){
this.refreshInField(newValue);
}.bind(this));
this.value.subscribe(this.refreshInGrid.bind(this));
if(context.isDurationCountFixed())
{
this.value(1);
}
else
{
this.value(0);
}
return this;
},
refreshInGrid: function(){
this.updatingDurationFromField = true;
context.isDurationCountFixed(this.value());
this.updatingDurationFromField = false;
},
refreshInField: function(newValue){
if(!this.updatingDurationFromField)
{
this.value(context.isDurationCountFixed());
}
}
});});
I'm under Magento 2.
just what i think
change this
'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']]
to
'options' => [['label' => __('Yes'), 'value' => 1], ['label' => __('No'), 'value' => 0]]
and change
"default" => null,
to
"default" => "1",

Logstash : Is there a way to change some of the properties in document while migrating

I have been migrating some of the indexes from self-hosted Elasticsearch to AmazonElasticSearch using Logstash. While migrating the documents, We need to change the field names in the index based on some logic.
Our Logstash Config file
input {
elasticsearch {
hosts => ["https://staing-example.com:443"]
user => "userName"
password => "password"
index => "testingindex"
size => 100
scroll => "1m"
}
}
filter {
}
output {
amazon_es {
hosts => ["https://example.us-east-1.es.amazonaws.com:443"]
region => "us-east-1"
aws_access_key_id => "access_key_id"
aws_secret_access_key => "access_key_id"
index => "testingindex"
}
stdout{
codec => rubydebug
}
}
Here it is one of the documents for the testingIndex from our self-hosted elastic search
{
"uniqueIdentifier" => "e32d331b-ce5f-45c8-beca-b729707fca48",
"createdDate" => 1527592562743,
"interactionInfo" => [
{
"value" => "Hello this is testing",
"title" => "msg",
"interactionInfoId" => "8c091cb9-e51b-42f2-acad-79ad1fe685d8"
},
{
**"value"** => """"{"edited":false,"imgSrc":"asdfadf/soruce","cont":"Collaborated in <b class=\"mention\" gid=\"4UIZjuFzMXiu2Ege6cF3R4q8dwaKb9pE\">#2222222</b> ","chatMessageObjStr":"Btester has quoted your feed","userLogin":"test.comal#google.co","userId":"tester123"}"""",
"title" => "msgMeta",
"interactionInfoId" => "f6c7203b-2bde-4cc9-a85e-08567f082af3"
}
],
"componentId" => "compId",
"status" => [
"delivered"
]
},
"accountId" => "test123",
"applicationId" => "appId"
}
This is what we are expecting when documents get migrated to our AmazonElasticSearch
{
"uniqueIdentifier" => "e32d331b-ce5f-45c8-beca-b729707fca48",
"createdDate" => 1527592562743,
"interactionInfo" => [
{
"value" => "Hello this is testing",
"title" => "msg",
"interactionInfoId" => "8c091cb9-e51b-42f2-acad-79ad1fe685d8"
},
{
**"value-keyword"** => """"{"edited":false,"imgSrc":"asdfadf/soruce","cont":"Collaborated in <b class=\"mention\" gid=\"4UIZjuFzMXiu2Ege6cF3R4q8dwaKb9pE\">#2222222</b> ","chatMessageObjStr":"Btester has quoted your feed","userLogin":"test.comal#google.co","userId":"tester123"}"""",
"title" => "msgMeta",
"interactionInfoId" => "f6c7203b-2bde-4cc9-a85e-08567f082af3"
}
],
"componentId" => "compId",
"status" => [
"delivered"
]
},
"accountId" => "test123",
"applicationId" => "appId"
}
What we need is to change the "value" field to "value-keyword" wherever we find some JSON format. Is there any other filter in Logstash to achieve this
As documented in the Logstash website:
https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-rename
You can use the mutate filter, applying the rename function.
For example:
filter {
mutate {
replace => { "old-field" => "new-field" }
}
}
For nested fields, you could just pass the path of the field:
filter {
mutate {
replace => { "[interactionInfo][value]" => "[interactionInfo][value-keyword]" }
}
}
Try adding this to your filter:
filter {
ruby {
code => "event.get('interactionInfo').each { |item| if item['value'].match(/{.+}/) then item['value-keyword'] = item.delete('value') end }"
}
}

ElasticSearch 7 - combine filters

I use ES 7 and Laravel implementation, I want to combine a range and a term match, according to documentation, I did this :
$items = $client->search([
'index' => $instance->getSearchIndex(),
'type' => $instance->getSearchType(),
'body' => [
'size' => 50,
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'fields' => config('elasticsearch.fields'),
'query' => $query,
],
],
'filter' => [
'bool' => [
'must' => [
'range' => [
'note' => [
'gte' => config('elasticsearch.note_minimum')
]
],
'term' => [
'type_video_id' => 5
],
],
],
],
]
],
],
]);
And got this error :
"parsing_exception","reason":"[range] malformed query, expected
[END_OBJECT] but found [FIELD_NAME]
I only found documentation and examples for ES 2 about combining queries, did something change ?
I want my query to match the fields, and be filtered according to filter.
Here is the right way to do this:
$items = $client->search([
'index' => $instance->getSearchIndex(),
'type' => $instance->getSearchType(),
'body' => [
'size' => 50,
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'fields' => config('elasticsearch.fields'),
'query' => $query,
]
],
'filter' => [
[
'range' => [
'note' => [
'gte' => config('elasticsearch.note_minimum')
]
]
],
[
'term' => [
'type_video_id' => 5
]
]
]
]
]
]
]);
I don't have a way to test this, but I'm seeing a couple brackets where you need curlies. Also, don't you need you need $ before those "config"?
{
"query" => {
"bool" => {
"must" => [
{
"multi_match" => {
"query" => $query,
"fields" => $config('elasticsearch.fields')
}
}
],
"filter" => {
{
"range" => {
"note" => {
"gte" => $config('elasticsearch.note_minimum')
}
}
},
{
"term" => {
"type_video_id" => {
"value" => "5"
}
}
}
}
}
}
}
If this doesn't work, can you paste what the string looks like after your variables get rendered?

What's the best way to replace a string inside a string in ruby?

I have a bunch of these:
'link' => "http://twitter.com/home?status=Check out "{title}" {url}",
And Want to replace the {title} and {url} bits.
I'm currently doing this with gsub:
l.gsub! "{url}", URI::encode(#opts[:url])
l.gsub! "{title}", URI::encode(#opts[:title])
But I have the feeling there's a much better way to do this than with gsub...
#
This is an edit / addition to clarify:
class SocialBookmarkMaker
require 'open-uri'
attr_accessor :opts
def initialize(opts)
#opts = ##default_opts.merge opts
end
##default_opts = {
:icon_folder => "/images/icons/social_aquatic/24 X 24",
:sites => ['facebook', 'twitter', 'delicious', 'digg', 'stumbleupon', 'reddit', 'technorati', ],
:ext => 'png',
:url => 'not provided',
:title => 'not provided',
}
##bookmarks = {
'yahoo' => {
'name' => 'Yahoo! My Web',
'link' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u={url}&t={title}',
},
'google' => {
'name' => 'Google Bookmarks',
'link' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk={url}&title={title}',
},
'windows' => {
'name' => 'Windows Live',
'link' => 'https://favorites.live.com/quickadd.aspx?url={url}&title={title}',
},
'facebook' => {
'name' => 'Facebook',
'link' => 'http://www.facebook.com/sharer.php?u={url}&t={title}',
},
'digg' => {
'name' => 'Digg',
'link' => 'http://digg.com/submit?phase=2&url={url}&title={title}',
},
'ask' => {
'name' => 'Ask',
'link' => 'http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url={url}&title={title}',
},
'technorati' => {
'name' => 'Technorati',
'link' => 'http://www.technorati.com/faves?add={url}',
},
'delicious' => {
'name' => 'del.icio.us',
'link' => 'http://del.icio.us/post?url={url}&title={title}',
},
'stumbleupon' => {
'name' => 'StumbleUpon',
'link' => 'http://www.stumbleupon.com/submit?url={url}&title={title}',
},
'squidoo' => {
'name' => 'Squidoo',
'link' => 'http://www.squidoo.com/lensmaster/bookmark?{url}'
},
'netscape' => {
'name' => 'Netscape',
'link' => 'http://www.netscape.com/submit/?U={url}&T={title}',
},
'slashdot' => {
'name' => 'Slashdot',
'link' => 'http://slashdot.org/bookmark.pl?url={url}&title={title}',
},
'reddit' => {
'name' => 'reddit',
'link' => 'http://reddit.com/submit?url={url}&title={title}',
},
'furl' => {
'name' => 'Furl',
'link' => 'http://furl.net/storeIt.jsp?u={url}&t={title}',
},
'blinklist' => {
'name' => 'BlinkList',
'link' => 'http://blinklist.com/index.php?Action=Blink/addblink.php&Url={url}&Title={title}',
},
'dzone' => {
'name' => 'dzone',
'link' => 'http://www.dzone.com/links/add.html?url={url}&title={title}',
},
'swik' => {
'name' => 'SWiK',
'link' => 'http://stories.swik.net/?submitUrl&url={url}'
},
'shoutwire' => {
'name' => 'Shoutwrie',
'link' => 'http://www.shoutwire.com/?p=submit&&link={url}',
},
'blinkbits' => {
'name' => 'Blinkbits',
'link' => 'http://www.blinkbits.com/bookmarklets/save.php?v=1&source_url={url}',
},
'spurl' => {
'name' => 'Spurl',
'link' => 'http://www.spurl.net/spurl.php?url={url}&title={title}',
},
'diigo' => {
'name' => 'Diigo',
'link' => 'http://www.diigo.com/post?url={url}&title={title}',
},
'tailrank' => {
'name' => 'Tailrank',
'link' => 'http://tailrank.com/share/?link_href={url}&title={title}',
},
'rawsugar' => {
'name' => 'Rawsugar',
'link' => 'http://www.rawsugar.com/tagger/?turl={url}&tttl={title}&editorInitialized=1',
},
'twitter' => {
'name' => 'Twitter',
'link' => "http://twitter.com/home?status=Check out "{title}" {url}",
},
}
def self.bookmarks
##bookmarks
end
def icon_loc(site)
"http://common-resources.---.net.s3.amazonaws.com#{#opts[:icon_folder]}/#{site}.#{#opts[:ext]}"
end
def link_url(site)
l = SocialBookmarkMaker.bookmarks[site]['link']
l.gsub! "{url}", URI::encode(#opts[:url])
l.gsub! "{title}", URI::encode(#opts[:title])
l
end
end
shared/social_bookmarks/standard.html.haml
- opts ||= {}
- opts.merge! :url => request.url
- opts.merge! :title => "---.net: #{#layout[:social_bookmark_title] || #layout[:title] || default_view_title}"
- b = SocialBookmarkMaker.new opts
- b.opts[:sites].each do |site|
= link_to(image_tag( b.icon_loc(site) ), b.link_url(site), :title => "Share on #{SocialBookmarkMaker.bookmarks[site]['name']}")
I then call this like this in my rails layout:
render :partial => "shared/social_bookmarks/standard", :locals => { :opts => {:icon_folder => "/images/icons/social_aquatic/48 X 48" }}
Either you change your string to look like
"http://twitter.com/home?status=Check out "%{title}" %{url}"
and then use printf with a Hash
s = "http://twitter.com/home?status=Check out "%{title}" %{url}"
# you can of course use #opts as the Hash here.
s = s % {:title => "abc", :url => "def"} # => "http://twitter.com/home?status=Check out "abc" def"
and accept that it only works with Ruby 1.9.2 and upwards, or you continue using gsub but using the block syntax to condense it:
s.gsub!(/\{(.+?)\}/) do |m|
#opts[$1.to_sym]
end
You can just embed the variables directly in the string
'link' => "http://twitter.com/home?status=Check out "#{title}" #{url}"

Recurrence with Google calendar ruby API V3

Using the following code, I've not been able to push any recurring events to google calendar. However, take off the 'recurrence' item from event and it works.
What am I doing wrong ?
event = {
'summary' => 'Appointment',
'location' => 'Somewhere',
'start' => {
'dateTime' => '2011-06-03T10:00:00.000-07:00'
},
'end' => {
'dateTime' => '2011-06-03T10:25:00.000-07:00'
},
'recurrence' => [
"RRULE:FREQ=DAILY;COUNT=5"
]
}
result = #client.execute(:api_method => #service.events.insert,
:parameters => {'calendarId' => 'hg9a7o16bm6dj0tmuo481499mc#group.calendar.google.com'},
:body_object => event,
:headers => {'Content-Type' => 'application/json'})
puts result.data.id.to_s
The timezone needs to be set in a seperate field as this
event = {
'summary' => 'Appointment',
'location' => 'Somewhere',
'start' => {
'dateTime' => '2011-06-03T10:00:00.000-07:00',
'timeZone' => 'America/Montreal'
},
'end' => {
'dateTime' => '2011-06-03T10:25:00.000-07:00',
'timeZone' => 'America/Montreal'
},
'recurrence' => [
"RRULE:FREQ=DAILY;COUNT=5"
]
}
Please try with "RRULE:FREQ=WEEKLY;UNTIL=20120701T160000Z";

Resources