我在elasticsearch中索引了数据。索引名称是“demo”。
我为“demo”有两种类型(映射),一种是“用户”,另一种是“博客”。
“用户”类型具有字段-名称,城市,国家/地区其他字段,博客具有-“标题”,“描述”,“作者名”等。
现在,我想搜索“演示”。如果我要搜索“java”,那么它将带任何类型的任何字段(“用户”或“博客”)中包含“java”的所有文档。
最佳答案
您可以将 "_all"
field用于该索引。默认情况下,每种类型的每个字段都将包含在"_all"
字段中。然后,您可以仅对match
字段运行"_all"
查询。另外,在搜索索引时,只需不指定类型,所有类型都将被搜索。
这是一个例子:
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 - Elasticsearch-跨多种索引类型及其不同类型进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28830684/