MongoDB $regex query to number fails - regex

I stored info like this format on my MongoDB
{
name: 'Eric',
account: 13310
},
{
name: 'Ivan',
account: 12120
}
this is my noSQL sentence
db.users.find( "account": { $regex: /13/, $options: 'i' } );
But I got an error
Error: error: {
"ok" : 0,
"errmsg" : "$regex has to be a string",
"code" : 2,
"codeName" : "BadValue"
}
How could I make the query, I want to find whatever contains "13" on account index. I read about parse account index to String, but, the collection has over 100K documents, this is not the way.
I´m looking for this SQL Sentence
SELECT * FROM USERS WHERE ACCOUNT LIKE '%13%'

As you've seen, $regex only works with string fields; so because account is a numeric field, your query fails. Starting with MongoDB 4.0, you can use an aggregate pipeline to dynamically add a $toString version of account that you query against using a regex:
db.users.aggregate([
{$addFields: {accountStr: {$toString: '$account'}}},
{$match: {accountStr: /13/}}
])
The alternative would be to store a string copy of the account field in your documents that you could directly use.

I had a same problem, but i found a solution
Use like this:
"account": "/[A-z]*13/i"

Try this bro..
db.users.find( "account": { $regex: new RegExp( '^13' ), $options: 'i' } );

Related

Not able to get desired search results in ElasticSearch search api

I have field "xyz" on which i want to search. The type of the field is keyword. The different values of the field "xyz "are -
a/b/c/d
a/b/c/e
a/b/f/g
a/b/f/h
Now for the following query -
{
"query": {
"query_string" : {
"query" : "(xyz:(\"a/b/c\"*))"
}
}
}
I should only get these two results -
a/b/c/d
a/b/c/e
but i get all the four results -
a/b/c/d
a/b/c/e
a/b/f/g
a/b/f/h
Edit -
Actually i am not directly querying on ElasticSearch, I am using this API https://atlas.apache.org/api/v2/resource_DiscoveryREST.html#resource_DiscoveryREST_searchWithParameters_POST which creates the above mentioned query for elasticsearch, so i dont have much control over the elasticsearch query_string. What i can change is the elasticsearch analyzer for this field or it's type.
You'll need to let the query_string parser know you'll be using regex so wrap the whole thing in /.../ and escape the forward slashes:
{
"query": {
"query_string": {
"query": "xyz:/(a\\/b\\/c\\/.*)/"
}
}
}
Or, you might as well use a regexp query:
{
"query": {
"regexp": {
"xyz": "a/b/c/.*"
}
}
}

Bulk index django model to elastic search

I am trying to bulk index my django model to elastic search 6, my plan is to run this as a cron once a day to update the index.
import requests
data = serialize('json', CapitalSheet.objects.all())
data += "\n"
r = requests.post("http://127.0.0.1:9200/capitalsheet/_bulk", json = data)
print(r.content)
I am getting this error:
b'{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"},"status":400}'
If you can suggest something better, I would be glad.
I would recommend looking at the python library provided by elasticsearch. It will make it easier to do the bulk inserts. Here is a link to the docs:
https://elasticsearch-py.readthedocs.io/en/master/helpers.html#bulk-helpers
If you want to do it manually though, the ES bulk API actually requires two lines for every record you want to insert. The first line details what index and the type of operation, and the second line is the record to insert. For example your request body would look something like this:
{ "index" : { "_index" : "test", "_type" : "type1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "test", "_type" : "type1" } }
{ "field1" : "value2" }
The ES documentation explains this well here:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

Elasticsearch matching words

I have a problem. I want my query to match mysql with mysql!, mysql(, mysql ... in other words I want my query result to show only these values which have just word "mysql" OR word "mysql" and special characters around this word.
For example query should get - mysql, mysql, #mysql, mysql$. But not "mysql and R" or "mysql mysql".
I tried this query for matching, but I keep getting query malformed, no field after start_object, so not sure what to do.
Also wanted to ask maybe there are other option to perform the same task ?
POST skills/_search
{
"query": {
"filtered": {
"query": {
"match": {
"skillname": "mysql"
}
},
"filter": {
"type":"pattern_capture",
"patterns":["mysql([##]\\w+)"],
"preserve_original": true
}
}
}
}

Elasticsearch - behavior of regexp query against a non-analyzed field

What is the default behavior for a regexp query against a non-analyzed field? Also, is that the same answer when dealing with .raw fields?
After everything i've read, i understand the following.
1. RegExp queries will work on analyzed and non-analyzed fields.
2. A regexp query should work across the entire phrase rather than just matching on a single token in non-analyzed fields.
Here's the problem though. I can not actually get this to work. I've tried it across multiple fields.
The setup i'm working with is a stock elk install and i'm dumping pfsense and snort logs into it with a basic parser. I'm currently on Kibana 4.3 and ES 2.1
I did a query to look at the mapping for one of the fields and it indicates it is not_analyzed, yet the regex does not work across the entire field.
"description": {
"type": "string",
"norms": {
"enabled": false
},
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed",
"ignore_above": 256
}
}
}
What am i missing here?
if a field is non-analyzed, the field is only a single token.
It's same answer when dealing with .raw fields, at least in my work.
You can use groovy script:
matcher = (doc[fields.raw].value =~ /${pattern}/ );
if(matcher.matches()) {
matcher.group(matchname)}
you can pass pattern and matchname in params.
What's meaning of tried it across multiple fields.? If your situation is more complex, maybe you could make a native java plugin.
UPDATE
{
"script_fields" : {
"regexp_field" : {
"script" : "matcher = (doc[fieldname].value =~ /${pattern}/ );if(matcher.matches()) {matcher.group(matchname)}",
"params" : {
"pattern" : "your pattern",
"matchname" : "your match",
"fieldname" : "fields.raw"
}
}
}
}

Extract multiple strings using Regex jmeter

I have the following JSON response.
{ "Customer1": { "details": { "acc": { "number": "91422915166" }, "phone": { "number": "98400915180" } }, "DateofBirth": "1979-04-03", "firstName": "Harry", "lastName": "Potter" } }
Jmeter script structure:
Thread group (Get customer details)
+Regular expression extractor
.....name: customer
.....expression:"number":(.+?)"DateofBirth":"(.+?)"
.....MatchNo: -1
I want to use an extractor expression that only extracts the Phone "number". My present code is extracting both acc "number" and phone "number". Can you please tell me what expression I need to use in order to get this working? Thank you
If I understand correctly, the first number in your response is account number. You don't want that. If so
Expression : "phone": { "number": "(\d+)" }
should help.
P.S. : In your expression you also have DateofBirth in the expression. You needed only the number and not DateofBirth. If you want to extract two variables with multiple occurrences, I have tutorial exactly for that here. http://goo.gl/w3u1r