So i'm working a little search tool for a website but I ran into an little issue, when I enter a word and if this word is present it will return the results, but when i search on this word and add a extra character to it wil not return any results.
When I search on the word 'tool' it will return the results as this word is present in the DB, but when I search on 'tools' it will not return anything, as tools is not present in the DB.
word present in DB = 'tool'
search in DB with word 'tool' = returns results
search in DB with word 'tools' = returns noting as tools is not present
When I search on the word 'tools' it should also return any results that matches with 'tool'
$query->andFilterWhere([
'or',
['like', 'product.name', $this->searchValue],
['like', 'product.desc', $this->searchValue],
]);
You could run the search string through the singularize inflector.
$value = yii\helpers\Inflector::singularize($this->searchValue);
$query->andFilterWhere([
'or',
['like', 'product.name', $value],
['like', 'product.desc', $value],
]);
Obviously this will only work for single word plurals.
Ed.
You can search using below query :
$query->andFilterWhere(
"product.name LIKE CONCAT('%',$this->searchValue,'%') OR
product.desc LIKE CONCAT('%',$this->searchValue,'%')"
);
Related
Help with filter function!
I have a table with phone number and name as columns.
The JSON object would look something like this :
Details = [
{PN : '123-456-7890',
NAME : 'PERSON A',
},{
PN: '192-453-7655',
NAME: 'PERSON B',
}
]
I need to search on keyup and filter the data. The user can search in any of these patterns:
xxxxxxxxxx,
xxx-xxx-xxxx,
xxx-xxxxxxx,
xxxxxx-xxxx.
and still needs to get the data having xxx-xxx-xxxx as phone number in the table. Search should start as soon as the keyup triggers.
I tried using match and test methods, but couldn't make connection between search input, regex and the elements from table.
Can anyone suggest how I can tackle this situation?
Please use this regex -
/^\d{3}-\d{3}-\d{4}$|^\d{3}-\d{7}$|^\d{6}-\d{4}|^\d{10}$/
Angular Code -
matchNumber(){
var str = "12311111455";
var res = str.match(/^\d{3}-\d{3}-\d{4}$|^\d{3}-\d{7}$|^\d{6}-\d{4}|^\d{10}$/);
if(res.length > 0){
console.log('Matched);
}}
Valid Numbers
1234567890
123-456-7890
123-4567890
123456-7890
Invalid Numbers
123453647564756876
111223-234234
Check DEMO -
Check Valid Numbers
Code
//text to search
$details = "Successfully";
ActivityLog::with('getCauserDetails')
->when($details ?? false, function ($q) use ($details) {
$q->whereJsonContains('properties->activity', $details);
})
->get()
->toArray();
Table Structure
id - int
name - varchar
properties - json
user_id - int
Json Data
{
"ip":"192.168.0.1",
"platform":"Windows",
"activity":"Successfully logout"
}
{
"ip":"192.168.0.1",
"device":"WebKit",
"browser":"Chrome",
"platform":"Windows",
"activity":"Successfully logged in"
}
Question: Above code have been successfully to search the value of data inside the JSON data but need to search into full sentence. For example, "Successfully logout", if I search with "Successfully" sentence, it's will not filter the data. Does anyone know how to filter it's with the SQL LIKE Operator inside the JsonContains, mean that if I filter with sentence "Successfully", it's will also return the data instead of full sentence.
I didn't find any appropriate Laravel method but your query in SQL would be like this. It seems you don't have other choice except using raw query:
SELECT *
FROM table_name
WHERE JSON_EXTRACT(properties, "$.activity") LIKE '%Successfully logout%'
I have 2 objects, Visitors and Events. Visitors have multiple Events. An event stores parameters like this...
#<Event id: 5466, event_type: "Visit", visitor_token: "c26a6098-64bb-4652-9aa0-e41c214f42cb", contact_id: 657, data: {"url"=>"http://widget.powerpress.co/", "title"=>"Home (light) | Widget"}, created_at: "2015-12-17 14:51:53", updated_at: "2015-12-17 14:51:53", website_id: 2>
As you can see, there is a serialized text column called data that stores a hash with more data.
I need to find out if a visitor has visited a certain page, which would be very simple if the url parameter were it's own column, or if the hash were an hstore column, however it wasn't originally set up that way and it's a part of the saved hash.
Here's my attempted rails queries...
visitor.events.where("data -> url = :value", value: 'http://widget.powerpress.co/')
visitor.events.where("data like ?", "{'url' => 'http://widget.powerpress.co/'}")
visitor.events.where("data -> :key LIKE :value", :key => 'url', :value => "%http://widget.powerpress.co/%")
How does one properly query postgres to find objects that have a hash that contains a key with a specific value?
I suspect you're not looking for the right string. It should be "url"=>"http://widget.powerpress.co/", so:
visitor.events.where("data like ?", '%"url"=>"http://widget.powerpress.co/"%')
Check the right value directly in DB.
If you are storing hash in a text column, try following:
visitor.events.select{|ve| eval(ve.data)["url"] == "http://widget.powerpress.co/"}
Hope, it helps!
It worked for me.
visitor.events.select { |n| n.data && n.data['url'] == "http://widget.powerpress.co/"}
I want to import many informations from a CSV file to Elastic Search.
My issue is I don't how can I use a equivalent of substring to select information into a CSV column.
In my case I have a field date (YYYYMMDD) and I want to have (YYYY-MM-DD).
I use filter, mutate, gsub like:
filter
{
mutate
{
gsub => ["date", "[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]", "[0123456789][0123456789][0123456789][0123456789]-[0123456789][0123456789]-[0123456789][0123456789]"]
}
}
But my result is false.
I can indentified my string but I don't how can I extract part of this.
My target it's to have something like:
gsub => ["date", "[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]","%{date}(0..3}-%{date}(4..5)-%{date}"(6..7)]
%{date}(0..3} : select from the first to the 4 characters of csv columns date
You can use ruby plugin to do conversion. As you say, you will have a date field. So, we can use it directly in ruby
filter {
ruby {
code => "
date = Time.strptime(event['date'],'%Y%m%d')
event['date_new'] = date.strftime('%Y-%m-%d')
"
}
}
The date_new field is the format you want.
First, you can use a regexp range to match a sequence, so rather than [0123456789], you can do [0-9]. If you know there will be 4 numbers, you can do [0-9]{4}.
Second, you want to "capture" parts of your input string and reorder them in the output. For that, you need capture groups:
([0-9]{4})([0-9]{2})([0-9]{2})
where parens define the groups. Then you can reference those on the right side of your gsub:
\1-\2-\3
\1 is the first capture group, etc.
You might also consider getting these three fields when you do the grok{}, and then putting them together again later (perhaps with add_field).
I am trying to devise a MySQL regex that would match—or not match—against dates entered in text fields like this:
1990/93
1988/93
1969/70
1990/1996
1984/2007
1975/2008
1973/2011
2003/10
The goal is to match a text field—which contains freeform, human entered dates—based on where the date formats seen above show up. The only pitiful thing I have gotten to work is a simple regex that checks for the slash like this:
SELECT *
FROM `some_objects`
WHERE `date_text` REGEXP '[\/]'
;
Which is fairly useless to me since that would just be the equivalent of LIKE '%/%':
SELECT *
FROM `some_objects`
WHERE `date_text` LIKE '%/%'
;
And I am not liking LIKE since it just grabs any record that has a date_text field that has a slash in it.
What I am looking for is something more robust that works like this:
If you find a 2-4 digit number.
And number is followed by a /.
And that / is followed by another 2-4 digit number.
Then success!
FWIW, I have been able to code some regex in PHP which works well using lookarounds. So I am posting it as an example. I understand MySQL regex functionality is not as robust as in PHP, but is there some way for me to implement the same—or as close as possible—regex logic in MySQL as I am doing in this PHP script?
// Test string including items that should not be matched.
$string = <<<EOT
1990/93
1988/93
1969/70
1990/1996
1984/2007
1975/2008
1973/2011
2003/10
2003/10
not/this
not/10this
/10never
EOT;
// One test.
$regex_pattern = '/[0-9]{4}(?:\/)[0-9]{2,4}/is';
// Another test.
$regex_pattern = '/(?<=[0-9]{4})\/(?=[0-9]{2,4})/is';
// Run it through 'preg_match_all'.
preg_match_all($regex_pattern, $string, $matches);
// Echo the results.
echo '<pre>';
print_r($matches);
echo '</pre>';
The output of that would be this. Which is fine. I do not need values. But I do want to match items with that patten in MySQL for retrieval of other fields.
Array
(
[0] => Array
(
[0] => /
[1] => /
[2] => /
[3] => /
[4] => /
[5] => /
[6] => /
[7] => /
[8] => /
)
)
It seems that this page may tell you what you want to know:
In this case, the expression would be something like
[0-9]{2,4}/[0-9]{2,4}