I want to extract the field names where the search text appears in the elastic search (stored) indexed documents.
Is this type of querying possible in elastic search, I am using Nest Client in C#
Please refer to the example below:
Example: employee document { "first_name" : "emp first", "last_name" : "emp last" }
Input search text: "first" Expected out : ["first_name"]
Input search text : "emp" Expected output : ["first_name", "last_name"]
Thanks, AT
There is a feature in elasticsearch "Named Queries" , you can named each query and elasticsearch will return the matched queries names
For your case you can use this query
GET index/doc_type/_search
{
"_source": [
"first_name",
"last_name"
],
"query": {
"bool": {
"should": [
{
"match": {
"first_name": {
"query": "emp",
"_name": "first_name"
}
}
},
{
"match": {
"last_name": {
"query": "emp",
"_name": "last_name"
}
}
}
]
}
}
}
Elasticsearch will return result like this one
{
"took": 90,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 16.399673,
"hits": [
{
"_index": "index",
"_type": "doc_type",
"_id": "1",
"_score": 16.399673,
"_routing": "1",
"_source": {
"first_name": "emp first",
"last_name": "emp last"
},
"matched_queries": [
"first_name",
"last_name"
]
}
]
}
}
You can also do the same thing with highlighting
GET index/doc_type/_search
{
"_source": [
"first_name",
"last_name"
],
"query": {
"bool": {
"should": [
{
"match": {
"first_name": "emp"
}
},
{
"match": {
"last_name": "emp"
}
}
]
}
},
"highlight": {
"fields": {
"first_name": {},
"last_name" : {}
}
}
}
Sample Response :
{
"took": 90,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 16.399673,
"hits": [
{
"_index": "index",
"_type": "doc_type",
"_id": "1",
"_score": 16.399673,
"_routing": "1",
"_source": {
"first_name": "emp first",
"last_name": "emp last"
},
"highlight": [
"first_name" : ["<em>emp</em> first"],
"last_name" : ["<em>emp</em> last"]
]
}
]
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.