TagStudio icon indicating copy to clipboard operation
TagStudio copied to clipboard

Add search by metadata

Open lunaro-4 opened this issue 1 year ago • 3 comments

Similar issues

This feature is considered to be one of "Priority Features" by readme, yet I did not find any opened issues on that topic. Although I found a pull request #190, which could make use of parsing field names from search query.

Suggestions

I did not check contributing rules beforehand, so I accidentally started working on issue beforehand. Here is snippet from my fork:

    def parse_metadata(self, query: str | None = None) \
            -> list[dict[str, str | list[str]]]:
        """
        Splits query into several maps 'meta_key -> value'\n
        Values without specified key parsed as tags and put in 'unbound' key \n
        example query1:
            "meta_first: value; meta_second: value; tag1;\
 | meta_first: value; meta_second: value; notag;"
        example query2:
            "tag1 | notag | tag2"
        """
        if query is None:
            return {}
        meta_list: list = []
        meta_conditions = query.strip().split("|")
        for meta_condition in meta_conditions:
            meta_to_value: dict = {}
            field_data_list = meta_condition.strip().split(";")
            print(field_data_list)
            for field_data in field_data_list:
                field_parsed = field_data.strip().split(":")
                if len(field_parsed) < 2:
                    unbound_values = field_parsed[0].strip().split(' ')
                    if meta_to_value.get('unbound') is None:
                        meta_to_value['unbound'] = unbound_values
                    else:
                        meta_to_value['unbound'].append(unbound_values)
                    continue
                if len(field_parsed) != 2:
                    logging.warning("""[ERROR] Attempt to parse mutiple fields\
                                    as one! Do not forget to specify ';'\
                                    between different meta fields""")
                meta_to_value[field_parsed[0].lower()] = field_parsed[1].lower()
            meta_list.append(meta_to_value)

        logging.info("Parsed values: ",meta_list)
        return meta_list

Current state

I have tried to not touch much of code in Library.search_library, only add few new functions to allow additional entries to be filtered, but all other features should have stayed the same.

I planned to open a pull request straight up, but it is explicitly specified new issue to be opened beforehand, so let me know if is ok to go forward with this

lunaro-4 avatar Jun 10 '24 19:06 lunaro-4

Thank you for opening up an issue first, I really appreciate it!

This is definitely a much-needed feature. So if I'm understanding correctly, your approach is proposing syntax similar to the following?: title: bookname Single search for "bookname" in Title field author: authorname Single search for "authorname" in Author field title: bookname; author: authorname Combined search for for "bookname" in Title field and search for "authorname" in Author field content tags: tag1 Search for "tag1" in Content Tags field (this one may be tricky with spaces)

CyanVoxel avatar Jun 10 '24 19:06 CyanVoxel

@CyanVoxel Yes, Your understanding is mostly correct. In currents state, tags are handled through 'unbound' category, since you already have them covered and I did not want to accidentally break anything) For now, query with request would look like this: tag1 tag2 no author; title: titlename; field1: fieldcontent; So everything that does not have a word with ':' sign is processed as usual

lunaro-4 avatar Jun 10 '24 20:06 lunaro-4

@CyanVoxel Yes, Your understanding is mostly correct. In currents state, tags are handled through 'unbound' category, since you already have them covered and I did not want to accidentally break anything) For now, query with request would look like this: tag1 tag2 no author; title: titlename; field1: fieldcontent; So everything that does not have a word with ':' sign is processed as usual

Sounds good! Thank you for working on this, and let me know if you have any questions!

CyanVoxel avatar Jun 10 '24 21:06 CyanVoxel