word search on multiple fields in elastic search

Viewed 518

I have input message is "Java developer with 4 years " and there are 2 fields in DB which are skill and exp.

here skill field maintain : java developer etc

exp field maintains : 4 years etc

When I search string(Java developer with 4 years) it has to search on these 2 fields and get only java developer having 4 years exp only.

1 Answers

You can use the boolean match query with must clause on your two fields(skill and experience).

Please refer https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html for example and how to do it.

working example

Index Def

{
  "mappings": {
    "properties": {
      "skill": {
        "type": "text"
      },
      "exp" :{
        "type" : "text"
      }
    }
  }
}

Index sample docs

{
  "skill" : "java",
  "exp" : "4 years"
}

{
  "skill" : "php",
  "exp" : "4 years"
}

Search only for java with 4 years exp

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "skill": "java"
                    }
                },
                {
                    "match": {
                        "exp": "4 years"
                    }
                }
            ]
        }
    }
}

And result

 "hits": [
      {
        "_index": "multimatch",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0577903,
        "_source": {
          "skill": "java",
          "exp": "4 years"
        }
      }
    ]
Related