如何使用dsl查询匹配elasticsearch中的精确文档数据?

btqmn9zl  于 2021-07-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(376)

我的标记器

"tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit"
          ]
        }

我正试图基于此字段搜索值,但这里的问题是,无论何时,我都希望基于标记进行搜索,例如,假设我使用s标记进行搜索,那么我应该得到与s匹配或开始于s的项,现在如果我使用sp进行搜索,我希望得到从sp开始的项,放弃其他项,我只想得到以sp开始的值并放弃所有,我没有得到是我的查询错误还是我使用的过滤器错误有人能帮我这个吗

{
     "query": {
      "bool": {
       "must": [
        {
         "multi_match": {
          "query": "PRODUCT",
          "fields": [
           "hierarchy_name",
           "attribute_name"
          ]
         }
        },
        {
         "multi_match": {
          "query": "SUB_FAMILY",
          "fields": [
           "hierarchy_name",
           "attribute_name"
          ]
         }
        },
        {
         "match": {
          "item_pk": "SP"
         }
        }
       ]
      }
     }
    }

此查询的输出为

"hits": [
                {
                    "_index": "logs_datas",
                    "_type": "_doc",
                    "_id": "H1PfEnkBQXpKNrJSp8bV",
                    "_score": 9.418445,
                    "_source": {
                        "message": "PRODUCT,SUB_FAMILY,SPRINHO2H",
                        "path": "/home/niteshb/elasticsearchDatas.csv",
                        "hierarchy_name": "PRODUCT",
                        "@version": "1",
                        "@timestamp": "2021-04-27T10:28:37.578Z",
                        "host": "ewiglp71",
                        "item_pk": "SPRINHO2H",
                        "attribute_name": "SUB_FAMILY"
                    }
                },
                {
                    "_index": "logs_datas",
                    "_type": "_doc",
                    "_id": "y1PfEnkBQXpKNrJSp8XQ",
                    "_score": 5.3059187,
                    "_source": {
                        "message": "PRODUCT,SUB_FAMILY,SCMLPLWVI",
                        "path": "/home/niteshb/elasticsearchDatas.csv",
                        "hierarchy_name": "PRODUCT",
                        "@version": "1",
                        "@timestamp": "2021-04-27T10:28:37.577Z",
                        "host": "ewiglp71",
                        "item_pk": "SCMLPLWVI",
                        "attribute_name": "SUB_FAMILY"
                    }
                },
                {
                    "_index": "logs_datas",
                    "_type": "_doc",
                    "_id": "zFPfEnkBQXpKNrJSp8XQ",
                    "_score": 5.3059187,
                    "_source": {
                        "message": "PRODUCT,SUB_FAMILY,SSVRKEN2Z",
                        "path": "/home/niteshb/elasticsearchDatas.csv",
                        "hierarchy_name": "PRODUCT",
                        "@version": "1",
                        "@timestamp": "2021-04-27T10:28:37.579Z",
                        "host": "ewiglp71",
                        "item_pk": "SSVRKEN2Z",
                        "attribute_name": "SUB_FAMILY"
                    }
                }
                }
            ]
        }
    }
a11xaf1n

a11xaf1n1#

自从 min_gram 为1,因此生成的令牌 SCMLPLWVI

{
  "tokens": [
    {
      "token": "S",
      "start_offset": 0,
      "end_offset": 1,
      "type": "word",
      "position": 0
    },
    {
      "token": "SC",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 1
    },
    {
      "token": "SCM",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 2
    },
    {
      "token": "SCML",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 3
    },
    {
      "token": "SCMLP",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 4
    },
    {
      "token": "SCMLPL",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 5
    },
    {
      "token": "SCMLPLW",
      "start_offset": 0,
      "end_offset": 7,
      "type": "word",
      "position": 6
    },
    {
      "token": "SCMLPLWV",
      "start_offset": 0,
      "end_offset": 8,
      "type": "word",
      "position": 7
    },
    {
      "token": "SCMLPLWVI",
      "start_offset": 0,
      "end_offset": 9,
      "type": "word",
      "position": 8
    }
  ]
}

如果你想从 sp 然后您需要修改您的标记器

"tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,          // note this
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit"
          ]
        }

更新1:
您可以使用match\u bool\u前缀来搜索以 s 或者 sp 添加工作示例
索引Map:

{
  "mappings": {
    "properties": {
      "item_pk": {
        "type": "text"
      }
    }
  }
}

搜索查询1:

{
  "query": {
    "match_bool_prefix" : {
      "item_pk" : "s"
    }
  }
}

搜索结果将是

"hits": [
      {
        "_index": "67281810",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "message": "PRODUCT,SUB_FAMILY,SPRINHO2H",
          "path": "/home/niteshb/elasticsearchDatas.csv",
          "hierarchy_name": "PRODUCT",
          "@version": "1",
          "@timestamp": "2021-04-27T10:28:37.578Z",
          "host": "ewiglp71",
          "item_pk": "SPRINHO2H",
          "attribute_name": "SUB_FAMILY"
        }
      },
      {
        "_index": "67281810",
        "_type": "_doc",
        "_id": "i7quE3kB6jKCA-nFYii6",
        "_score": 1.0,
        "_source": {
          "message": "PRODUCT,SUB_FAMILY,SCMLPLWVI",
          "path": "/home/niteshb/elasticsearchDatas.csv",
          "hierarchy_name": "PRODUCT",
          "@version": "1",
          "@timestamp": "2021-04-27T10:28:37.577Z",
          "host": "ewiglp71",
          "item_pk": "SCMLPLWVI",
          "attribute_name": "SUB_FAMILY"
        }
      },
      {
        "_index": "67281810",
        "_type": "_doc",
        "_id": "jLquE3kB6jKCA-nFgiju",
        "_score": 1.0,
        "_source": {
          "message": "PRODUCT,SUB_FAMILY,SSVRKEN2Z",
          "path": "/home/niteshb/elasticsearchDatas.csv",
          "hierarchy_name": "PRODUCT",
          "@version": "1",
          "@timestamp": "2021-04-27T10:28:37.579Z",
          "host": "ewiglp71",
          "item_pk": "SSVRKEN2Z",
          "attribute_name": "SUB_FAMILY"
        }
      }
    ]

搜索查询2:

{
  "query": {
    "match_bool_prefix" : {
      "item_pk" : "sp"
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "67281810",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "message": "PRODUCT,SUB_FAMILY,SPRINHO2H",
          "path": "/home/niteshb/elasticsearchDatas.csv",
          "hierarchy_name": "PRODUCT",
          "@version": "1",
          "@timestamp": "2021-04-27T10:28:37.578Z",
          "host": "ewiglp71",
          "item_pk": "SPRINHO2H",
          "attribute_name": "SUB_FAMILY"
        }
      }
    ]

相关问题