Elasticsearch - How to do a partial match from your query

615 views Asked by At

I am a bit new to Elasticsearch and I am wondering how it is possible to do a partial match of a query on a particular filed.

Lets say I have a field called "department" and it has a value "accounting". So when I search for something like "The accounting", I should be able to get the result.

eg:- Below are my two documents:

{
   "name": "Joe",
   "department": "finance"
},
{
   "name": "Matt",
   "department": "accounting"
}

My search query on the field department is The accounting or The accounting department and my expected result should be:

{
   "name": "Matt",
   "department": "accounting"
}

UPDATE:

@Russ Cam: The Standard analyzer removes all the punctuation and special characters so what if I have the value in the field department saved as dept/accounting and when i search for dept: the dept/accounting I should get those documents that have the department value as dept/accounting.

I don't want ES to give me documents with the department as dept/accounting when someone searches for dept or accounting. Is this possible?

Assume that the following are my documents in ES:

{
   "name": "Matt",
   "department": "dept/accounting"
},
{
   "name": "Kate",
   "department": "dept"
},
{
   "name": "Adam",
   "department": "accounting"
}

The user searches for dept and he gets:

{
  "name": "Kate",
  "department": "dept"
}

When the user searches for blah blah dept/accounting blah he should get only this:

{
   "name": "Matt",
   "department": "dept/accounting"
}
2

There are 2 answers

4
prasad kp On

Have you tried match query ,this should work for you

{
  "query": {
    "match": {
      "department": "accounting"
    }
  }
1
drjz On

By default Elasticsearch is supporting the best match approach and the default Boolean operator is the OR. This should work out-of-the-box with for example the Query String query, see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html