0
votes

I'm very new to elastic search, how do I write a query which search for a keyword (ie. test keyword) in all fields in the document, and boost for

  1. exact match for this keyword phrase in all fields.
  2. occurrences for certain fields (which I have boosted 5 for A, 3 for B and 1 for C)

I see some documentation on match_phrase, but it doesn't seem to support multiple fields.

{
    "query": {
        "multi_match": {
            "query": "test keyword",
            "fields": ["A^5", "B^3", "C^1"]
        }
    }
}
1
can you please share your index mapping, sample index document and expected search result ?ESCoder

1 Answers

1
votes

If you want an exact match for the keyword phrase in all fields along with boost then try out this below search query where the multi-match query is used with type phrase parameter :

Adding a working example with index data, search query, and search result

Index data:

{
  "A":"test keyword",
  "B":"a",
  "C":"c"
}
{
  "A":"a",
  "B":"test keyword",
  "C":"c"
}
{
  "A":"a",
  "B":"b",
  "C":"test keyword"
}

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "test keyword",
            "fields": [
              "A^5",
              "B^3",
              "C^1"
            ],
            "type":"phrase"    <-- note this
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "stof_64266554",
        "_type": "_doc",
        "_id": "1",
        "_score": 16.285465,
        "_source": {
          "A": "test keyword",
          "B": "a",
          "C": "c"
        }
      },
      {
        "_index": "stof_64266554",
        "_type": "_doc",
        "_id": "2",
        "_score": 8.142733,
        "_source": {
          "A": "a",
          "B": "test keyword",
          "C": "c"
        }
      },
      {
        "_index": "stof_64266554",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.6285465,
        "_source": {
          "A": "a",
          "B": "b",
          "C": "test keyword"
        }
      }
    ]