Terms and multi match search query for elasticsearch golang

287 Views Asked by At

I have a situation where I need to do elastic search based on multi-match.

case 1: phrase search for keywords field. should return the results on complete phrase match only(Should not split the phrase). case 2: for remaining fields 1 keyword match must return the results.

I combined the multi-match and terms queries using should. as below.

For example : if I search with "Phrase search in multiple fields"

First it should search as complete phrase in keywords field and later it can search in multi-match for other fields. Note: If I search with single word like "multiple" or "field" it should not return any results from keywords.

"search": {
    "method": "POST",
    "param": {
       "query": {
        "bool": {
            "should": [
            {
            "terms": {
            "keywords": [
               "Phrase search in multiple fields",
               " Phrase search in multiple fields"
              ]
             }
            },
            {
            "multi_match": {
                "query": "Phrase search in multiple fields",
                "fields": {
                "title": "title",
                    "metaTitle": "title",
                "href": "Path",
                },
                "fuzziness": "AUTO"
                   }
                }       
            ]
            }
           }
         }
    
1

There are 1 best solutions below

2
Musab Dogan On

case 1: phrase search for keywords field. should return the results on complete phrase match only(Should not split the phrase)

Use the keyword field type.

Should not split the phrase => if you use the keyword field type which mean is no analyzer for text it won't split the phrases.

case 2: for remaining fields 1 keyword match must return the results.

Standard analyzer will

Use the text field type.

1 keyword match must return the results => standard (default) analyzer tokenize the phrase with whitespaces (and some special characters) and split into the words.

I'm sharing some examples to explain deeply, hope it helps.

#Put the documents.
PUT test_stack_over_flow/_doc/1
{
  "title": "Phrase search in multiple fields"
}

#check the mapping
GET test_stack_over_flow
"mappings": {
  "properties": {
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

#1hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple fields"
            ]
          }
        }
      ]
    }
  }
}

#0hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple"
            ]
          }
        }
      ]
    }
  }
}

#1hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "multiple",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#1 hit (because of fuzziness enabled)
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "multipl3",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#1 hit because of minimum_should_match:1 (default) and multi_match hit.
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple"
            ]
          }
        },
        {
          "multi_match": {
            "query": "multiple",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#0 hit because niehtier terms nor multi_match hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple"
            ]
          }
        },
        {
          "multi_match": {
            "query": "multi",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#1 hit because terms hit.
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple fields"
            ]
          }
        },
        {
          "multi_match": {
            "query": "multi",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}