Elasticsearch(11) intervals的使用

elasticsearch version 7.10.1
在Elasticsearch中,intervals查询是用来做复杂的区间表达式匹配的,它可以基于分析过的文本字段执行一系列复杂的关系运算。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查询用于找到文本字段中满足一系列间隔条件的文
  • all_of子句要求所有列出的间隔条件都必须满足。
  • ordered: 设置为true,表示间隔条件必须按照指定的顺序出现。
  • intervals: 包含了一系列具体的间隔条件
  • any_of: 至少有一个列出的间隔条件必须满足

第一个间隔条件

  • 查找与"my favorite food"这个查询完全匹配的文本。
  • 设置为0,表示查询词之间不允许有任何间隔(即它们是连续的)。
  • 设置为true,再次确认查询词必须按照给定的顺序出现。

第二个间隔条件

  • 第一个match:查找与"hot water"这个查询匹配的文本
  • 第二个match:查找与"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
        }
      }
    }
  }
}

你可能感兴趣的:(elastic的学习笔记,elasticsearch)