【发布时间】: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