elasticsearch version 7.10.1
在Elasticsearch中,intervals查询是用来做复杂的区间表达式匹配的,它可以基于分析过的文本字段执行一系列复杂的关系运算。intervals查询特别适合于那些需要对文本数据进行模式匹配,而不只是单一词汇匹配的情况。
POST _search
{
"query": {
"intervals" : {
"my_text" : {
"all_of" : {
"ordered" : true,
"intervals" : [
{
"match" : {
"query" : "my favorite food",
"max_gaps" : 0,
"ordered" : true
}
},
{
"any_of" : {
"intervals" : [
{ "match" : { "query" : "hot water" } },
{ "match" : { "query" : "cold porridge" } }
]
}
}
]
}
}
}
}
}
使用intervals查询找寻句子中“quick”和“dog”之间不超过两个单词的文档
PUT /example_index
{
"mappings": {
"properties": {
"sentence": {
"type": "text",
"analyzer": "standard"
}
}
}
}
POST /example_index/_doc
{
"sentence": "The quick brown fox jumps over the lazy dog."
}
POST /example_index/_doc
{
"sentence": "Red roses are blue violets are red."
}
POST /example_index/_doc
{
"sentence": "The cat in the hat sat on the mat."
}
使用intervals查询查找包含数字“3”到“7”的连续序列的文档
GET /example_index/_search
{
"query": {
"intervals": {
"sentence": {
"all_of": {
"intervals": [
{
"match": {
"query": "quick"
}
},
{
"match": {
"query": "dog"
}
}
],
"ordered": true,
"max_gaps": 10
}
}
}
}
}
POST /example_index_numbers/_doc
{
"numbers": "1 2 3 4 5 6 7"
}
POST /example_index_numbers/_doc
{
"numbers": "8 9 10 11 12 13 14"
}
POST /example_index_numbers/_doc
{
"numbers": "15 16 17 18 19 20 21"
}
GET /example_index_numbers/_search
{
"query": {
"intervals": {
"numbers": {
"all_of": {
"intervals": [
{
"match": {
"query": "3"
}
},
{
"match": {
"query": "7"
}
}
],
"max_gaps": 3,
"ordered": true
}
}
}
}
}
查询在职位描述中包含了“blue”并且紧接着是“violets”的文档。
PUT /example_index
{
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "standard"
}
}
}
}
POST /example_index/_doc
{
"content": "John Doe is an engineer from New York working at XYZ Corp."
}
POST /example_index/_doc
{
"content": "Jane Smith is a software developer based in California."
}
POST /example_index/_doc
{
"content": "Michael Johnson works as a data scientist at ABC Inc. in Texas."
}
POST /example_index/_doc
{
"content": "Sarah Brown is a product manager living in Illinois."
}
POST /example_index/_doc
{
"content": "Emily Davis, an architect from Washington DC, joined XYZ Corp last year."
}
POST /example_index/_doc
{
"content": "Robert Harris, who lives in Oregon, is a senior software engineer."
}
POST /example_index/_doc
{
"content": "Jessica Wilson works in marketing for ABC Inc., located in Florida."
}
GET /example_index/_search
{
"query": {
"intervals": {
"sentence": {
"all_of": {
"intervals": [
{
"match": {
"query": "blue"
}
},
{
"match": {
"query": "violets"
}
}
],
"ordered": true,
"max_gaps":0
}
}
}
}
}