Weaviate Python客户端调用nearText查询报错,求解决方案
问题与解决方法
问题描述
使用Python客户端测试Weaviate葡萄酒评论示例时,执行以下查询出现错误:
查询代码:
query_result = ( client.query .get("Wine",["title","description"]) .with_limit(5) .with_near_text({"concepts" : ["white"]}) .do())
错误响应:
{'errors': [{'locations': [{'column': 20, 'line': 1}], 'message': 'Unknown argument "nearText" on field "Wine" of type "GetObjectsObj". Did you mean "nearVector" or "nearObject"?', 'path': None}]}
空查询可正常返回结果,当前Wine类的Schema如下:
{"classes": [{"class": "Wine","invertedIndexConfig": {"bm25": {"b": 0.75,"k1": 1.2},"cleanupIntervalSeconds": 60,"stopwords": {"additions": null,"preset": "en","removals": null}},"properties": [{"dataType": ["text"],"name": "title","tokenization": "word"},{"dataType": ["text"],"name": "description","tokenization": "word"}],"replicationConfig": {"factor": 1},"shardingConfig": {"virtualPerPhysical": 128,"desiredCount": 1,"actualCount": 1,"desiredVirtualCount": 128,"actualVirtualCount": 128,"key": "_id","strategy": "hash","function": "murmur3"},"vectorIndexConfig": {"skip": false,"cleanupIntervalSeconds": 300,"maxConnections": 64,"efConstruction": 128,"ef": -1,"dynamicEfMin": 100,"dynamicEfMax": 500,"dynamicEfFactor": 8,"vectorCacheMaxObjects": 1000000000000,"flatSearchCutoff": 40000,"distance": "cosine"},"vectorIndexType": "hnsw","vectorizer": "none"}]}
预期返回与“white”相关的葡萄酒评论结果,需解决上述错误。
错误原因
从Schema可以看出,Wine类的vectorizer被设置为none,意味着Weaviate不会自动为该类的文本属性生成向量。而nearText查询依赖文本的向量表示,要求类必须配置文本向量器(如text2vec-openai、text2vec-cohere等),因此触发错误提示。
解决方案
方案1:配置文本向量器以使用nearText查询
先更新Wine类的Schema,指定一个文本向量器(以OpenAI向量器为例,需提前在Weaviate中配置好OpenAI API密钥):
# 更新Schema示例代码 client.schema.update_config( class_name="Wine", config={ "vectorizer": "text2vec-openai", "vectorIndexType": "hnsw" } )
更新完成后,需重新导入数据(或触发Weaviate为现有数据重新生成向量),之后即可正常使用原有的nearText查询。
方案2:改用BM25关键词搜索
若不想配置向量器,可利用已配置好的BM25倒排索引进行关键词搜索,匹配包含“white”的内容:
query_result = ( client.query .get("Wine", ["title", "description"]) .with_limit(5) .with_bm25( query="white", properties=["title", "description"] # 指定搜索的属性字段 ) .do() )
这种方式无需向量支持,直接基于文本关键词匹配,可快速获取相关结果。
内容的提问来源于stack exchange,提问作者Dan Porter
相关产品推荐
相关产品推荐

