使用spring-data @Query的自动建议

时间:2018-12-23 18:07:07

标签: autocomplete spring-data-elasticsearch

使用@Query的spring-data-elasticsearch无法完成自动补全

版本详细信息: Elasticsearc:6.3.0,spring-data-elasticsearch:3.0.5.RELEASE和spring-boot 2.x.release

如下所示在ES 6.3.0中创建索引

{
  "settings": {
    "index": {
      "analysis": {
        "filter": {},
        "analyzer": {
          "keyword_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding",
              "trim"
            ],
            "char_filter": [],
            "type": "custom",
            "tokenizer": "keyword"
          },
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "edge_ngram_search_analyzer": {
            "tokenizer": "lowercase"
          }
        },
        "tokenizer": {
          "edge_ngram_tokenizer": {
            "type": "edge_ngram",
            "min_gram": 2,
            "max_gram": 5,
            "token_chars": [
              "letter"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "sometype": {
      "properties": {
        "rs": {
          "type": "text",
          "fields": {
            "keywordstring": {
              "type": "text",
              "analyzer": "keyword_analyzer"
            },
            "edgengram": {
              "type": "text",
              "analyzer": "edge_ngram_analyzer",
              "search_analyzer": "edge_ngram_search_analyzer"
            },
            "completion": {
              "type": "completion"
            }
          },
          "analyzer": "standard"
        },
        "tl1": {
            "type": "text"
        },
        "tl2": {
          "type": "text"
        },
        "tl3": {
          "type": "text"
        },
        "tl4": {
          "type": "text"
        }
      }
    }
  }
}

下面是实体代码,

@Document(indexName = "someindex", type = "sometype")
public class ContentEntity {

@CompletionField()
private String rs;

private String tl2;

private String tl3;

private String tl4;

@Id
private String tl1;

以下是存储库类中的自定义查询

@Repository
public interface ContentRepository extends ElasticsearchRepository<ContentEntity, String> {
    @Query("{ \"suggest\": { \"content-suggest\" : { \"prefix\" : \"?0\", \"completion\" : { \"field\" : \"rs.completion\" }}}}")
List<ContentEntity> getSuggestions(String ts);

}

当我运行API进行自动建议时,遇到错误

org.elasticsearch.common.ParsingException: no [query] registered for [suggest]
    at org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder    (AbstractQueryBuilder.java:322) ~[elasticsearch-5.6.8.jar:5.6.8]
    at org.elasticsearch.index.query.WrapperQueryBuilder.doRewrite(WrapperQueryBuilder.java:165) ~[elasticsearch-5.6.8.jar:5.6.8]
    at org.elasticsearch.index.query.AbstractQueryBuilder.rewrite(AbstractQueryBuilder.java:269) ~[elasticsearch-5.6.8.jar:5.6.8]
    at org.elasticsearch.search.builder.SearchSourceBuilder.rewrite(SearchSourceBuilder.java:888) ~[elasticsearch-5.6.8.jar:5.6.8]
    at org.elasticsearch.search.builder.SearchSourceBuilder.rewrite(SearchSourceBuilder.java:79) ~[elasticsearch-5.6.8.jar:5.6.8]

提供的代码库@ https://github.com/VenkataMR/ElasticsearchWithAutosuggestion

代码有问题吗?请建议。

0 个答案:

没有答案