elasticsearchnestelasticsearch-6

Elasticsearch ngram tokenizer returns all results regardless of query input


I am trying to build a query to search for records in the following format: TR000002_1_2020.

Users should be able to search for results the following ways:

TR000002 or 2_1_2020 or TR000002_1_2020 or 2020. I figured an ngram tokenization query would be best suited for my needs. I am using Elasticsearch 6.8 so I cannot use the built in Search-As-You-Type introduced in E7.

Here's my implementation I followed from docs here. The only thing I modified was EdgeNGram -> NGram as the user can search from any point of the text.

My Analysis block looks like this:

.Analysis(a => a
    .Analyzers(aa => aa
        .Custom("autocomplete", ca => ca
            .Tokenizer("autocomplete")
            .Filters(new string[] {
                "lowercase"
            })
        )
        .Custom("autocomplete_search", ca => ca
            .Tokenizer("lowercase")
        )
    )
    .Tokenizers(t => t
        .NGram("autocomplete", e => e
            .MinGram(2)
            .MaxGram(16)
            .TokenChars(new TokenChar[] {
                TokenChar.Letter,
                TokenChar.Digit,
                TokenChar.Punctuation,
                TokenChar.Symbol
            })
        )
    )
)

Then in my mapping I define:

.Text(t => t
    .Name(tr => tr.TestRecordId)
    .Analyzer("autocomplete")
    .SearchAnalyzer("autocomplete_search")
)

When I search for TR000002, my query returns all results instead of just the records that contain those specific characters. What am I doing wrong? Is there a better tokenizer for this specific use case? Thanks!

