Elasticsearch/Lucene highlight

依然范特西╮ 提交于 2020-01-23 03:31:06

问题


How to highlight result query with fuzzyLikeThisFieldQuery in elasticsearch? I can pick up on fuzzyQuery but not fuzzyLikeThisFieldQuery. For example, in the code below i used fuzzyQuery:

QueryBuilder allquery = QueryBuilders.fuzzyQuery("name", "fooobar").minSimilarity(0.4f);

SearchRequestBuilder builder = ds.getElasticClient()
                                        .prepareSearch("data")
                                        .setQuery(allquery)
                                        .setFrom(0)
                                        .setSize(10)
                                        .setTypes("entity")
                                        .setSearchType(SearchType.DEFAULT)
                                        .addHighlightedField("name")
                                        .addField("name");

    SearchResponse sr = builder.execute().actionGet();

the result is

If you want to have a <em>foobar</em> for oracle

But if i use fuzzyLikeThisFieldQuery, didn't highlight

QueryBuilder allquery = QueryBuilders.fuzzyLikeThisFieldQuery("name").likeText("fooobar").minSimilarity(0.4f);

the result is

If you want to have a foobar for oracle

Anyone know why?


回答1:


You need to call these two functions to set the highlighter tags..

builder.setHighlighterPreTags("<pre>").setHighlighterPostTags("</pre>");



回答2:


I need to highlight the keyword and use the method I've written below works fine for me:

searchRequest.setQuery(
       QueryBuilders.queryString(q))
       .addHighlightedField("title")
       .addHighlightedField("text")
       .setHighlighterPreTags("<em>")
       .setHighlighterPostTags("</em>");
 _searchResponse = searchRequest.execute().actionGet();

I use Gson to parse response string as a json object and cast to my entity like below:

root = new JsonParser().parse(_searchResponse.toString());
p.results.add(root.getAsJsonObject().get("hits").getAsJsonObject().get("hits"));

You will get such a response like this:

    content: {
results: [
[
{
_index: "news",
_type: "news",
_id: "111",
_score: 0.6056677,
_source: {
id: "1349298458",
title: "Title text",
text: "Detail text"
},
highlight: {
text: [
" some text <em>keyword</em> some text <em>keyword</em>- some text <em>keyword</em> some text."
]
}
},...

Wish you to get how it works and try it yourself.



来源:https://stackoverflow.com/questions/11424927/elasticsearch-lucene-highlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!