# Context suggester with search api

**URL:** <https://discuss.elastic.co/t/context-suggester-with-search-api/328245>\
**Category:** Elasticsearch\
**Created:** [March 22, 2023, 12:20pm UTC](https://discuss.elastic.co/t/context-suggester-with-search-api/328245 "2023-03-22T12:20:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mangeshs](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@mangeshs](https://discuss.elastic.co/u/mangeshs)\
**Post date:** [March 22, 2023, 12:20pm UTC](https://discuss.elastic.co/t/context-suggester-with-search-api/328245/1 "2023-03-22T12:20:35Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#context-suggester)  
can any one give me any sample documents which explains how to do in java api service

I am querying like this

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

```

and my mapping is

```auto
{
  "**_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"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 24, 2023, 12:17am UTC](https://discuss.elastic.co/t/context-suggester-with-search-api/328245/2 "2023-03-24T00:17:35Z")

</div>

Hi @mangeshs

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

```auto
 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);
  }

```

---

<div class="post-metadata">

**Author:** ![mangeshs](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@mangeshs](https://discuss.elastic.co/u/mangeshs)\
**Post date:** [March 24, 2023, 12:13pm UTC](https://discuss.elastic.co/t/context-suggester-with-search-api/328245/3 "2023-03-24T12:13:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 21, 2023, 12:13pm UTC](https://discuss.elastic.co/t/context-suggester-with-search-api/328245/4 "2023-04-21T12:13:39Z")

</div>

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