elasticsearch: using wildcards for integer values - elasticsearch

I would like to find all objects which have a year value which starts with 20... so I would like to search for 20*, where * is a wild card.
I tries something like
'match_phrase_prefix': { 'year': { 'query': '20*', 'max_expansions': 50}}
but I guess this only works for strings. How would I do this for integers?
EDIT: I found a solution... its not pretty but it works
year_query = '20'
if len(str(year_query)) < 4:
try:
low_year, high_year = extend_year(int(year_query))
filter_list.append({"range": {"year": {"gte": low_year, "lte": high_year}}})
except ValueError:
print "Not a valid input for year"
pass
else:
for year in year_query.split(','):
if '-' in year:
year_range = year.split('-')
high_year = year_range[1].strip()
if len(high_year) < 4:
low_year, high_year = extend_year(high_year)
try:
filter_list.append({"range" : {"year" : {"gte" : int(year_range[0].strip()),"lte" : int(high_year),"format": "yyyy"}}})
except ValueError:
print "Not a valid input for year"
pass
else:
try:
filter_list.append({ "match": {"year": int(year.strip()) }})
except ValueError:
print "Not a valid input for year"
pass
with the function
def extend_year(input_year):
length = len(str(input_year))
if length == 4:
return input_year, 0
elif length == 3:
return input_year*10, int(str(input_year) + '9')
elif length == 2:
return input_year*100, int(str(input_year) + '99')
elif length == 1:
return input_year*1000, int(str(input_year) + '999')
elif length == 0:
return 0, 9999
else:
return input_year, 0
if anybody can come up with a better solution please let me know

This should work
'range': { 'year': { 'gte': 2000, 'max_expansions': 50}}

Related

how to fix 'pylint too many statements'?

Is there a way to condense the following code to remove the pylint too many statements error. All this code is contained within a single function: -
selection = input("Enter 1, 2, 3, 4 or 5 to start: \n")
if selection == "1":
print("\nYou have selected "+"'"+question_data[0][0]+"', let's begin!")
for key in science:
print("--------------------------------------------------------\n")
print(key)
for i in science_choices[num_question-1]:
print("")
print(i)
choice = input("Enter you answer (A, B or C): \n").upper()
answers.append(choice)
correct_answers += check_correct_answer(science.get(key), choice)
num_question += 1
player_score(correct_answers, answers)
elif selection == "2":
...................................................
elif selection == "3":
...................................................
elif selection == "4":
...................................................
elif selection == "5":
...................................................
else:
print("\nYou entered an incorrect value.Please try again.\n")
start_new_quiz()
the program works as is but it's for a college assignment and I would prefer not to submit with pylint errors.
One obvious way is to create a function for each selection:
selection = input("Enter 1, 2, 3, 4 or 5 to start: \n")
if selection == "1":
treat_one()
elif selection == "2":
...................................................
elif selection == "3":
...................................................
elif selection == "4":
...................................................
elif selection == "5":
...................................................
else:
print("\nYou entered an incorrect value.Please try again.\n")
start_new_quiz()
def treat_one():
print("\nYou have selected "+"'"+question_data[0][0]+"', let's begin!")
for key in science:
print("--------------------------------------------------------\n")
print(key)
for i in science_choices[num_question-1]:
print("")
print(i)
choice = input("Enter you answer (A, B or C): \n").upper()
answers.append(choice)
correct_answers += check_correct_answer(science.get(key), choice)
num_question += 1
player_score(correct_answers, answers)
You could also create a dictionary:
functions_to_use = {
"1": treat_one,
...
}
function = functions_to_use.get(selection)
if function is None:
print("\nYou entered an incorrect value.Please try again.\n")
else:
function()
Of if you want to over-engineer it, use getattr:
function = getattr("treat_{selection}", error)
function()
def error():
print("\nYou entered an incorrect value.Please try again.\n")

How do I make this flow from def get_burger_choice to def get_burger_price

