es中如何分析文档内toker和他们的position
时间: 2024-02-25 21:52:44 浏览: 14
在ES中,可以使用term vectors来获取文档中每个词项(term)的位置信息。term vectors是一种结构化数据,它记录了每个词项在文档中的位置、频率、偏移量等信息。
要获取文档的term vectors,可以使用termvector API。例如,以下是一个获取文档term vectors的示例:
```
GET /my_index/_termvector/1
{
"fields": ["my_text_field"],
"offsets": true,
"positions": true,
"term_statistics": true,
"field_statistics": true
}
```
其中,my_index是要查询的索引名称,1是要查询的文档ID,my_text_field是要查询的文本字段名称。通过设置offsets和positions参数为true,可以获取每个词项的偏移量和位置信息。
返回结果将包含每个词项的term vectors信息,例如:
```
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_version": 1,
"found": true,
"took": 1,
"term_vectors": {
"my_text_field": {
"field_statistics": {
"sum_doc_freq": 8,
"doc_count": 3,
"sum_ttf": 8
},
"terms": {
"brown": {
"term_freq": 1,
"tokens": [
{
"position": 2,
"start_offset": 5,
"end_offset": 10
}
]
},
"dog": {
"term_freq": 1,
"tokens": [
{
"position": 1,
"start_offset": 0,
"end_offset": 3
}
]
},
"fox": {
"term_freq": 1,
"tokens": [
{
"position": 0,
"start_offset": 11,
"end_offset": 14
}
]
},
"jumps": {
"term_freq": 1,
"tokens": [
{
"position": 3,
"start_offset": 15,
"end_offset": 20
}
]
},
"lazy": {
"term_freq": 1,
"tokens": [
{
"position": 4,
"start_offset": 21,
"end_offset": 25
}
]
},
"over": {
"term_freq": 1,
"tokens": [
{
"position": 5,
"start_offset": 26,
"end_offset": 30
}
]
},
"quick": {
"term_freq": 1,
"tokens": [
{
"position": 6,
"start_offset": 31,
"end_offset": 36
}
]
},
"red": {
"term_freq": 1,
"tokens": [
{
"position": 7,
"start_offset": 37,
"end_offset": 40
}
]
}
}
}
}
}
```
在返回结果中,每个词项都包含了它的term frequency(term_freq)和tokens信息。tokens信息包括了每个词项在文档中的位置信息,包括它的position、start_offset和end_offset。
希望这可以帮助您获取文档内toker和它们的position信息。