Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG to_tsquery Exception:syntax error in tsquery: "What's your password policies?" #8920

Closed
5 tasks done
ThreeFish-AI opened this issue Sep 30, 2024 · 1 comment · Fixed by #8921
Closed
5 tasks done
Labels
🐞 bug Something isn't working

Comments

@ThreeFish-AI
Copy link
Contributor

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.8.3

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

image

In hybrid search, when you do a full-text search with keywords in PG, if there are special characters in the keywords, such as single quotes, an exception will be thrown: syntax error in tsquery: "What's your password policies?"

✔️ Expected Behavior

Fix this bug.

❌ Actual Behavior

No response

Copy link

dosubot bot commented Sep 30, 2024

Hello, @ThreeFish-AI! I'm here to help you with any bugs, questions, or contributions you may have. Let's work together to resolve any issues you're experiencing.

The issue you're encountering is due to the presence of special characters, such as single quotes, in the full-text search query, which causes a syntax error in PostgreSQL's tsquery.

To address this, you can modify the escape_query_for_search method to handle single quotes as well. Here is an updated version of the method:

@staticmethod
def escape_query_for_search(query: str) -> str:
    return query.replace("'", "''").replace('"', '\\"')

This method will escape single quotes by doubling them, which is the correct way to handle single quotes in PostgreSQL queries.

Additionally, ensure that this method is used wherever user input is passed to the full-text search functionality. For example, in the HitTestingService class:

class HitTestingService:
    @classmethod
    def retrieve(cls, dataset: Dataset, query: str, account: Account, retrieval_model: dict, limit: int = 10) -> dict:
        # ... other code ...
        all_documents = RetrievalService.retrieve(
            retrieval_method=retrieval_model.get("search_method", "semantic_search"),
            dataset_id=dataset.id,
            query=cls.escape_query_for_search(query),
            # ... other parameters ...
        )
        # ... other code ...

    @staticmethod
    def escape_query_for_search(query: str) -> str:
        return query.replace("'", "''").replace('"', '\\"')

This should prevent the syntax error when performing full-text searches with special characters like single quotes [1][2].

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant