When using the msearch (multi-search) API in Elasticsearch, you can search across multiple indices in a single request. Each search query in the msearch request can be targeted to a specific index..
Create Elasticsearch Index
PUT product-index
{
"settings": {
"analysis": {
"normalizer": {
"lowercaseNormalizer": {
"type": "custom",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"properties": {
"Product": {
"type": "object",
"properties": {
"ProductName": {
"type": "keyword",
"normalizer": "lowercaseNormalizer"
},
"ProductCode": {
"type": "keyword",
"normalizer": "lowercaseNormalizer"
},
"ProductDescription": {
"type": "text"
},
"ProductFamily": {
"type": "keyword",
"normalizer": "lowercaseNormalizer"
},
"Color": {
"type": "keyword"
}
}
}
}
}
}
Adding sample data/records
#Add first Document in elasticsearch
PUT /product-index/_doc/1
{
"Product": {
"ProductName": "CBC Anti Virus",
"ProductCode": "Network",
"ProductDescription": "APAC",
"ProductFamily": "Sales Cloud",
"Color": "Silver"
}
}
PUT /product-index/_doc/2
{
"Product": {
"ProductName": "CBC Esoteric",
"ProductDescription": "CBC Panel - 1",
"ProductFamily": "Hardware",
"Color": "BLUE"
}
}
PUT /product-index/_doc/3
{
"Product": {
"ProductName": "CBC Oncology",
"ProductCode": "CBC Health",
"ProductFamily": "Software"
}
}
PUT /product-index/_doc/4
{
"Product": {
"ProductName": "CBC Panel",
"ProductCode": "Secure Wifi",
"ProductDescription": "Only One Charge Type"
}
}
PUT /product-index/_doc/5
{
"Product": {
"ProductName": "CBC Panel (Invitro)",
"ProductCode": "Secure Wifi",
"ProductDescription": "CBC Panel",
"ProductFamily": "Hardware",
"Color": "GREEN"
}
}
PUT /product-index/_doc/6
{
"Product": {
"ProductName": "CBC Panel - Beijing",
"ProductCode": "CBC Panel",
"ProductDescription": "Only One Charge Type",
"ProductFamily": "Software",
"Color": "BLUE"
}
}
PUT /product-index/_doc/7
{
"Product": {
"ProductName": "Esoteric",
"ProductCode": "CBC",
"ProductFamily": "Marketing Cloud",
"Color": "WHITE"
}
}
PUT /product-index/_doc/8
{
"Product": {
"ProductName": "Oncology",
"ProductCode": "Network",
"ProductDescription": "CBC",
"ProductFamily": "Sales Cloud",
"Color": "WHITE"
}
}
Create Elasticsearch Index
PUT order-index
{
"settings": {
"analysis": {
"normalizer": {
"lowercaseNormalizer": {
"type": "custom",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"properties": {
"Order": {
"type": "object",
"properties": {
"OrderCode": {
"type": "keyword",
"normalizer": "lowercaseNormalizer"
},
"Description": {
"type": "text"
},
"Family": {
"type": "keyword",
"normalizer": "lowercaseNormalizer"
},
"ProductName": {
"type": "keyword",
"normalizer": "lowercaseNormalizer"
},
"ProductId": {
"type": "keyword"
}
}
}
}
}
}
Adding sample data/records
#Add first Document in elasticsearch
PUT /order-index/_doc/5c57754b-86a6-41f1-a4af-de65b531fe8f
{
"Order": {
"OrderCode": "Network",
"Description": "APAC",
"Family": "Sales Cloud",
"ProductName": "CBC Anti Virus",
"ProductId": 1
}
}
PUT /order-index/_doc/218c4907-89c2-482f-90cc-61dd7e27f999
{
"Order": {
"OrderCode": "Network",
"Description": "CBC Panel - 1",
"Family": "Hardware",
"ProductName": "CBC Esoteric",
"ProductId": 2
}
}
View data/regcords
GET /product-index/_search
GET /order-index/_search
Search case-insensitive records
In the request body, provide the search queries in a newline-separated JSON format. Each search query should have two parts: the header and the search body. The header contains information about the index and search type, while the search body contains the actual search query DSL.
#Try 1
POST http://localhost:9200/_msearch
Content-type:application/json
Payload as below
{"index" : "product-index"}
{"query": {"match": {"Product.OrderCode": "Network"}}, "from" : 0, "size" : 10}
{"index" : "order-index"}
{"query": {"match": {"Order.OrderCode": "Network"}}}
Note
- Text fields are analyzed.
- For keyword fields we need to add custom elasticsearch setting for analysis.
Reference
- run elasticsearch locally run elasticsearch locally
- Documentation: Elasticsearch - _msearch
No comments:
Post a Comment