ElasticSearch:使用匹配的搜索词标记文档 [英] ElasticSearch: Labelling documents with matching search term

查看:50
本文介绍了ElasticSearch:使用匹配的搜索词标记文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Elasticsearch 1.7,并且需要一种使用文档中他们匹配的query_string查询的哪一部分来标记文档的方法.

I'm using elasticsearch 1.7 and am in need of a way to label documents with what part of a query_string query they match.

我一直在尝试突出显示,但是发现在某些情况下它有点混乱.我希望用匹配的搜索词来标记文档.

I've been experimenting with highlighting, but found that it gets a bit messy with some cases. I'd love to have the document tagged with matching search terms.

这是我正在使用的查询:(请注意,这是一个后来被编码为JSON的ruby哈希)

Here is the query that I'm using: ( note this is a ruby hash that later gets encoded to JSON )

{
  query: {
    query_string: {
      fields: ["title^10", "keywords^4", "content"],
      query: query_string,
      use_dis_max: false
    }
  },
  size: 20,
  from: 0,
  sort: [
    { pub_date: { order: :desc }},
    { _score:   { order: :desc }}
  ]
}

query_string 变量基于用户关注的主题,可能看起来像这样:(与AND AND AND死"或(iphone)OR(视频AND游戏)"

The query_string variable is based off user followed topics and might look something like this: "(the AND walking AND dead) OR (iphone) OR (video AND games)"

我是否可以使用任何选项,以便返回的文档具有与搜索字词相匹配的属性,例如行尸走肉(与行尸走肉与死者)

Is there any option I can use so that documents returned would have a property matching a search term like the walking dead or (the AND walking AND dead)

推荐答案

如果您准备切换为使用 bool/should 查询,则可以在每个字段上拆分匹配项并使用命名查询,然后在结果中,您将获得匹配的查询的名称.

If you're ready to switch to using bool/should queries, you can split the match on each field and use named queries, then in the results you'll get the name of the query that matched.

基本上是这样的:在一个 bool/should 查询中,您为每个字段添加一个 query_string 查询并命名查询以标识该字段(例如< title 字段的code> title_query 等)

It goes basically like this: in a bool/should query, you add one query_string query per field and name the query so as to identify that field (e.g. title_query for the title field, etc)

{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "fields": [
              "title^10"
            ],
            "query": "query_string",
            "use_dis_max": false,
            "_name": "title_query"
          }
        },
        {
          "query_string": {
            "fields": [
              "keywords^4"
            ],
            "query": "query_string",
            "use_dis_max": false,
            "_name": "keywords_query"
          }
        },
        {
          "query_string": {
            "fields": [
              "content"
            ],
            "query": "query_string",
            "use_dis_max": false,
            "_name": "content_query"
          }
        }
      ]
    }
  }
}

结果中,您将在 _source 下找到另一个名为 matched_queries 的数组,其中包含与返回的文档匹配的查询的名称.

In the results, you'll then get below the _source another array called matched_queries which contains the name of the query that matched the returned document.

"_source": {
    ...
},
"matched_queries": [
    "title_query"
],

这篇关于ElasticSearch:使用匹配的搜索词标记文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