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

[Elasticsearch] fix: Filters not working with metadata that contain a space or capitalization #639

Merged
merged 3 commits into from
Apr 3, 2024

Conversation

julian-risch
Copy link
Member

@julian-risch julian-risch commented Apr 3, 2024

Related Issues

fixes #616

So far, metadata fields were analyzed during indexing, for example lowercased because the elasticsearch integration did not define a different mapping for them. When users tried to filter based on metadata fields by a string with uppercase letter or with whitespace, it is not automatically analyzed. Therefore the filter did not match.

Proposed Changes

  • Add dynamic template that defines any additional index fields as mapping type keyword, which means they are not analyzed. This is the desired behavior. Same behavior as in other document stores such as OpenSearchDocumentStore.
  • Explicitly defined the mapping type of the content field as text, which means it is analyzed

How did you test it?

I tested Elasticsearch and OpenSearch locally.

from haystack import Document
from haystack.components.retrievers import FilterRetriever
from haystack.document_stores.types import DuplicatePolicy
from haystack_integrations.document_stores.elasticsearch import ElasticsearchDocumentStore

docs = [
    Document(content="Python is a popular programming language",
            meta={"about": "Python", "language": "english"}),

    Document(content="python ist eine beliebte Programmiersprache",
             meta={"about": "Python", "language": "german"}),
]

document_store = ElasticsearchDocumentStore(hosts="http://localhost:9200")
document_store.write_documents(docs, policy=DuplicatePolicy.OVERWRITE)
retriever = FilterRetriever(document_store)
result = retriever.run(filters={"field": "about", "operator": "==", "value": "Python"})
print(result["documents"])  # now returns documents but previously did not
result = retriever.run(filters={"field": "about", "operator": "==", "value": "python"})
print(result["documents"]). # now does not return documents but previously did

Notes for the reviewer

  • I confirmed that the issue does not occur with the OpenSearch integration.
  • I didn't add any integration tests. If we decide we need to add them, we should add similar new tests to OpenSearch too. I wouldn't add tests. They would test Elasticsearch more than the Haystack integration.

@julian-risch julian-risch requested a review from a team as a code owner April 3, 2024 09:07
@julian-risch julian-risch requested review from masci and removed request for a team April 3, 2024 09:07
@github-actions github-actions bot added integration:elasticsearch type:documentation Improvements or additions to documentation labels Apr 3, 2024
@julian-risch julian-risch changed the title [Elasticsearch] fix: filters not working with metadata that contain a space or capitalization [Elasticsearch] fix: Filters not working with metadata that contain a space or capitalization Apr 3, 2024
Copy link
Contributor

@masci masci left a comment

Choose a reason for hiding this comment

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

Left a comment but I might be mistaken, feel free to merge

"dynamic_templates": [
{
"strings": {
"path_match": "*",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: not sure you need this line, the template will run on any string field anyways

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct. It's not needed and I removed it now. Thank you!

@julian-risch julian-risch merged commit dc590f2 into main Apr 3, 2024
12 checks passed
@julian-risch julian-risch deleted the elasticsearch-dynamic-template branch April 3, 2024 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The filters are not working with metadata that contain a space.
2 participants