Search code examples
elasticsearchautocompleteresthighlevelclient

ElasticSearch 7.6.3 Java HighLevel Rest Client : Auto Suggest across multiple fields - How to implement


We have a index with the following fields and there is a requirement to give an Auto Suggest to the user by searching the data across all text and keyword mapping fields in the index

{


"settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
     }
  },

  "mappings": {
    "properties": {
      "id": {
        "type": "text"
      },
      "title": {
        "type": "text"
      },
      "date": {
        "type": "date",
        "format": "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
      },
      "subject": {
        "type": "text"
      },
      "title_suggest": {
            "type": "completion",
            "analyzer": "simple",
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50
        },
        "subject_suggest": {
            "type": "completion",
            "analyzer": "simple",
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50

        }
      "fieldOr": {
        "type": "text"
      },
      "fieldsTa": {
        "type": "text"
      },
      "notes": {
        "type": "text"
      },
      "fileDocs": {
        "type": "nested",
        "properties": {
          "fileName": {
            "type": "text",
            "analyzer": "autocomplete", 
            "search_analyzer": "standard"
          },
          "fileContent": {
            "type": "text",
            "analyzer": "autocomplete", 
            "search_analyzer": "standard" 
          },
          "docType": {
            "type": "keyword"
          },
          "opinionId": {
            "type": "integer"
          }
        }
      },   
      "fileMeta": {
        "type": "nested",
        "properties": {
          "url": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

I have tried the Completion Suggest but it works with 1 fields. I have created 2 fields with *-suggest in the index and tried to create the Suggest using the completionSuggest

SuggestBuilders.completionSuggestion("my_index_suggest").text(input);

But it supports 1 field only. I am using ES 7.6.3 with Java HighLevel Rest Client and it works for 1 fields. What changes I need to do to support across multiple fields . Is this possible via JSON search ? If yes then I can create a json using Xcontentbuilder and do a Auto suggest ?


Solution

  • Use the copy_to and copy all your desired fields to one field and perform your suggestion on top of it.

    Example from the documentation for copy_to is,

    PUT my_index
    {
      "mappings": {
        "properties": {
          "first_name": {
            "type": "text",
            "copy_to": "full_name" 
          },
          "last_name": {
            "type": "text",
            "copy_to": "full_name" 
          },
          "full_name": {
            "type": "text"
          }
        }
      }
    }