Elasticsearch 具有分面、过滤和提升功能的语义搜索

Elasticsearch Semantic search with facet, filter and boost

提问人:Shahid Salim 提问时间:9/12/2023 最后编辑:Shahid Salim 更新时间:9/12/2023 访问量:49

问:

我正在根据 https://dev.to/mmoutih/semantic-search-with-openai-springboot-vaadin-elasticsearch-4lhg 教程实施,它对我有用。 我想在查询中添加分面、过滤器和提升。

我的数据对象可能如下所示

@Document(
    indexName="posts"
)
@ToString
public class Post {

    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String title;

    @Field(type = FieldType.Text)
    private String content;

    @Field(type = FieldType.Text)
    private String color;// e.g. red, blue, green

    @Field(type = FieldType.Text)
    private String size;//e.g XS,S,M,L,XL,XXL

    @Field(type = FieldType.Dense_Vector, dims = 1536, index = true)
    private Vector<Double> embedding;

    public Post(String title, String content) {
        this.title = title;
        this.content = content;
    }
}

我应该如何在当前如下所示的查询中添加它

@Repository
public interface PostRepository extends ElasticsearchRepository<Post, String>{

    @Query("{" +
            "    \"script_score\": {" +
            "       \"query\": {\"match_all\": {}}," +
            "       \"script\": {"+
            "           \"source\": \"cosineSimilarity(params.queryVector, 'embedding') +0.1\","+
            "           \"params\": {\"queryVector\": ?0}"+
            "       }"+
            "   }"+
            "}")
    List<Post> findBySimilar( String content);
}

我试过lik

public interface PostRepository extends ElasticsearchRepository<Post, String> {

    @Query("{" +
            "    \"bool\": {" +
            "        \"must\": {" +
            "            \"script_score\": {" +
            "                \"query\": {" +
            "                    \"match_all\": {}" +
            "                }," +
            "                \"script\": {" +
            "                    \"source\": \"cosineSimilarity(params.queryVector, 'embedding') + 0.1\"," +
            "                    \"params\": {" +
            "                        \"queryVector\": ?0" +
            "                    }" +
            "                }" +
            "            }" +
            "        }," +
            "        \"filter\": {" +
            "            \"term\": {" +
            "                \"color\": \"red\"" + // Replace with the color you want to filter by
            "            }" +
            "        }," +
            "        \"boost\": 1.2" +
            "    }" +
            "}")
    List<Post> findBySimilar(String content);
}

没有成功

Elasticsearch 过滤器 分面 嵌入

评论


答: 暂无答案