So I am trying to make this continue from get_burger_choice to get_burger price to where it would flow and finish the program. I am trying to write this program to where I could get the type of burger then the price from get_burger_price. Heres the problem, I do not know how to intergrate it towards doing that. I am completely stuck after the part where it says "no" at the end part of get_burger_choice to where I can transition to get_burger_price
Here is the code that I used : the part highlighted is where Im having trouble transitioning:I would like it to where if the user says no, it would ask if they would like to see the price, and it would transition towards get_burger_price
def get_burger_choice():
SIZE = 3
burgers = ["Cheesy", "Regular", "Veggie" ]
search = True
while search == True:
index = 0
found = False
print("What type of burger would you like?")
searchValue = input()
while found == False and index <= SIZE - 1:
if burgers[index].lower()==searchValue.lower():
found = True
else:
index = index + 1
if found:
print("That is on our menu")
else:
print("That is not in our menu")
print("Do you want see another burger? yes/no: ")
tryagain = input()
if tryagain.lower() =='no':
print("Would you like to see the price? yes or no: ")
if
def price_burger(burger_choice, burger_price):
if burger_price == 'cheesy':
burger_price = 3.00
elif burger_choice == 'regular':
burger_price = 2.00
elif burger_choice == 'veggie':
burger_price = 2.00
else:
print("we do not have that, sorry")
return burger_price
def total_price(burger_price, burger_choice=None):
print("The burger cost $", burger_price)
def closing(burger_choice):
if burger_choice == 'cheesy':
print("Nice selection, This is our best!")
else:
print("enjoy!")
def main(burger_price = 0):
choice = "yes"
while choice != "no":
burger_choice = get_burger_choice()
burger_price = price_burger(burger_choice,burger_price)
total_price(burger_price)
closing(burger_choice)
choice = input("\nWould you like to try again? yes or no: ")
main()

How to do smart sort in groovy? [duplicate]

