Elasticsearch ngram tokenizer returns 所有结果,无论查询输入如何

Elasticsearch ngram tokenizer returns all results regardless of query input

我正在尝试构建一个查询来搜索以下格式的记录:TR000002_1_2020.

用户应该能够通过以下方式搜索结果:

TR0000022_1_2020TR000002_1_20202020。我认为 ngram 标记化查询最适合我的需求。我使用的是 Elasticsearch 6.8,所以我无法使用 E7 中引入的内置搜索即键入。

这是我从文档 here 中得到的实现。我唯一修改的是 EdgeNGram -> NGram,因为用户可以从文本的任何位置进行搜索。

我的分析块如下所示:

.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
            })
        )
    )
)

然后在我的映射中我定义:

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

当我搜索 TR000002 时,我的查询 returns 所有结果,而不仅仅是包含这些特定字符的记录。我究竟做错了什么?对于这个特定用例,是否有更好的分词器?谢谢!

编辑:这是返回的示例:

{
  "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
        }
      },

此外,mapping 的显示内容如下:

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

我想我还应该提一下,我一直在控制台中测试这个查询,如下所示:

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

编辑 2:添加了来自索引 _settings 端点的 API 响应:

{
  "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"
        }
      }
    }
  }
}

因为我没有 JSON 格式的分析器设置访问权限,所以我无法确认,但很可能问题出在您的搜索分析器 autocomplete_search 上,它正在创建搜索时间标记正在匹配索引时间标记。

例如:您正在搜索 TR000002_1_2020 并且如果它创建 2020 作为标记,并且对于包含 TR000011_1_2020 的文档也会创建一个 2020 标记而不是您的查询将匹配它。

您可以使用 analyze API 根据分析器检查生成的标记,如前所述,大多数情况下有一些标记与上面显示的匹配。