elasticsearch ngram标记器返回所有结果,而不考虑查询输入

kgqe7b3p  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(337)

我正在尝试构建一个查询以搜索以下格式的记录: TR000002_1_2020 .
用户应该能够通过以下方式搜索结果: TR000002 或者 2_1_2020 或者 TR000002_1_2020 或者 2020 . 我认为ngram标记化查询最适合我的需要。我使用的是elasticsearch 6.8,因此无法使用e7中引入的内置搜索。
这是我的实现,我遵循这里的文档。我唯一修改的是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
            })
        )
    )
)

然后在我的Map中我定义:

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

当我寻找 TR000002 ,我的查询将返回所有结果,而不仅仅是包含这些特定字符的记录。我做错什么了?对于这个特定的用例有更好的标记器吗?谢谢!
编辑:以下是返回内容的示例:

{
  "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:添加了来自索引的api响应 _settings 终结点:

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

gab6jxml1#

由于我没有分析器设置json格式的访问权限,我无法确认,但最可能的问题是您的搜索分析器 autocomplete_search 它正在创建与索引时间标记匹配的搜索时间标记。
例如:您正在搜索 TR000002_1_2020 如果它创造了 2020 作为一个标记和包含 TR000011_1_2020 还创建了 2020 比您的查询匹配的令牌。
您可以使用analyze api检查基于分析器生成的令牌,如前所述,大部分令牌是匹配的,如上图所示。

相关问题