Elasticsearch - 跨多种类型的索引及其不同类型进行搜索
Elasticsearch - searching across multiple multiple types of a index and its different types
我在 elasticsearch 中索引了数据。索引名称是 "demo" 。
"demo" 我有两种类型(映射),一种是 "user",另一种是 "blog"。
"user" 类型有字段 - name 、 city 、 country 其他字段和博客有 - "title" 、 description" 、 "author_name" 等。
现在我想搜索 "demo"。如果我想搜索 "java" 那么它会带来任何类型的任何字段中具有 "java" 的所有文档,"user" 或 "blog".
您可以为该索引使用 "_all"
field。默认情况下,您的每个字段都将包含在每种类型的 "_all"
字段中。然后您可以 运行 对 "_all"
字段进行 match
查询。另外,搜索索引时,只要不指定类型,就会搜索所有类型。
这是一个例子:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"user": {
"properties": {
"name" : { "type": "string" },
"city" : { "type": "string" },
"country" : { "type": "string" }
}
},
"blog": {
"properties": {
"title" : { "type": "string" },
"description" : { "type": "string" },
"author_name" : { "type": "string" }
}
}
}
}
POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"user"}}
{"name":"Bob","city":"New York","country":"USA"}
{"index":{"_index":"test_index","_type":"user"}}
{"name":"John","city":"Jakarta","country":"Java/Indonesia"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Python/ES","description":"using Python with Elasticsearch","author_name":"John"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Java/ES","description":"using Java with Elasticsearch","author_name":"Bob"}
POST /test_index/_search
{
"query": {
"match": {
"_all": "Java"
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.68289655,
"hits": [
{
"_index": "test_index",
"_type": "blog",
"_id": "hNJ-AOG2SbS0nw4IPBuXGQ",
"_score": 0.68289655,
"_source": {
"title": "Java/ES",
"description": "using Java with Elasticsearch",
"author_name": "Bob"
}
},
{
"_index": "test_index",
"_type": "user",
"_id": "VqfowNx8TTG69buY9Vd_MQ",
"_score": 0.643841,
"_source": {
"name": "John",
"city": "Jakarta",
"country": "Java/Indonesia"
}
}
]
}
}
我在 elasticsearch 中索引了数据。索引名称是 "demo" 。 "demo" 我有两种类型(映射),一种是 "user",另一种是 "blog"。 "user" 类型有字段 - name 、 city 、 country 其他字段和博客有 - "title" 、 description" 、 "author_name" 等。 现在我想搜索 "demo"。如果我想搜索 "java" 那么它会带来任何类型的任何字段中具有 "java" 的所有文档,"user" 或 "blog".
您可以为该索引使用 "_all"
field。默认情况下,您的每个字段都将包含在每种类型的 "_all"
字段中。然后您可以 运行 对 "_all"
字段进行 match
查询。另外,搜索索引时,只要不指定类型,就会搜索所有类型。
这是一个例子:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"user": {
"properties": {
"name" : { "type": "string" },
"city" : { "type": "string" },
"country" : { "type": "string" }
}
},
"blog": {
"properties": {
"title" : { "type": "string" },
"description" : { "type": "string" },
"author_name" : { "type": "string" }
}
}
}
}
POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"user"}}
{"name":"Bob","city":"New York","country":"USA"}
{"index":{"_index":"test_index","_type":"user"}}
{"name":"John","city":"Jakarta","country":"Java/Indonesia"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Python/ES","description":"using Python with Elasticsearch","author_name":"John"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Java/ES","description":"using Java with Elasticsearch","author_name":"Bob"}
POST /test_index/_search
{
"query": {
"match": {
"_all": "Java"
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.68289655,
"hits": [
{
"_index": "test_index",
"_type": "blog",
"_id": "hNJ-AOG2SbS0nw4IPBuXGQ",
"_score": 0.68289655,
"_source": {
"title": "Java/ES",
"description": "using Java with Elasticsearch",
"author_name": "Bob"
}
},
{
"_index": "test_index",
"_type": "user",
"_id": "VqfowNx8TTG69buY9Vd_MQ",
"_score": 0.643841,
"_source": {
"name": "John",
"city": "Jakarta",
"country": "Java/Indonesia"
}
}
]
}
}