Querying an array of strings with a string that represents search terms
I have an array of values like:
array ('hello world', 'foo', 'bar') -- terms here have only letters and one space at most
and some text like: 'foo hello-world hello1world'
and I would like to get back from the original array the values foo
and hello world
filtering out bar
.
Is there any way to get that with Postgres?
postgresql array string-splitting string-searching
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have an array of values like:
array ('hello world', 'foo', 'bar') -- terms here have only letters and one space at most
and some text like: 'foo hello-world hello1world'
and I would like to get back from the original array the values foo
and hello world
filtering out bar
.
Is there any way to get that with Postgres?
postgresql array string-splitting string-searching
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
How do you expecthelloXworld
to matchhello world
there is anX
in it? How do we know what characters you permit? What abouthellosworld
?
– Evan Carroll
Sep 25 '17 at 16:18
you are right sorry :) I changed it with a number.
– Randomize
Sep 25 '17 at 16:24
That's still really weird. how come you wanthello1world
to matchhello world
– Evan Carroll
Sep 25 '17 at 16:30
If you are interested in more fuzzy matching, Pavel Stěhule has a LIKE version using arrays at postgres.cz/wiki/PostgreSQL_SQL_Tricks#LIKE_to_list_of_patterns
– bma
Sep 25 '17 at 18:37
add a comment |
I have an array of values like:
array ('hello world', 'foo', 'bar') -- terms here have only letters and one space at most
and some text like: 'foo hello-world hello1world'
and I would like to get back from the original array the values foo
and hello world
filtering out bar
.
Is there any way to get that with Postgres?
postgresql array string-splitting string-searching
I have an array of values like:
array ('hello world', 'foo', 'bar') -- terms here have only letters and one space at most
and some text like: 'foo hello-world hello1world'
and I would like to get back from the original array the values foo
and hello world
filtering out bar
.
Is there any way to get that with Postgres?
postgresql array string-splitting string-searching
postgresql array string-splitting string-searching
edited Sep 25 '17 at 17:00
Evan Carroll
32.5k970221
32.5k970221
asked Sep 25 '17 at 16:10
RandomizeRandomize
476215
476215
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 18 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
How do you expecthelloXworld
to matchhello world
there is anX
in it? How do we know what characters you permit? What abouthellosworld
?
– Evan Carroll
Sep 25 '17 at 16:18
you are right sorry :) I changed it with a number.
– Randomize
Sep 25 '17 at 16:24
That's still really weird. how come you wanthello1world
to matchhello world
– Evan Carroll
Sep 25 '17 at 16:30
If you are interested in more fuzzy matching, Pavel Stěhule has a LIKE version using arrays at postgres.cz/wiki/PostgreSQL_SQL_Tricks#LIKE_to_list_of_patterns
– bma
Sep 25 '17 at 18:37
add a comment |
How do you expecthelloXworld
to matchhello world
there is anX
in it? How do we know what characters you permit? What abouthellosworld
?
– Evan Carroll
Sep 25 '17 at 16:18
you are right sorry :) I changed it with a number.
– Randomize
Sep 25 '17 at 16:24
That's still really weird. how come you wanthello1world
to matchhello world
– Evan Carroll
Sep 25 '17 at 16:30
If you are interested in more fuzzy matching, Pavel Stěhule has a LIKE version using arrays at postgres.cz/wiki/PostgreSQL_SQL_Tricks#LIKE_to_list_of_patterns
– bma
Sep 25 '17 at 18:37
How do you expect
helloXworld
to match hello world
there is an X
in it? How do we know what characters you permit? What about hellosworld
?– Evan Carroll
Sep 25 '17 at 16:18
How do you expect
helloXworld
to match hello world
there is an X
in it? How do we know what characters you permit? What about hellosworld
?– Evan Carroll
Sep 25 '17 at 16:18
you are right sorry :) I changed it with a number.
– Randomize
Sep 25 '17 at 16:24
you are right sorry :) I changed it with a number.
– Randomize
Sep 25 '17 at 16:24
That's still really weird. how come you want
hello1world
to match hello world
– Evan Carroll
Sep 25 '17 at 16:30
That's still really weird. how come you want
hello1world
to match hello world
– Evan Carroll
Sep 25 '17 at 16:30
If you are interested in more fuzzy matching, Pavel Stěhule has a LIKE version using arrays at postgres.cz/wiki/PostgreSQL_SQL_Tricks#LIKE_to_list_of_patterns
– bma
Sep 25 '17 at 18:37
If you are interested in more fuzzy matching, Pavel Stěhule has a LIKE version using arrays at postgres.cz/wiki/PostgreSQL_SQL_Tricks#LIKE_to_list_of_patterns
– bma
Sep 25 '17 at 18:37
add a comment |
1 Answer
1
active
oldest
votes
I would do something like this,
- create a helper function to generate your query terms from the text-string input. You could inline this into the function, but it's ugly and silly and if you need it here then you probably need it elsewhere.
- Write a query with
NOT word LIKE ANY()
Here is a demo of the helper function,
CREATE OR REPLACE FUNCTION my_stupid_query(q text)
RETURNS text AS $$
SELECT ARRAY(
SELECT regexp_replace(t, '[^[:alpha:]]', '_', 'g')
FROM unnest(string_to_array(q, ' ')) AS q(t)
);
$$ LANGUAGE sql
IMMUTABLE;
Then you can use NOT word LIKE ANY()
. This shows what it would look like,
SELECT word
FROM (VALUES (ARRAY['hello world', 'foo', 'bar'])) AS t1(x)
CROSS JOIN LATERAL unnest(x) AS t2(word)
WHERE NOT word LIKE ANY(my_stupid_query('foo hello-world helloXworld'));
word
------
bar
(1 row)
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f186835%2fquerying-an-array-of-strings-with-a-string-that-represents-search-terms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would do something like this,
- create a helper function to generate your query terms from the text-string input. You could inline this into the function, but it's ugly and silly and if you need it here then you probably need it elsewhere.
- Write a query with
NOT word LIKE ANY()
Here is a demo of the helper function,
CREATE OR REPLACE FUNCTION my_stupid_query(q text)
RETURNS text AS $$
SELECT ARRAY(
SELECT regexp_replace(t, '[^[:alpha:]]', '_', 'g')
FROM unnest(string_to_array(q, ' ')) AS q(t)
);
$$ LANGUAGE sql
IMMUTABLE;
Then you can use NOT word LIKE ANY()
. This shows what it would look like,
SELECT word
FROM (VALUES (ARRAY['hello world', 'foo', 'bar'])) AS t1(x)
CROSS JOIN LATERAL unnest(x) AS t2(word)
WHERE NOT word LIKE ANY(my_stupid_query('foo hello-world helloXworld'));
word
------
bar
(1 row)
add a comment |
I would do something like this,
- create a helper function to generate your query terms from the text-string input. You could inline this into the function, but it's ugly and silly and if you need it here then you probably need it elsewhere.
- Write a query with
NOT word LIKE ANY()
Here is a demo of the helper function,
CREATE OR REPLACE FUNCTION my_stupid_query(q text)
RETURNS text AS $$
SELECT ARRAY(
SELECT regexp_replace(t, '[^[:alpha:]]', '_', 'g')
FROM unnest(string_to_array(q, ' ')) AS q(t)
);
$$ LANGUAGE sql
IMMUTABLE;
Then you can use NOT word LIKE ANY()
. This shows what it would look like,
SELECT word
FROM (VALUES (ARRAY['hello world', 'foo', 'bar'])) AS t1(x)
CROSS JOIN LATERAL unnest(x) AS t2(word)
WHERE NOT word LIKE ANY(my_stupid_query('foo hello-world helloXworld'));
word
------
bar
(1 row)
add a comment |
I would do something like this,
- create a helper function to generate your query terms from the text-string input. You could inline this into the function, but it's ugly and silly and if you need it here then you probably need it elsewhere.
- Write a query with
NOT word LIKE ANY()
Here is a demo of the helper function,
CREATE OR REPLACE FUNCTION my_stupid_query(q text)
RETURNS text AS $$
SELECT ARRAY(
SELECT regexp_replace(t, '[^[:alpha:]]', '_', 'g')
FROM unnest(string_to_array(q, ' ')) AS q(t)
);
$$ LANGUAGE sql
IMMUTABLE;
Then you can use NOT word LIKE ANY()
. This shows what it would look like,
SELECT word
FROM (VALUES (ARRAY['hello world', 'foo', 'bar'])) AS t1(x)
CROSS JOIN LATERAL unnest(x) AS t2(word)
WHERE NOT word LIKE ANY(my_stupid_query('foo hello-world helloXworld'));
word
------
bar
(1 row)
I would do something like this,
- create a helper function to generate your query terms from the text-string input. You could inline this into the function, but it's ugly and silly and if you need it here then you probably need it elsewhere.
- Write a query with
NOT word LIKE ANY()
Here is a demo of the helper function,
CREATE OR REPLACE FUNCTION my_stupid_query(q text)
RETURNS text AS $$
SELECT ARRAY(
SELECT regexp_replace(t, '[^[:alpha:]]', '_', 'g')
FROM unnest(string_to_array(q, ' ')) AS q(t)
);
$$ LANGUAGE sql
IMMUTABLE;
Then you can use NOT word LIKE ANY()
. This shows what it would look like,
SELECT word
FROM (VALUES (ARRAY['hello world', 'foo', 'bar'])) AS t1(x)
CROSS JOIN LATERAL unnest(x) AS t2(word)
WHERE NOT word LIKE ANY(my_stupid_query('foo hello-world helloXworld'));
word
------
bar
(1 row)
edited Sep 25 '17 at 17:05
answered Sep 25 '17 at 16:44
Evan CarrollEvan Carroll
32.5k970221
32.5k970221
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f186835%2fquerying-an-array-of-strings-with-a-string-that-represents-search-terms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you expect
helloXworld
to matchhello world
there is anX
in it? How do we know what characters you permit? What abouthellosworld
?– Evan Carroll
Sep 25 '17 at 16:18
you are right sorry :) I changed it with a number.
– Randomize
Sep 25 '17 at 16:24
That's still really weird. how come you want
hello1world
to matchhello world
– Evan Carroll
Sep 25 '17 at 16:30
If you are interested in more fuzzy matching, Pavel Stěhule has a LIKE version using arrays at postgres.cz/wiki/PostgreSQL_SQL_Tricks#LIKE_to_list_of_patterns
– bma
Sep 25 '17 at 18:37