Context suggester with search api

I am using Elasticsearch 7.4 and java client api. I want to use context suggester with search api given in this document Suggesters | Elasticsearch Guide [8.6] | Elastic
can any one give me any sample documents which explains how to do in java api service

I am querying like this

POST ***_autosuggest/_search?pretty
{
  "suggest": {
    "place_suggestion": {
      "prefix": "tim",
      "completion": {
        "field": "suggest",
        "size": 10,
        "contexts": {
          "genre": [ "**-faculty", "**-companies" ]
        }
      }
    }
  }
}

and my mapping is

{
  "**_autosuggest": {
    "aliases": {},
    "mappings": {
      "properties": {
        "dictionary": {
          "type": "keyword",
          "index": false,
          "store": true
        },
        "suggest": {
          "type": "completion",
          "analyzer": "suggestionAnalyzer",
          "preserve_separators": true,
          "preserve_position_increments": false,
          "max_input_length": 50,
          "contexts": [
            {
              "name": "genre",
              "type": "CATEGORY"
            }
          ]
        },
        "suggestion": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "title": {
          "type": "keyword",
          "index": false,
          "store": true
        }
      }
    },
    "settings": {
      "index": {
        "number_of_shards": "1",
        "provided_name": "***_autosuggest",
        "creation_date": "1679448840300",
        "analysis": {
          "filter": {
            "en_stop": {
              "type": "stop",
              "stopwords": [
                "_english_"
              ]
            }
          },
          "analyzer": {
            "suggestionAnalyzer": {
              "filter": [
                "lowercase",
                "en_stop"
              ],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        },
        "number_of_replicas": "0",
        "uuid": "t97z4ZYdRB2KclQV28fTTQ",
        "version": {
          "created": "136277827"
        }
      }
    }
  }
}

Hi @mangeshs

This example is using Java High level. I hope it helps you get started.

 public static void main(String[] args) throws IOException {

    var suggestBuilder = new SuggestBuilder();
    var searchSourceBuilder = new SearchSourceBuilder();

    suggestBuilder.addSuggestion("place_suggestion", SuggestBuilders.completionSuggestion("suggest")
        .skipDuplicates(true)
        .contexts(getContext())
        .prefix("tim"));

    searchSourceBuilder.suggest(suggestBuilder);

    var searchRequest = new SearchRequest();
    searchRequest.source(searchSourceBuilder);
    SearchResponse result = getClient().search(searchRequest, RequestOptions.DEFAULT);
  }

  public static Map<String, List<? extends ToXContent>> getContext() {
    List<CategoryQueryContext> ctx = new ArrayList<>();
    ctx.add(CategoryQueryContext.builder().setCategory("**-faculty").build());
    ctx.add(CategoryQueryContext.builder().setCategory("**-companies").build());
    return Collections.singletonMap("genre", ctx);
  }

Thanks, I will try this and let you know. FYI I am using Elasticsearch 7.4 version hope that is okay.

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