# Intervalsクエリで、同一文字列を指定した場合の挙動に違和感

**URL:** <https://discuss.elastic.co/t/intervals/387934>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [July 9, 2026, 5:18am UTC](https://discuss.elastic.co/t/intervals/387934 "2026-07-09T05:18:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![2e65f6a209d2626883a5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/2e65f6a209d2626883a5/32/115775_2.png) [@2e65f6a209d2626883a5](https://discuss.elastic.co/u/2e65f6a209d2626883a5)\
**Post date:** [July 9, 2026, 5:18am UTC](https://discuss.elastic.co/t/intervals/387934/1 "2026-07-09T05:18:10Z")

</div>

お世話になっております。

当方、elasticsearch9.4で動作確認を行っております。  
intervalsクエリで、同一の文字列（複数トークン）を指定したときの距離のカウント方法に違和感があるのですが、どのようなカウントになっているのかご教示いただければ幸いです。

■インデックス設定、データ投入

```applescript
PUT test_intervals
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "my_ngram_tok": {"type": "ngram","min_gram": 1,"max_gram": 1}
      },
      "analyzer": {
        "my_unigram": {"type": "custom","tokenizer": "my_ngram_tok"}
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {"type": "text","analyzer": "my_unigram"}
    }
  }
}

POST test_intervals/_doc/1
{
  "text": "あいうあいうえおかきくけこ"
}

```

■検索クエリ  
ケース１(OK)

```auto
GET test_intervals/_search
{
  "query": {
    "intervals": {
      "text": {
        "all_of": {
          "ordered": true,
          "max_gaps": 2,
          "intervals": [
            {"match": {"query": "あ","max_gaps": 0,"ordered": true}},
            {"match": {"query": "あ","max_gaps": 0,"ordered": true}}
          ]
        }
      }
    }
  }
}

```

→max\_gaps\>=2でヒット。納得の動作。

ケース２(OK)

```auto
GET test_intervals/_search
{
  "query": {
    "intervals": {
      "text": {
        "all_of": {
          "ordered": true,
          "max_gaps": 1,
          "intervals": [
            {"match": {"query": "あい","max_gaps": 0,"ordered": true}},
            {"match": {"query": "あ","max_gaps": 0,"ordered": true}}
          ]
        }
      }
    }
  }
}

```

→max\_gaps\>=1でヒット。納得の動作。

ケース３(???)

```auto
GET test_intervals/_search
{
  "query": {
    "intervals": {
      "text": {
        "all_of": {
          "ordered": true,
          "max_gaps": 1,
          "intervals": [
            {"match": {"query": "あい","max_gaps": 0,"ordered": true}},
            {"match": {"query": "あい","max_gaps": 0,"ordered": true}}
          ]
        }
      }
    }
  }
}

```

→max\_gaps=1でヒットしない。5以上ならヒットする。なぜ？

ケース４(OK)

```auto
GET test_intervals/_search
{
  "query": {
    "intervals": {
      "text": {
        "all_of": {
          "ordered": true,
          "max_gaps": 2,
          "intervals": [
            {"match": {"query": "あい","max_gaps": 0,"ordered": true}},
            {"match": {"query": "いう","max_gaps": 0,"ordered": true}}
          ]
        }
      }
    }
  }
}

```

→max\_gapsが2以上でヒット。納得。

ご教示いただければ幸いです。
