简体   繁体   English

Elasticsearch插件和访问请求对象

[英]Elasticsearch plugins and accessing request objects

I'm new to elasticsearch and am trying to figure out to access/use elastic requests/responses in a plugin. 我是Elasticsearch的新手,正在尝试弄清楚在插件中访问/使用弹性请求/响应。 I have an action that needs to know about term statistics, so I looked up and found there is a termvector request and response object/s. 我有一个需要了解术语统计信息的操作,因此我查了一下,发现有一个termvector请求和响应对象。 In the prepareRequest method of my code I'm trying to figure out how to access it. 在我的代码的prepareRequest方法中,我试图弄清楚如何访问它。

    @Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    LOGGER.info("Handle _custom endpoint");
    GetRequest req = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
    TermVectorsRequest tvReq = new TermVectorsRequest(request.param("index"), request.param("type"), request.param("id"));
    //set to true to get total term stats
    tvReq.termStatistics(true);
    tvReq.selectedFields(request.param("field"));

    TermVectorsResponse tvResponse;
    GetResponse response;

    try {
        // Response that holds field contents, works great
        response = client.get(req).get();
        // Response that has stats, I think it needs a listener, no clue :(
        tvResponse = client.termVectors(tvReq).get();
        System.out.println("After: " + tvResponse.getFields().terms("after").getSumDocFreq());

        // Can ignore the rest of the code
        boolean fieldExists = response.getSource().containsKey(request.param("field"));
        if(fieldExists) {
            System.out.println(response.getSource().get(request.param("field")));
            return createMessageResponse(request, new SingleMessage("message", "Successfully did a thing!"));
        } else {
            return createMessageResponse(request, new SingleMessage("message", "Field \"" + request.param("field") + "\" does not exist."));
        }
    } catch (InterruptedException | ExecutionException e) {
        LOGGER.error(e);
    }

}

So I can use the getRequest object to get fields from a document which is great, but I'm having trouble accessing the termvector information. 因此,我可以使用getRequest对象从一个很棒的文档中获取字段,但是在访问termvector信息时遇到了麻烦。 The getFields() is empty and I'm getting null. getFields()为空,并且我为空。 If I access the term vectors in kibana via the endpoint I get several terms, such as 'after'. 如果我通过端点访问kibana中的术语向量,则会得到多个术语,例如“之后”。 Looking at the RestTermVectorsAction here at line 75 they use the second parameter of client.termVectors 在第75行的RestTermVectorsAction中他们使用client.termVectors的第二个参数

return channel -> client.termVectors(termVectorsRequest, new RestToXContentListener<>(channel));

My issue here is I don't want to return that as I have other calculations to do and want to create my own return message, and I'm not sure if I can access this channel object otherwise... 我的问题是我不想返回它,因为我还有其他计算要做,并且想创建自己的返回消息,而且不确定是否可以访问此通道对象...

So is this the right approach for accessing information(In this case term vectors) about an index inside an elasticsearch plugin? 那么,这是访问Elasticsearch插件内部有关索引的信息(在本例中为向量)的正确方法吗? I've seen the searchRequest used in other actions so I feel it's correct, I'm just really unfamiliar with the actionListener and what's going on with it. 我已经看到了searchRequest在其他操作中使用过,所以我觉得它是正确的,我真的不熟悉actionListener以及它发生了什么。 If it is what do I need to do to get access my term vectors? 如果要访问我的术语向量,该怎么办? I've tried returning an equivalent of line 75 and get the same output which means I need that action listener added to the client request, but I'm not sure how to get access to the RestChannel channel object to do so. 我尝试返回等价的75行并获得相同的输出,这意味着我需要将该动作侦听器添加到客户端请求中,但是我不确定如何访问RestChannel通道对象。

Also if this isn't the right approach what is? 另外,如果这不是正确的方法,那是什么?

Edit: I just realized that adding the listen makes the method void so I can't use that. 编辑:我刚刚意识到,添加监听会使该方法无效,所以我不能使用它。 I've found that using 我发现使用

tvResponse.getFields().terms("contents").getSumDocFreq()

works... which seems strange as that's the field name and not a term 起作用...这似乎很奇怪,因为这是字段名而不是术语

It looks like the problem has nothing to do with the restchannel object. 看来问题与restchannel对象无关。 My problem is for some reason the getFields().terms("term here") only has the string "contents" as a term and not anything within it. 我的问题是由于某种原因,getFields()。terms(“ term here”)仅将字符串“ contents”作为术语,而其中没有任何内容。 I'm going to make a new question regarding this. 我将对此提出一个新的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM