我是Elasticsearch的新手。我希望在Java客户端中获得突出显示的字段。如果我在Windows提示符下运行以下查询:

{
    "query": {
        "filtered" : {
            "query" : {
                "term" : {
                    "title" : "western"
                }
            },
            "filter" : {
                "term" : { "year" : 1961 }
            }
        }
    },
    "highlight" : {
        fields" : {
            "title" : {}
            }
        }
}

我得到很好的突出显示的文本,如下所示:
{
      "_index" : "book",
      "_type" : "history",
      "_id" : "1",
      "_score" : 0.095891505,
      "_source":{ "title": "All Quiet on the Western great Front", "year": 1961}
      "highlight" : {
        "title" : [ "All Quiet on the <em>Western</em> great Front dead" ]
      }
}

亮点
  "highlight" : {
    "title" : [ "All Quiet on the <em>Western</em> great Front dead" ]
  }

可以轻松地转换为Java Map对象,并且“title”属性包含匹配字段的整个文本,这正是我想要的。

但是,在Java客户端中,我得到了突出显示的片段,该片段将同一字段的突出显示文本的不同段放入文本数组中。

感谢致敬。

最佳答案

在Java API中,返回的默认片段数为5。因此,如果您只想返回一个片段,则需要进行设置。

client.prepareSearch("book")
 .setTypes("history")
 .addHighlightedField("title")
 .setQuery(query)
 .setHighlighterFragmentSize(2000)
 .setHighlighterNumOfFragments(1);

关于Elasticsearch突出显示: how to get entire text of the field in Java client,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25105147/

10-11 09:17