I have indexed data in elasticsearch . Index name is "demo" . I have two types (mappings) for "demo" , one is "user" and other is "blog". "user" type have fields - name , city , country other fields and blog have - "title" , description" , "author_name" etc. Now I want to search across "demo". If I want to search "java" then it will bring all the documents which have "java" in any fields of any type , either "user" or "blog".
1 Answer
You can use the "_all" field for that index. By default each of your fields will be included in the "_all" field for each type. Then you can just run a match query against the "_all" field. Also, when searching the index, just don't specify a type and all types will be searched.
Here is an example:
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"
}
}
]
}
}