position_increment_gap

edit

Analyzed text fields take term positions into account, in order to be able to support proximity or phrase queries. When indexing text fields with multiple values a "fake" gap is added between the values to prevent most phrase queries from matching across the values. The size of this gap is configured using position_increment_gap and defaults to 100.

For example:

resp = client.index(
    index="my-index-000001",
    id="1",
    document={
        "names": [
            "John Abraham",
            "Lincoln Smith"
        ]
    },
)
print(resp)

resp1 = client.search(
    index="my-index-000001",
    query={
        "match_phrase": {
            "names": {
                "query": "Abraham Lincoln"
            }
        }
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    query={
        "match_phrase": {
            "names": {
                "query": "Abraham Lincoln",
                "slop": 101
            }
        }
    },
)
print(resp2)
response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    names: [
      'John Abraham',
      'Lincoln Smith'
    ]
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match_phrase: {
        names: {
          query: 'Abraham Lincoln'
        }
      }
    }
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match_phrase: {
        names: {
          query: 'Abraham Lincoln',
          slop: 101
        }
      }
    }
  }
)
puts response
const response = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    names: ["John Abraham", "Lincoln Smith"],
  },
});
console.log(response);

const response1 = await client.search({
  index: "my-index-000001",
  query: {
    match_phrase: {
      names: {
        query: "Abraham Lincoln",
      },
    },
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  query: {
    match_phrase: {
      names: {
        query: "Abraham Lincoln",
        slop: 101,
      },
    },
  },
});
console.log(response2);
PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln" 
      }
    }
  }
}

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln",
        "slop": 101 
      }
    }
  }
}

This phrase query doesn’t match our document which is totally expected.

This phrase query matches our document, even though Abraham and Lincoln are in separate strings, because slop > position_increment_gap.

The position_increment_gap can be specified in the mapping. For instance:

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "names": {
                "type": "text",
                "position_increment_gap": 0
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="1",
    document={
        "names": [
            "John Abraham",
            "Lincoln Smith"
        ]
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    query={
        "match_phrase": {
            "names": "Abraham Lincoln"
        }
    },
)
print(resp2)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        names: {
          type: 'text',
          position_increment_gap: 0
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    names: [
      'John Abraham',
      'Lincoln Smith'
    ]
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match_phrase: {
        names: 'Abraham Lincoln'
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      names: {
        type: "text",
        position_increment_gap: 0,
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    names: ["John Abraham", "Lincoln Smith"],
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  query: {
    match_phrase: {
      names: "Abraham Lincoln",
    },
  },
});
console.log(response2);
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "names": {
        "type": "text",
        "position_increment_gap": 0 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": "Abraham Lincoln" 
    }
  }
}

The first term in the next array element will be 0 terms apart from the last term in the previous array element.

The phrase query matches our document which is weird, but its what we asked for in the mapping.