Search code examples
elasticsearchelasticsearch-5

How Do I Get ES To Ignore Conjunctions (and, of, or, etc...) In The Query String?


I have the following Elasticsearch search config:

  query_string: {
    query: `${sanitizedQueryString}~`,
    fields: ['fieldOne^5', 'fieldTwo^5', 'fieldThree'],
  }, 

The above breaks down every search string I give it into its separate words and searches around every word in the search string. If I have the following records:

[{
    id: 1,
    name: 'Harris'
}, {
    id: 2,
    name: 'Smith'
}, {
    id: 3,
    name: 'Dallas'
}, {
    id: 4,
    name: 'Farmers And Workers'
}];

and my search query was harris smith, then I'd get back the first 2 records in the above array.

If I pass in the search query harris and smith, I would get back 3 records - the first 2 records and the last record. In this scenario, the last record would be returned because it has the word And in its name and my search query also has the word and.

Words like and, of, and or are called conjunctions in English. How do I exclude conjunctions in my search queries from being analyzed and searched on?


Solution

  • It seems you are using default Standard analyzer, Where stop token filter is disabled by default. This is how your token is being created -

    POST _analyze
    {
      "analyzer": "standard",
      "text": "harris and smith"
    }
    

    Lets tweak the mapping and analyzer

    PUT /standard_example
    {
      "settings": {
        "analysis": {
          "analyzer": {
            "rebuilt_standard": {
              "tokenizer": "standard",
              "filter": [
                "lowercase",
                "stop"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "name":{
            "type": "text",
            "analyzer": "rebuilt_standard"
          }
        }
      }
    }
    

    Index data

    PUT /standard_example/_bulk
    {"create":{"_id":1}}
    {"id":1,"name":"Harris"}
    {"create":{"_id":2}}
    {"id":2,"name":"Smith"}
    {"create":{"_id":3}}
    {"id":3,"name":"Dallas"}
    {"create":{"_id":4}}
    {"id":4,"name":"Farmers And Workers"}
    

    Perform search with query string -

    GET standard_example/_search
    {
      "query": {
        "query_string": {
          "query": "Harris and Smith",
          "fields": ["name"]
        }
      }
    }