RamRajVasavi
RamRajVasavi

Reputation: 906

match_query is not working in elasticsearch

I am new ElasticSearch.Recently we started using ElasticSearch in my project. I am trying to get data based on first_name and last_name by using match_query. I write query like this

{
"query":{
  "multi_match" : {
    "query": "michkel",
    "fields": [ "last_name"] 

  }
}
}

My index name is employee and it contains below documents

{
   "first_name":"smith",
"last_name":"michkel",
"age":25,
"doj":"2015-05-2"
},
{
   "first_name":"john",
"last_name":"smith",
"age":46,
"doj":"1989-03-25"
}

when run the above query i am getting total records but my requirement is if i give "query": "michkel" like this, then only one record is match with my documents. Give me solutions as soon as possible And one more question I want to search based on user given date.My user will give date like this 1989-04-15.for this i write like below { "query":{ " query_string" : { "query": "1989-04-15",

  }
}
}
{"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[du1Tb7U8QLWSnXO2m9ATCg][employee][0]: SearchParseException[[employee][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n\"query\":{\n  \" query_string\" : {\n    \"query\": \"1989-04-15\",\n\n  }\n}\n}]]]; nested: QueryParsingException[[employee] No query registered for [ query_string]]; }]","status":400}

Upvotes: 2

Views: 73

Answers (1)

Neo-coder
Neo-coder

Reputation: 7840

Instead of multi_match use bool filter as below :

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "last_name": "michkel"
                            }
                        }
                    ]
                }
            }
        }
    }
}

As per your edited question you should use same above in match replace as "doj": "1989-04-15" and if you want to find out combing both last_name and doj then use query as below :

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "query_string": {
                                "query": "1989-04-15"
                            }
                        }
                    ],
                    "must": [
                        {
                            "match": {
                                "last_name": "michkel"
                            }
                        }
                    ]
                }
            }
        }
    }
}

Upvotes: 1

Related Questions