尝试使用CompletionSuggester进行自动完成功能,通过Java的RestHighLevelClient有不同的方法来实现完成建议程序,想了解以下方法之间(在性能/查询执行速度方面(有什么区别
1:使用CompletionSuggestionBuilder(耗时8毫秒(:
val completionSuggestionBuilder = SuggestBuilders.completionSuggestion("field_name").prefix("ban", Fuzziness.ONE).size(10)
val suggestBuilder = SuggestBuilder().addSuggestion("find_by_prefix", completionSuggestionBuilder)
val searchSourceBuilder = SearchSourceBuilder().suggest(suggestBuilder)
val searchRequest = SearchRequest("locations").source(searchSourceBuilder)
val searchResponse = restClient.search(searchRequest, RequestOptions.DEFAULT)
2:使用SimpleQueryString(耗时30毫秒(:
val query = """{"suggest":{"find_by_prefix":{"prefix":"Ban","completion":{"field":"field_name","size":10,"fuzzy":{"fuzziness":1},"contexts":{"locationType":[{"context":"AREA","boost":1},{"context":"CITY","boost":1}],"geolocation":[{"lon":77.6279354,"lat":12.9331699,"precision":"30km","boost":2}]}}}}}"""
val searchSourceBuilder = SearchSourceBuilder().query(QueryBuilders.simpleQueryStringQuery(query))
val searchRequest = SearchRequest("locations").source(searchSourceBuilder)
val searchResponse = restClient.search(searchRequest, RequestOptions.DEFAULT)
根据您的应用程序和需求,它可能会有所不同,通常取决于许多变量,例如碎片复制的数量或索引级别的索引映射等,作为构建在Apache Lucene上的地方Elastic,叶查询或复合查询通常在简单使用时具有更好的性能,但如果您需要复杂的结果,最好的方法是使用聚合。此外,如果您想在应用程序级别处理查询语法,最好的方式是使用字符串本地查询作为示例创建字符串quires,而不是使用默认的存储库,考虑下面的示例
@Query("{"bool":" +
"{"must" :" +
"[" + "" +
"{"query_string":{ "query" : "?0", "fields" : [ "parentKnowledgeGroupId" ] }}," +
"{"query_string":{ "query" : "?1", "fields" : [ "knowledgeGroupId" ]}}," +
"{"query_string":{ "query" : "?2", "fields" : [ "content" ]}}," +
"{"query_string":{ "query" : "?3", "fields" : [ "courseType" ]}}," +
"]" +
"}" +
"}")
ElasticModelSequence SearchCore(@Param("parentKnowledgeGroupId") String parentKnowledgeGroupId,
@Param("knowledgeGroupId") String knowledgeGroupId, @Param("term") String term,
@Param("courseType") ElasticModelSequence.CourseType courseType);
或者对于更简单的
@Query("{"term": {"sequenceId" : "?0"} " + "}")
ElasticModelSequence findBySequenceId(@Param("SequenceId")String SequenceId);
你只需要将它们添加到你的Elasticsearch存储库类中,它从Elasticsearch存储库(如(扩展而来
@Repository
public interface YourRepositoryName extends ElasticsearchRepository<yourmodelname, String>
对于春季案例,您可以在此处找到更多信息。