EDIT: Here's a sample of what is returned:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 27,
    "max_score" : 0.105360515,
    "hits" : [
      {
        "_index" : "test-records-development-09-09-2020-02-00-00",
        "_type" : "testrecorddto",
        "_id" : "3",
        "_score" : 0.105360515,
        "_source" : {
          "id" : 3,
          "testRecordId" : "TR000002_1_2020",
          "type" : 0,
          "typeName" : "TIDCo60",
          "missionId" : 1,
          "mission" : {
            "missionId" : 1,
            "name" : "[REDACTED]",
            "mRPLUsername" : "[REDACTED]",
            "missionRadiationPartsLead" : {
              "username" : "[REDACTED]",
              "displayName" : "[REDACTED]"
            },
            "missionInstruments" : [
              {
                "missionId" : 1,
                "instrumentId" : 1,
                "cognizantEngineerUsername" : "[REDACTED]",
                "instrument" : {
                  "intstrumentId" : 1,
                  "name" : "Instrument"
                },
                "cognizantEngineer" : {
                  "username" : "[REDACTED]",
                  "displayName" : "[REDACTED]"
                }
              },
              {
                "missionId" : 1,
                "instrumentId" : 2,
                "instrument" : {
                  "intstrumentId" : 2,
                  "name" : "Instrument 2"
                }
              }
            ]
          },
          "procurementPartId" : 2,
          "procurementPart" : {
            "procurementPartId" : 2,
            "partNumber" : "procurement part",
            "part" : {
              "partId" : 1,
              "manufacturer" : "Texas Instruments",
              "genericPartNumber" : "123",
              "description" : "description",
              "partTechnology" : "Part Tech"
            }
          },
          "testStatusId" : 12,
          "testStatus" : {
            "testStatusId" : 12,
            "name" : "Complete: Postponed Until Further Notice"
          },
          "discriminator" : "SingleEventEffectsRecord",
          "testRecordServiceOrders" : [
            {
              "testRecordId" : 3,
              "serviceOrderId" : 9,
              "serviceOrder" : {
                "serviceOrderId" : 9,
                "serviceOrderNumber" : "105702"
              }
            }
          ],
          "rtdbFiles" : [ ],
          "personnelGroups" : [
            {
              "personnelGroupUsers" : [ ]
            },
            {
              "personnelGroupUsers" : [ ]
            }
          ],
          "testRecordTestSubTypes" : [ ],
          "testRecordTestFacilityConditions" : [ ],
          "testRecordFollowers" : [ ],
          "isDeleted" : false,
          "sEETestRates" : [ ]
        }
      },
      {
        "_index" : "test-records-development-09-09-2020-02-00-00",
        "_type" : "testrecorddto",
        "_id" : "11",
        "_score" : 0.105360515,
        "_source" : {
          "id" : 11,
          "testRecordId" : "TR000011_1_2020",
          "type" : 0,
          "typeName" : "TIDCo60",
          "missionId" : 1,
          "mission" : {
            "missionId" : 1,
            "name" : "[REDACTED]",
            "mRPLUsername" : "[REDACTED]",
            "missionRadiationPartsLead" : {
              "username" : "[REDACTED]",
              "displayName" : "[REDACTED]"
            },
            "missionInstruments" : [
              {
                "missionId" : 1,
                "instrumentId" : 1,
                "cognizantEngineerUsername" : "[REDACTED]",
                "instrument" : {
                  "intstrumentId" : 1,
                  "name" : "Instrument"
                },
                "cognizantEngineer" : {
                  "username" : "[REDACTED]",
                  "displayName" : "[REDACTED]"
                }
              },
              {
                "missionId" : 1,
                "instrumentId" : 2,
                "instrument" : {
                  "intstrumentId" : 2,
                  "name" : "Instrument 2"
                }
              }
            ]
          },
          "procurementPartId" : 2,
          "procurementPart" : {
            "procurementPartId" : 2,
            "partNumber" : "procurement part",
            "part" : {
              "partId" : 1,
              "manufacturer" : "Texas Instruments",
              "genericPartNumber" : "123",
              "description" : "description",
              "partTechnology" : "Part Tech"
            }
          },
          "testStatusId" : 1,
          "testStatus" : {
            "testStatusId" : 1,
            "name" : "Active"
          },
          "discriminator" : "TotalIonizingDoseRecord",
          "creatorUsername" : "[REDACTED]",
          "creator" : {
            "username" : "[REDACTED]",
            "displayName" : "[REDACTED]"
          },
          "testRecordServiceOrders" : [ ],
          "partLDC" : "12",
          "waferLot" : "1",
          "rtdbFiles" : [ ],
          "personnelGroups" : [
            {
              "personnelGroupUsers" : [ ]
            }
          ],
          "testRecordTestSubTypes" : [ ],
          "testRecordTestFacilityConditions" : [ ],
          "testRecordFollowers" : [ ],
          "isDeleted" : false,
          "testStartDate" : "2020-07-30T00:00:00",
          "actualCompletionDate" : "2020-07-31T00:00:00"
        }
      },
      {
        "_index" : "test-records-development-09-09-2020-02-00-00",
        "_type" : "testrecorddto",
        "_id" : "17",
        "_score" : 0.105360515,
        "_source" : {
          "id" : 17,
          "testRecordId" : "TR000017_1_2020",
          "type" : 0,
          "typeName" : "TIDCo60",
          "missionId" : 1,
          "mission" : {
            "missionId" : 1,
            "name" : "[REDACTED]",
            "mRPLUsername" : "[REDACTED]",
            "missionRadiationPartsLead" : {
              "username" : "[REDACTED]",
              "displayName" : "[REDACTED]"
            },
            "missionInstruments" : [
              {
                "missionId" : 1,
                "instrumentId" : 1,
                "cognizantEngineerUsername" : "[REDACTED]",
                "instrument" : {
                  "intstrumentId" : 1,
                  "name" : "Instrument"
                },
                "cognizantEngineer" : {
                  "username" : "lewallen",
                  "displayName" : "[REDACTED]"
                }
              },
              {
                "missionId" : 1,
                "instrumentId" : 2,
                "instrument" : {
                  "intstrumentId" : 2,
                  "name" : "Instrument 2"
                }
              }
            ]
          },
          "procurementPartId" : 2,
          "procurementPart" : {
            "procurementPartId" : 2,
            "partNumber" : "procurement part",
            "part" : {
              "partId" : 1,
              "manufacturer" : "Texas Instruments",
              "genericPartNumber" : "123",
              "description" : "description",
              "partTechnology" : "Part Tech"
            }
          },
          "testStatusId" : 1,
          "testStatus" : {
            "testStatusId" : 1,
            "name" : "Active"
          },
          "discriminator" : "TotalIonizingDoseRecord",
          "creatorUsername" : "[REDACTED]",
          "creator" : {
            "username" : "[REDACTED]",
            "displayName" : "[REDACTED]"
          },
          "testRecordServiceOrders" : [ ],
          "rtdbFiles" : [ ],
          "personnelGroups" : [
            {
              "personnelGroupUsers" : [ ]
            }
          ],
          "testRecordTestSubTypes" : [ ],
          "testRecordTestFacilityConditions" : [ ],
          "testRecordFollowers" : [ ],
          "isDeleted" : false
        }
      },

Also here's what shows for mapping:

"testRecordId" : {
  "type" : "text",
  "analyzer" : "autocomplete",
  "search_analyzer" : "autocomplete_search"
},

I guess I should also mention, I've been testing this query in the console like so:

GET test-records-development/_search
{
  "query": {
    "match": {
      "testRecordId": {
        "query": "TR000002_1_2020"
      }
    }
  }
}

EDIT 2: Added API response from index _settings endpoint:

{
  "test-records-development-09-09-2020-02-00-00" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "5",
        "provided_name" : "test-records-development-09-09-2020-02-00-00",
        "creation_date" : "1599617013874",
        "analysis" : {
          "analyzer" : {
            "autocomplete" : {
              "filter" : [
                "lowercase"
              ],
              "type" : "custom",
              "tokenizer" : "autocomplete"
            },
            "autocomplete_search" : {
              "type" : "custom",
              "tokenizer" : "lowercase"
            }
          },
          "tokenizer" : {
            "autocomplete" : {
              "token_chars" : [
                "letter",
                "digit",
                "punctuation",
                "symbol"
              ],
              "min_gram" : "2",
              "type" : "ngram",
              "max_gram" : "16"
            }
          }
        },
        "number_of_replicas" : "0",
        "uuid" : "FSeCa0YwRCOJVbjfxYGkig",
        "version" : {
          "created" : "6080199"
        }
      }
    }
  }
}

Solution

  • As I don't have the analyzer setting access in JSON format,I can't confirm it but most probably issue is with your search analyzer autocomplete_search which is creating search time tokens which are matching the index time tokens.

    For example: you are searching for TR000002_1_2020 and if it creates 2020 as a token and for document containing TR000011_1_2020 also creates a 2020 token than your query will match it.

    You can use the analyze API to check the generated tokens based on a analyzer and as mentioned earlier mostly there is some tokens which are matching as shown above.