搜索多个数据流和索引

编辑

要搜索多个数据流和索引,请在搜索 API的请求路径中以逗号分隔的值添加它们。

以下请求搜索 my-index-000001my-index-000002 索引。

resp = client.search(
    index="my-index-000001,my-index-000002",
    query={
        "match": {
            "user.id": "kimchy"
        }
    },
)
print(resp)
response = client.search(
  index: 'my-index-000001,my-index-000002',
  body: {
    query: {
      match: {
        'user.id' => 'kimchy'
      }
    }
  }
)
puts response
const response = await client.search({
  index: "my-index-000001,my-index-000002",
  query: {
    match: {
      "user.id": "kimchy",
    },
  },
});
console.log(response);
GET /my-index-000001,my-index-000002/_search
{
  "query": {
    "match": {
      "user.id": "kimchy"
    }
  }
}

您还可以使用索引模式搜索多个数据流和索引。

以下请求的目标是 my-index-* 索引模式。该请求将搜索集群中任何以 my-index- 开头的数据流或索引。

resp = client.search(
    index="my-index-*",
    query={
        "match": {
            "user.id": "kimchy"
        }
    },
)
print(resp)
response = client.search(
  index: 'my-index-*',
  body: {
    query: {
      match: {
        'user.id' => 'kimchy'
      }
    }
  }
)
puts response
const response = await client.search({
  index: "my-index-*",
  query: {
    match: {
      "user.id": "kimchy",
    },
  },
});
console.log(response);
GET /my-index-*/_search
{
  "query": {
    "match": {
      "user.id": "kimchy"
    }
  }
}

要搜索集群中的所有数据流和索引,请从请求路径中省略目标。或者,您可以使用 _all*

以下请求是等效的,并搜索集群中的所有数据流和索引。

resp = client.search(
    query={
        "match": {
            "user.id": "kimchy"
        }
    },
)
print(resp)

resp1 = client.search(
    index="_all",
    query={
        "match": {
            "user.id": "kimchy"
        }
    },
)
print(resp1)

resp2 = client.search(
    index="*",
    query={
        "match": {
            "user.id": "kimchy"
        }
    },
)
print(resp2)
response = client.search(
  body: {
    query: {
      match: {
        'user.id' => 'kimchy'
      }
    }
  }
)
puts response

response = client.search(
  index: '_all',
  body: {
    query: {
      match: {
        'user.id' => 'kimchy'
      }
    }
  }
)
puts response

response = client.search(
  index: '*',
  body: {
    query: {
      match: {
        'user.id' => 'kimchy'
      }
    }
  }
)
puts response
const response = await client.search({
  query: {
    match: {
      "user.id": "kimchy",
    },
  },
});
console.log(response);

const response1 = await client.search({
  index: "_all",
  query: {
    match: {
      "user.id": "kimchy",
    },
  },
});
console.log(response1);

const response2 = await client.search({
  index: "*",
  query: {
    match: {
      "user.id": "kimchy",
    },
  },
});
console.log(response2);
GET /_search
{
  "query": {
    "match": {
      "user.id": "kimchy"
    }
  }
}

GET /_all/_search
{
  "query": {
    "match": {
      "user.id": "kimchy"
    }
  }
}

GET /*/_search
{
  "query": {
    "match": {
      "user.id": "kimchy"
    }
  }
}

索引提升

编辑

搜索多个索引时,您可以使用 indices_boost 参数来提升来自一个或多个指定索引的结果。当来自某些索引的命中结果比来自其他索引的命中结果更重要时,这很有用。

您不能将 indices_boost 与数据流一起使用。

resp = client.search(
    indices_boost=[
        {
            "my-index-000001": 1.4
        },
        {
            "my-index-000002": 1.3
        }
    ],
)
print(resp)
response = client.search(
  body: {
    indices_boost: [
      {
        "my-index-000001": 1.4
      },
      {
        "my-index-000002": 1.3
      }
    ]
  }
)
puts response
const response = await client.search({
  indices_boost: [
    {
      "my-index-000001": 1.4,
    },
    {
      "my-index-000002": 1.3,
    },
  ],
});
console.log(response);
GET /_search
{
  "indices_boost": [
    { "my-index-000001": 1.4 },
    { "my-index-000002": 1.3 }
  ]
}

也可以使用别名和索引模式

resp = client.search(
    indices_boost=[
        {
            "my-alias": 1.4
        },
        {
            "my-index*": 1.3
        }
    ],
)
print(resp)
response = client.search(
  body: {
    indices_boost: [
      {
        "my-alias": 1.4
      },
      {
        "my-index*": 1.3
      }
    ]
  }
)
puts response
const response = await client.search({
  indices_boost: [
    {
      "my-alias": 1.4,
    },
    {
      "my-index*": 1.3,
    },
  ],
});
console.log(response);
GET /_search
{
  "indices_boost": [
    { "my-alias":  1.4 },
    { "my-index*": 1.3 }
  ]
}

如果找到多个匹配项,将使用第一个匹配项。例如,如果索引包含在 alias1 中并与 my-index* 模式匹配,则应用 1.4 的提升值。