Skip to content

Allow extra searchable metadata in package - #351

Merged
ericmj merged 1 commit into
hexpm:masterfrom
mobileoverlord:meta_search
Jun 16, 2016
Merged

ericmj merged 1 commit into
hexpm:masterfrom
mobileoverlord:meta_search

Conversation

@mobileoverlord

Copy link
Copy Markdown
Contributor

WIP

Added fields and tests to have package_metadata contain extra map.

Todo:
extend search API to pass object path and search words

Tracked alongside
hexpm/hex#234
hexpm/specifications#6

@mobileoverlord

Copy link
Copy Markdown
Contributor Author

How should we handle bringing in the ability to match on extra path

Lets say we have several packages with extra package metadata defined like this:
%{"extra" => %{"foo" => "bar"}}

How to search for nerves packages where the key foo in extra = bar
https://hex.pm/packages?_utf8=%E2%9C%93&search=nerves&sort=downloads&extra:foo=bar

@ericmj

ericmj commented May 31, 2016

Copy link
Copy Markdown
Member

execute """
CREATE INDEX packages_extra_text ON
packages USING GIN (to_tsvector('english', regexp_replace((meta->'extra')::text, '/', ' ')))
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work if you want to specify paths so we can't use full text search unfortunately. This code is encoding the json as text so you will have a string with "{...}".

If we wan't full text search we need to concatenate all text inside the nested json values. I think exact value search would be better.

@mobileoverlord

Copy link
Copy Markdown
Contributor Author

I started putting in some test information.
{"id": "12345", "extra": {"foo": {"bar": "baz"}, "key": "value", "list": ["a", "b", "c"]}, "links": {"Github": "http://example.com/github"}, "licenses": ["Apache 2.0"], "description": "description", "maintainers": ["Justin Schneck", "Frank Hunleth"]}

Should we only support nested paths, or can we also support nested lists

@mobileoverlord

mobileoverlord commented May 31, 2016 •

Copy link
Copy Markdown
Contributor Author

nested paths are pretty simple

SELECT p0 FROM "packages" AS p0 
WHERE p0.meta #>> '{extra, key}'= 'value'

I was thinking we could split the valiue of the extra path param with :
?extra_path=foo:bar&extra_value=baz

do we or can we support nested lists. In the example above if trying to select form a list
?extra_path=list&extra_value=a
could return results where a in list

@ericmj

ericmj commented May 31, 2016

Copy link
Copy Markdown
Member

I've been looking more at JSON value indexing at https://www.postgresql.org/docs/9.5/static/datatype-json.html. It seems we can only query using the index on top level values, but we can query on array elements. If we want to query on nested elements we need to flatten the JSON object inside the index expression.

I was thinking we could split the valiue of the extra path param with :
?extra_path=foo:bar&extra_value=baz

This assumes that the keys cannot contain :. That's fair but would need to be documented in the specification and rejected in validation.

do we or can we support nested lists. In the example above if trying to select form a list
?extra_path=list&extra_value=a
could return results where a in list

We can support that on the first "level" of a list but nesting the path inside lists would be tricky so I don't think we should allow that.

@ericmj

ericmj commented May 31, 2016

Copy link
Copy Markdown
Member

We can query on one level of nesting if the nested element is an array as shown in the example from the documentation:

{
    "guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a",
    "name": "Angela Barton",
    "is_active": true,
    "company": "Magnafone",
    "address": "178 Howard Place, Gulf, Washington, 702",
    "registered": "2009-11-07T08:53:22 +08:00",
    "latitude": 19.793713,
    "longitude": 86.513373,
    "tags": [
        "enim",
        "aliquip",
        "qui"
    ]
}

-- Find documents in which the key "tags" contains key or array element "qui"
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc -> 'tags' ? 'qui';

@mobileoverlord

Copy link
Copy Markdown
Contributor Author

That syntax works really well for both the value being a list or a value. I think that the strategy is:

nested maps
%{"foo" => %{"bar" => "baz"}}

nested lists
%{"foo" => ["bar", "baz"]}

not maps in lists
%{"foo" => [%{"bar" => "baz"}]}

As per the params

Is it safe to enum params and find the one with the key that starts_with? "extra:" and then break the path off the remainder of the key. This would let us do
?extra:foo:bar=baz

@ericmj

ericmj commented May 31, 2016

Copy link
Copy Markdown
Member

nested maps

Keep in mind that this would require us to flatten json object in the index expression (if I understood the postgres docs correctly).

Is it safe to enum params and find the one with the key that starts_with? "extra:" and then break the path off the remainder of the key. This would let us do ?extra:foo:bar=baz

It would be possible, but with ?extra_path[]=... that would not be needed since phoenix/plug will convert that to a list for us.

@mobileoverlord

Copy link
Copy Markdown
Contributor Author

Im getting caught up in trying to compose search in terms of a text fragments in to a query. After a talk with Eric, we determined that it would make more sense to incorporate searching on extra into the standard search behavior. For example

Current Search
nerves_pkg
searches:
name OR description

New Search
searching extra metadata values is exact and therefore should be joined with AND. The proposition is to allow expression in the search value.

name:nerves_pkg OR description:\"nerves_pkg\"

backwards compatibility is achieved by converting a string with no keywords ("name", "description", "extra") into the above example.

The problems start to arise when introducing extra

name:nerves_pkg OR description:\"nerves_pkg\" AND extra:"{\"a\" => \"b\"}"

When parsed, the AND takes precedence so description AND extra... OR name. To get around this we would have to introduce ()

(name:nerves_pkg OR description:\"nerves_pkg\") AND extra:"{\"a\" => \"b\"}"

This is where things start to feel like they are getting out of hand. Composing where/3 concats with AND not OR, also, how do we dynamically add the () for grouping?

My main use case for this feature is to allow filtering search results so we can list all packages where name like 'nerves_pkg_%' and extra:{"nerves": {"binaries": [...]}} contains "this_binary". Offering everything through an expressive search is feeling daunting. Wondering if anyone has an recommendations or different angles on the best implementation for everyone.

@mobileoverlord

mobileoverlord commented Jun 2, 2016 •

Copy link
Copy Markdown
Contributor Author

for updated notes, we also ran experiments on how to execute using a single operand for jsonb. These produce viable results

SELECT '{"a": {"b":[1,2], "d": 1}, "c": [1, 2]}'::jsonb @> '{"a": {"d": 1}}'::jsonb;
SELECT '{"a": {"b":[1,2], "d": 1}, "c": [1, 2]}'::jsonb @> '{"a": {"b": [2]}}'::jsonb;

@ericmj

ericmj commented Jun 2, 2016

Copy link
Copy Markdown
Member

You can make it always AND as long as we keep the current behaviour of ORing name and description when doing the "standard" search.

@mobileoverlord

Copy link
Copy Markdown
Contributor Author

Ready for another pass

@mobileoverlord

Copy link
Copy Markdown
Contributor Author

I changed the search semantics a little for name:v to expect the wildcards to be passed in as part of the name:nerves%

rebased and resolved conflicts

def index(conn, params) do
page = HexWeb.Utils.safe_int(params["page"])
search = HexWeb.Utils.safe_search(params["search"])
search = params["search"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need the conversion to nil for "". Maybe add a new function to Package that only does the String.replace?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants