【问题标题】:Position as result, instead of highlighting作为结果定位,而不是突出显示
【发布时间】:2018-04-11 17:41:22
【问题描述】:

我尝试获取位置而不是突出显示的文本作为弹性搜索查询的结果。

创建索引:

PUT /test/
{
  "mappings": {
    "article": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "english"
        },
        "author": {
          "type": "text"
        }
      }
    }
  }
}

放一个文件:

PUT /test/article/1
{
  "author": "Just Me",
  "text": "This is just a simple test to demonstrate the audience the purpose of the question!"
}

搜索文档:

GET /test/article/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "text": {
              "query": "simple test",
              "_name": "must"
            }
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "text": {
              "query": "need help",
              "_name": "first",
              "slop": 2
            }
          }
        },
        {
          "match_phrase": {
            "text": {
              "query": "purpose question",
              "_name": "second",
              "slop": 3
            }
          }
        },
        {
          "match_phrase": {
            "text": {
              "query": "don't know anything",
              "_name": "third"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "highlight": {
    "fields": {
      "text": {}
    }
  }
}

当我运行这个搜索时,我得到这样的结果:

This is just a simple test to <em>demonstrate</em> the audience the purpose of the <em>question</em>!

我对用 em 标签包围的结果不感兴趣,但我想像这样获得结果的所有位置:

"hits": [
   { "start_offset": 30, "end_offset": 40 },
   { "start_offset": 74, "end_offset": 81 }
]

希望你能明白我的想法!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    要获得文本中单词的偏移位置,您应该将termvector - doc here 添加到索引映射中。正如文档中所写,您必须在索引时启用此参数:

    "term_vector": "with_positions_offsets_payloads"
    

    具体查询请关注链接文档页面

    【讨论】:

    • 感谢您的回答,但这并不能解决我的问题。也许我的问题不够精确。或者我可能没有正确理解 term_vectors 。当我正确理解这篇文章时,elasticsearch 会返回一个包含该文档中所有单词的“列表”,包括出现的位置。要获得这些职位,我需要确切地知道这个词。但我需要搜索短语,我也搜索同义词。所以我无法按照示例提供的方式获取列表中的匹配项。我已经编辑了我的问题以显示一个更现实的查询并想知道如何将其与 term_vectors 匹配。
    • @Stefan 看看这个答案。 stackoverflow.com/questions/63460335/…它解决了你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 2017-12-01
    • 2019-02-23
    • 2021-05-12
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多