I have a list of version numbers like,
Versions = [0.0.10, 0.0.11, 0.0.13, 0.0.14, 0.0.15, 0.0.16, 0.0.17, 0.0.18, 0.0.19, 0.0.20, 0.0.21, 0.0.22, 0.0.23, 0.0.24, 0.0.25, 0.0.26, 0.0.27, 0.0.28, 0.0.29, 0.0.3, 0.0.30, 0.0.33, 0.0.34, 0.0.35, 0.0.36, 0.0.37, 0.0.38, 0.0.39, 0.0.4, 0.0.41, 0.0.42, 0.0.43, 0.0.44, 0.0.45, 0.0.46, 0.0.47, 0.0.48, 0.0.49, 0.0.5, 0.0.5-delivery.5, 0.0.50, 0.0.51, 0.0.52, 0.0.53, 0.0.54, 0.0.55, 0.0.56, 0.0.57, 0.0.58, 0.0.59, 0.0.6, 0.0.60, 0.0.61, 0.0.62, 0.0.63, 0.0.64, 0.0.7, 0.0.8, 0.0.9]'
And i need to get the last version (0.0.64), Versions.sort() && Collections.max(Versions) doesn't work for me.
So I developed this function blow
def mostRecentVersion(def versions) {
def lastversion = "0.0.0"
for (def items : versions) {
def version = items.tokenize('-')[0]
def ver = version.tokenize('.')
def lastver = lastversion.tokenize('.')
if (lastver[0].toInteger() < ver[0].toInteger() ){
lastversion = version
}else if(lastver[0].toInteger() == ver[0].toInteger()) {
if (lastver[1].toInteger() < ver[1].toInteger() ){
lastversion = version
}else if(lastver[1].toInteger() == ver[1].toInteger()){
if (lastver[2].toInteger() < ver[2].toInteger() ){
lastversion = version
}
}
}
}
return lastversion }
i'm asking if there is something better,
Thank you for help :)
the idea:
build map with sortable key and original version value, then sort map by keys, then get only values
to create sortable key for each value
split version to digits & not-digit strings array
prepend to each part 0 to have minimum length 3 (assume each number not longer then 3 digits)
join array to string
so, for 0.11.222-dev ->
1. [ '0', '.', '11', '222', '-dev' ]
2. [ '000', '00.', '011', '222', '-dev' ]
3. '00000.011222-dev'
the code
def mostRecentVersion(versions){
return versions.collectEntries{
[(it=~/\d+|\D+/).findAll().collect{it.padLeft(3,'0')}.join(),it]
}.sort().values()[-1]
}
//test cases:
def fullVersions = ['0.0.10', '0.0.11', '0.0.13', '0.0.14', '0.0.15', '0.0.16',
'0.0.17', '0.0.18', '0.0.19', '0.0.20', '0.0.21', '0.0.22', '0.0.23', '0.0.24',
'0.0.25', '0.0.26', '0.0.27', '0.0.28', '0.0.29', '0.0.3', '0.0.30', '0.0.33',
'0.0.34', '0.0.35', '0.0.36', '0.0.37', '0.0.38', '0.0.39', '0.0.4', '0.0.41',
'0.0.42', '0.0.43', '0.0.44', '0.0.45', '0.0.46', '0.0.47', '0.0.48', '0.0.49',
'0.0.5', '0.0.5-delivery.5', '0.0.50', '0.0.51', '0.0.52', '0.0.53', '0.0.54',
'0.0.55', '0.0.56', '0.0.57', '0.0.58', '0.0.59', '0.0.6', '0.0.60', '0.0.61',
'0.0.62', '0.0.63', '0.0.64', '0.0.7', '0.0.8', '0.0.9']
assert mostRecentVersion(fullVersions) == '0.0.64'
assert mostRecentVersion(['0.0.5-delivery.5', '0.0.3', '0.0.5']) == '0.0.5-delivery.5'
assert mostRecentVersion(['0.0.5.5', '0.0.5-delivery.5', '0.0.5']) == '0.0.5.5'
I believe this will work... it also keeps the original version strings around, incase 0.5.5-devel.5 is the latest... It relies on the fact that Groovy will use a LinkedHashMap for the sorted map, so the order will be preserved :-)
def mostRecentVersion(def versions) {
versions.collectEntries {
[it, it.split(/\./).collect { (it =~ /([0-9]+).*/)[0][1] }*.toInteger()]
}.sort { a, b ->
[a.value, b.value].transpose().findResult { x, y -> x <=> y ?: null } ?:
a.value.size() <=> b.value.size() ?:
a.key <=> b.key
}.keySet()[-1]
}
def fullVersions = ['0.0.10', '0.0.11', '0.0.13', '0.0.14', '0.0.15', '0.0.16', '0.0.17', '0.0.18', '0.0.19', '0.0.20', '0.0.21', '0.0.22', '0.0.23', '0.0.24', '0.0.25', '0.0.26', '0.0.27', '0.0.28', '0.0.29', '0.0.3', '0.0.30', '0.0.33', '0.0.34', '0.0.35', '0.0.36', '0.0.37', '0.0.38', '0.0.39', '0.0.4', '0.0.41', '0.0.42', '0.0.43', '0.0.44', '0.0.45', '0.0.46', '0.0.47', '0.0.48', '0.0.49', '0.0.5', '0.0.5-delivery.5', '0.0.50', '0.0.51', '0.0.52', '0.0.53', '0.0.54', '0.0.55', '0.0.56', '0.0.57', '0.0.58', '0.0.59', '0.0.6', '0.0.60', '0.0.61', '0.0.62', '0.0.63', '0.0.64', '0.0.7', '0.0.8', '0.0.9']
assert mostRecentVersion(fullVersions) == '0.0.64'
assert mostRecentVersion(['0.0.5-delivery.5', '0.0.3', '0.0.5']) == '0.0.5-delivery.5'
assert mostRecentVersion(['0.0.5.5', '0.0.5-delivery.5', '0.0.5']) == '0.0.5.5'
Edit:
Made a change so that 0.5.5.5 > 0.5.5-devel.5

Painless Scripting Kibana 6.4.2 not matching using matcher, but matches using expression conditional

