首页 > 解决方案 > ElasticSearch rescorer 插件。如何用原始分数解析内部命中

问题描述

我创建一个对 ES 的查询:

GET my-index/_search
{
      "query": {
        "nested": {
          "inner_hits": {},
          "score_mode": "max",
          "path": "my_nested_field",
          "query": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "my_nested_field.value.token_analyzed": {
                            "query": "Looking for something like this"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      "rescore": {
        "my_plugin_name": {
        }
      }
    }

索引中的文档类似于:

{
    "some_field": "some_value",
    "some_other_field": "some_other_value",
    "my_nested_field": [
        {
            "value": "some nested value",
            "something_else": "something else"
        },
        {
            "value": "some nested value 2",
            "something_else": "something else 2"
        }
    ]
    ]
}

我的自定义 rescorer 插件已执行,一切都很好。我想优化我的插件。目前,当我点击某个文档时,我会使用其中my_nested_field的每个元素来重新评分顶级文档。我只想使用那些实际导致点击的那些来重新评分顶级文档。但是我不知道如何过滤掉那些没有导致插件命中的。

我当前的代码:

public TopDocs rescore(TopDocs topDocs, IndexSearcher searcher, RescoreContext rescoreContext) throws IOException {
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
        Document document = searcher.doc(topDocs.scoreDocs[i].doc);
        String json = parserSource(document);
    }
  ...

private String parseSource(Document document) {
  return new String(document.getField("_source").binaryValue().bytes, StandardCharsets.UTF_8);
}

我正在寻找的东西不在 path_source中,但我可以像这样解析的唯一东西是_sourceand _id。我希望这是因为您只能解析存储的字段。但是肯定有某种方法可以解析内部点击得分结果吗?

在每个文档源旁边的实际 ES 响应中都有这个(但我不知道如何在插件中解析这些东西):

"inner_hits": {
    "my_nested_field": {
        "hits": {
            "total": {
                "value": 1,
                "relation": "eq"
            },
            "max_score": 4.2184687,
            "hits": [ // I NEED THIS STUFF NOT THE _source
                {
                    "_index": "my-index",
                    "_type": "_doc",
                    "_id": "8b3d929a-8e90-4ce7-aa1e-7f11ec16de1e",
                    "_nested": {
                        "field": "my_nested_field",
                        "offset": 2
                    },
                    "_score": 4.2184687,
                    "_source": {
                        "value": "Some value which was actually hit",
                    }
                }
            ]
        }
    }
}

旁注:进行查询后,我需要完整的文档,而不仅仅是嵌套字段。

标签: elasticsearchelasticsearch-plugin

解决方案


推荐阅读