结果定位,而不是突出显示

时间:2018-04-10 10:23:57

标签: elasticsearch

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

创建索引:

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 }
]

希望你明白我的想法!

1 个答案:

答案 0 :(得分:0)

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

"term_vector": "with_positions_offsets_payloads"

有关具体查询,请按照链接的文档页

进行操作
相关问题