Hello I'm trying to take a substring of a log message using regex in kibana scripted fields. I've run into an interesting scenario that doesn't add up. I converted the message field to a keyword so I could do scripted field operations on it.
When I match with a conditional such as:
if (doc['message'].value =~ /(\b(?:\d{1,3}\.){3}\d{1,3}\b)/) {
return "match"
} else {
return "no match"
}
This will match the ip and return correctly that there is an ip in the message. However, whenever I try to do the matcher function which splits the matched text into substrings it doesn't find any matches.
Following the guide on Elastic's documentation for doing this located here:
https://www.elastic.co/blog/using-painless-kibana-scripted-fields
This is the example script they give to match the first octet of an ip in a log message. However, this returns no matches when indeed there is ip addresses in the log message. I can't even match just text characters no matter what I do it returns 0 matches.
I have enabled rexex in the elasticsearch.yml in my cluster as well.
def m = /^([0-9]+)\..*$/.matcher(doc['message'].value);
if ( m.matches() ) {
return Integer.parseInt(m.group(1))
} else {
return m.matches() + " - " + doc['message'].value;
}
This returns 0 matches. Even if I use the same expression used for the conditional:
/(\b(?:\d{1,3}.){3}\d{1,3}\b)/
the matcher will still return false.
Any idea what I'm doing wrong here according to the documentation this should work.
I tried using subs-strings when the value exists in the if conditional but there is to many variations between the log messages. I also don't see a way to split and look through the list of outputs to pick the one with ip if I just use conditional for the scripted field.
Any idea on how to solve this:
Here is a example of that is returned form
def m = /^([0-9]+)\..*$/.matcher(doc['message'].value);
if ( m.matches() ) {
return Integer.parseInt(m.group(1))
} else {
return m.matches() + " - " + doc['message'].value;
}
The funny part is they all return false and this is essentially just looking for numbers with . and I've tried all kinds of regex combinations with no luck.
[
{
"_id": "VRYK_2kB0_nHZ_3qyRwt",
"Source-IP": [
"false - #Version: 1.0"
]
},
{
"_id": "VhYK_2kB0_nHZ_3qyRwt",
"Source-IP": [
"false - 2019-02-17 00:34:11 127.0.0.1 GET /status/web - 8611 - 127.0.0.1 ELB-HealthChecker/2.0 - 200 0 0 31"
]
},
{
"_id": "VxYK_2kB0_nHZ_3qyRwt",
"Source-IP": [
"false - #Software: Microsoft Internet Information Services 10.0"
]
},
{
"_id": "WBYK_2kB0_nHZ_3qyRwt",
"Source-IP": [
"false - #Date: 2019-03-26 00:00:08"
]
},
{
"_id": "WRYK_2kB0_nHZ_3qyRwt",
"Source-IP": [
127.0.0.1 ELB-HealthChecker/2.0 - 200 0 0 15"
]
},
{
ended up being the following:
if (doc["message"].value != null) {
def m = /(\b(?:\d{1,3}\.){3}\d{1,3}\b)/.matcher(doc["message"].value);
if (m.find()) { return m.group(1) }
else { return "no match" }
}
else { return "NULL"}

PHP Function to Calculate Relative Time (Human Readable / Facebook Style)

function RelativeTime($timestamp) {
$difference = time() - $timestamp;
$periods = array(
"sec", "min", "hour", "day", "week", "month", "years", "decade"
);
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for ($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if ($difference != 1) $periods[$j] .= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
I found the above PHP function on the interwebs. It seems to be working pretty well, except it has issues with dates far in the future.
For example, I get looping PHP error
division by zero
for $difference /= $lengths[$j]; when the date is in 2033.
Any ideas how to fix that? The array already accounts for decades, so I am hoping 2033 would result in something like "2 decades to go".
The problem is that the second array $lengths contains 7 elements so when executing the last iteration of the loop (after deviding by 10 - for the decades) $j = 7, $lengths[7] is undefined, so converted to 0 and therefore the test $difference >= $lengths[$j] returns true. Then the code enters an infinite loop. To overcome this problem, just add one more element to the $lengths array, say "100", so the for loop to terminate after processing the decades. Note that dates can be represented in UNIX timestamp if they are before January 19, 2038. Therefore you cannot calculate dates in more than 4 decates so 100 is sufficiant to break from the loop.

Resources