# Unexpected behaviour of termvector API in Python

**URL:** https://discuss.elastic.co/t/unexpected-behaviour-of-termvector-api-in-python/228948
**Category:** Elasticsearch
**Created:** [April 21, 2020, 2:49am UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-termvector-api-in-python/228948 "2020-04-21T02:49:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Thek10patil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thek10patil/32/73940_2.png) [@Thek10patil](https://discuss.elastic.co/u/Thek10patil)
#### Post date: [April 21, 2020, 2:49am UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-termvector-api-in-python/228948/1 "2020-04-21T02:49:50Z")

</div>

I am currently working on Elasticsearch with a huge number of documents(around 500K) in an index. I want to store n-grams of each document's text data(This is also huge ~ per doc contains 2 pages of text data) in another index. So I calculating term vectors and their count in each document to store them in the new index. So I can execute aggregation queries on the new Index.

The setting of the old index has enabled me to execute [termvector](https://www.elastic.co/guide/en/elasticsearch/reference/current/term-vector.html) and [mtermvector](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-termvectors.html) API's. Due Elasticsearch server timeout issue most of the times mtermvectors API throwing Proxy error.

Some times I am getting expected response but most of the times I am getting the following error:

```auto
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /elastic/*indexname*/article/_mtermvectors.

Reason: Error reading from remote server

```

Sample mtermvector HTTP URL after calling mtermvector API in python

```auto
http://*servername*/elastic/*indexname*/article/_mtermvectors?offsets=false&fields=plain_text&ids=608467%2C608469%2C608473%2C608475%2C608477%2C608482%2C608485%2C608492%2C608498%2C608504%2C608509%2C608511%2C608520%2C608522%2C608528%2C608530%2C608541%2C608549%2C608562%2C608570%2C608573%2C608576%2C608577%2C608579%2C608585&field_statistics=true&term_statistics=true&payloads=false&positions=false

```

That's why I decided to use Termvector API.

```auto
elasticsearch_client.termvectors(index=INDEX_NAME, doc_type=DOC_TYPE, id=fetched_id,
                                            offsets=False,
                                            fields=["plain_text"],
                                            positions=False, payloads=False, term_statistics=True,
                                            field_statistics=False)

```

Sample termvector HTTP URL after calling mtermvector API in python

```auto
http:// **servername** /elastic/ **indexName** /article/608588/_termvectors?offsets=false&fields=plain_text&field_statistics=false&term_statistics=true&payloads=false&positions=false

```

**This request showing unexpected behaviour. I am not able to understand this. One request with HTTP request gives timeout error and next time with the same request it gives proper response**

**Index setting and mapping**

```auto
{
  "settings": {
    "analysis": {
      "analyzer": {
        "shingleAnalyzer": {
          "tokenizer": "letter_tokenizer",
          "filter": [
            "lowercase",
            "custom_stop",
            "custom_shingle",
            "custom_stemmer",
            "length_filter"
          ]
        }
      },
      "filter": {
        "custom_stemmer": {
          "type": "stemmer",
          "name": "english"
        },
        "custom_stop": {
          "type": "stop",
          "stopwords": "_english_"
        },
        "custom_shingle": {
          "type": "shingle",
          "min_shingle_size": "2",
          "max_shingle_size": "4",
          "filler_token":""
        },
        "length_filter": {
          "type": "length",
          "min": 2
        }
      },
      "tokenizer": {
        "letter_tokenizer": {
          "type": "letter"
        }
      }
    }
  },
  "mappings": {
    "properties": {"article_id":{"type": "text"},
      "plain_text": {
        "term_vector": "with_positions_offsets_payloads",
        "store": true,
        "analyzer": "shingleAnalyzer",
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

```

I don't think there is any problem with this setting and mapping as sometimes I am getting expected response.

**Please let me know if you need more information from my side. Any help will be appreciated.**

---

<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: [May 19, 2020, 2:49am UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-termvector-api-in-python/228948/2 "2020-05-19T02:49:56Z")

</div>

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