Elasticsearch 稀疏向量搜索示例(qbit)
前言
试验
PUT /tech_docs
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"tags": {
"type": "keyword"
},
"views": {
"type": "integer"
},
"vector_data": {
"type": "sparse_vector"
}
}
}
}POST /tech_docs/_bulk
{ "index": { "_id": "1" } }
{ "title": "Introduction to Python", "tags": ["programming"], "views": 150, "vector_data": {"101": 2.5, "202": 1.2} }
{ "index": { "_id": "2" } }
{ "title": "Advanced Java Patterns", "tags": ["programming"], "views": 50, "vector_data": {"303": 3.1, "404": 0.8} }
{ "index": { "_id": "3" } }
{ "title": "Elasticsearch Sparse Vector Guide", "tags": ["search"], "views": 300, "vector_data": {"101": 0.5, "505": 2.9} }
{ "index": { "_id": "4" } }
{ "title": "Distributed Systems Basics", "tags": ["architecture"], "views": 200, "vector_data": {"606": 1.8, "707": 2.2} }
{ "index": { "_id": "5" } }
{ "title": "Machine Learning with PyTorch", "tags": ["ai"], "views": 80, "vector_data": {"101": 1.1, "808": 3.5} }GET /tech_docs/_search
{
"query": {
"bool": {
"must": [
{
"sparse_vector": {
"field": "vector_data",
"query_vector": {
"101": 1.0,
"505": 2.0
}
}
}
],
"filter": [
{ "terms": { "tags": ["search", "programming"] } },
{ "range": { "views": { "gte": 100 } } }
]
}
}
}相关阅读