Boolean Logic in Synonym Set?

Hi all,

I’m trying to create a synonym set for an acronym so it will match against an exact phrase, like "United Stated of America" for USA. It seems I can’t put quotation marks in the synonym set fields. Does anyone know if there's another way to do this with “and” operators?

Thanks for your insights!

It seems I can’t put quotation marks in the synonym set fields

Can you explain what exactly you are trying to achieve? Generally speaking, "synonym_graph" filter should be the right thing for this kind of job (multi token matching) for synonyms. In cases like this its usually best to leave out synonyms expansion at index time and make sure at search time the query gets expanded to the right set of synonyms, e.g.

DELETE test_index

PUT /test_index
{
  "mappings": {
    "properties": {
      "f1" : {
        "type": "text",
        "analyzer": "standard", 
        "search_analyzer": "synonym"
        
      }
    }
  }, 
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "standard",
            "filter": [ "lowercase", "synonym" ]
          }
        },
        "filter": {
          "synonym": {
            "type": "synonym_graph",
            "synonyms": [
              "United States of America, USA"
            ]
          }
        }
      }
    }
  }
}

POST /test_index/_analyze
{
  "analyzer": "synonym",
  "text" : "USA"
}

PUT /test_index/_doc/1
{
  "f1" : "300 million people live in the United States of America."
}

PUT /test_index/_doc/2
{
  "f1" : "The USA is inhabited by 300 million people."
}

POST /test_index/_search
{
  "query": {
    "match": {
      "f1": "USA"
    }
  }
}

POST /test_index/_search
{
  "query": {
    "match": {
      "f1": "United States of America"
    }
  }
}

Sometimes this creates issues with "false positive" matches, e.g. if you would expand "United States" to "US" and also have lowercasing in place you would suddenly match all kinds of "us" pronouns. Some careful considerations of these cases however usually get you around this.

If the above doesn't work for your case please specify in some more details what you are trying to achieve, best together with the settings / mappings, some example documents, an example query and what you would expect to match.

Cheers

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.