# How to optimize the term query?

**URL:** <https://discuss.elastic.co/t/how-to-optimize-the-term-query/178280>\
**Category:** Elasticsearch\
**Created:** [April 24, 2019, 3:01pm UTC](https://discuss.elastic.co/t/how-to-optimize-the-term-query/178280 "2019-04-24T15:01:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![huangjs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/huangjs/32/44738_2.png) [@huangjs](https://discuss.elastic.co/u/huangjs)\
**Post date:** [April 24, 2019, 3:01pm UTC](https://discuss.elastic.co/t/how-to-optimize-the-term-query/178280/1 "2019-04-24T15:01:10Z")

</div>

I used **term query** to search some entries and the search result contains 4714 entries, but it cost 7 min.  
My index setting and mapping look like:

```auto
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "my_tokenizer",
          "filter": [
            "length_filter"
          ]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "pattern",
          "pattern": "(?<=[EW])(?!R)",
          "lowercase": false
        }
      },
      "filter": {
        "length_filter": {
          "type": "length",
          "max": 70
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "dynamic": "strict",
      "properties": {
        "id": {
          "type": "keyword"
        },
        "sequence1": {
          "type": "text",
          "analyzer": "my_analyzer"
        },
        "sequence2": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}

```

the doc that needs to been inserted looks like:

```auto
{
    "id": "001",
    "sequence1": "AAAAAAAAAAAAABBBBBBBBBBBBBBBBBBDDDDDDDDDDDDDDDDDEGGGGGGGGGGGGGGGGGGGEGGGGGGGGGGGGGGGW",
    "sequence2": "AAAAAAAAAAAAABBBBBBBBBBBBBBBBBBDDDDDDDDDDDDDDDDDEGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGWWJHSACSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSYBIUIBBSAHBSJB AAAAAAAAAAAAAAAAAIOJIONMBJKSSSSSSSSSSSSSSSJKNJKNJUHIOHJKJTIBBFAFOBS DHUAGABSBIGUIGBHJBKNIOHIUHHHIOHIUGVAOAPPPPPPP"
}

```

my java search code is

```auto
SearchResponse response = esClient.prepareSearch("my_index")
                .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
                .setScroll(new TimeValue(60_000))
                .setQuery(QueryBuilders.matchPhraseQuery("sequence1, "GGGGGGGGGGGGGGGW"))
                .setFetchSource(null, new String[]{"sequence1", "sequence2"})
                .setSize(100)
                .get();
        do {
            for (SearchHit hit : response.getHits().getHits()) {
                  // there is nothing
            }
            response = esClient.prepareSearchScroll(response.getScrollId())
                    .setScroll(new TimeValue(60_000))
                    .get();
        } while (response.getHits().getHits().length != 0);

```

I create a line chart to check the time consumed per \*\* scroll\*\*. The x-axis of the chart is the number of **scroll** (0 is the first time running scroll). The y-axis is the time consumed.  
 ![2019-04-24_22-59](https://us1.discourse-cdn.com/elastic/original/3X/2/e/2e62d0e6410391cb746f8d830e082cfbe574d7cd.png)

The elasticsearch version is 6.6.1 and use the default settings. I only modified **jvm.options** ,

```auto
-Xms4g
-Xmx4g

```

The index size is 74.03GB and has 66,006,373 docs. (one shard and no replica)

---

<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 22, 2019, 3:01pm UTC](https://discuss.elastic.co/t/how-to-optimize-the-term-query/178280/2 "2019-05-22T15:01:12Z")

</div>

Closed on May 22, 2019, 3:01pm UTC

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