Querying an array of strings with a string that represents search terms












1















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?










share|improve this question
















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 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











  • 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
















1















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?










share|improve this question
















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 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











  • 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














1












1








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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



















  • 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











  • 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

















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










1 Answer
1






active

oldest

votes


















0














I would do something like this,




  1. 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.

  2. 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)





share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    I would do something like this,




    1. 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.

    2. 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)





    share|improve this answer






























      0














      I would do something like this,




      1. 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.

      2. 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)





      share|improve this answer




























        0












        0








        0







        I would do something like this,




        1. 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.

        2. 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)





        share|improve this answer















        I would do something like this,




        1. 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.

        2. 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)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 25 '17 at 17:05

























        answered Sep 25 '17 at 16:44









        Evan CarrollEvan Carroll

        32.5k970221




        32.5k970221






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL Server 17 - Attemping to backup to remote NAS but Access is denied

            Always On Availability groups resolving state after failover - Remote harden of transaction...

            Restoring from pg_dump with foreign key constraints