【问题标题】:Need help to correctly perform wildcard search on a field需要帮助才能在字段上正确执行通配符搜索
【发布时间】:2020-05-13 10:56:49
【问题描述】:

我的sData.Name 映射如下:

{
    "abc_history": {
        "mappings": {
            "abc-data-type": {
                "sData.Name": {
                    "full_name": "sData.Name",
                    "mapping": {
                        "Name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我的sData.startDate 映射如下所示

{
    "abc_history": {
        "mappings": {
            "abc-data-type": {
                "sData.startDate": {
                    "full_name": "sData.startDate",
                    "mapping": {
                        "startDate": {
                            "type": "date"
                        }
                    }
                }
            }
        }
    }
}

我正在尝试对 sData.Name 执行通配符搜索并使用以下查询:

{
  "from": 0, 
  "size": 20, 
        "query": {
          "bool": {
            "must":[
              {"range": {"requestDate": { "gte": "2019-10-01T08:00:00.000Z" }}},
              {
                "wildcard": {
                  "sData.Name": "*Scream*"
                }
              }
            ]
          }
        },
        "sort": [
          { "requestDate": {"order": "desc"}}
        ]
}

上述查询返回空响应。 我应该如何修改我的查询,以便我可以在 sData.Name 上执行 wildcard search

http://{serverhost}:{port}/abc_history/_search 的回复如下:

{
  "took": 181,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "abc_history",
        "_type": "abc-data-type",
        "_id": "5e29cbb7965809fe6cb22a7b",
        "_score": null,
        "_source": {
          "sData": [
            {
              "status": "ASSIGNED",
              "Name": "CloudView abcmission Automation Support",
              startDate : "2020-01-26T20:12:57.091Z"

            },
            {
              "status": "RESOLVED",
              "Name": "DSE - Tools Engineering",
              startDate : "2020-01-27T20:12:57.091Z" 
            },
            {
              "status": "CLOSED",
              "Name": "abcmission Orchestration",
              startDate : "2020-01-29T20:12:57.091Z"
            },
            {
              "status": "ASSIGNED",
              "Name": "CloudView abcmission Automation Support",
              startDate : "2020-01-29T20:19:29.687Z"
            }
          ]
        },
        "sort": [
          1579797431366
        ]
      }
    ]
  }
}

我主要关心查询sData.Name。我只想在最后一个数组元素中执行搜索。所以就我而言,我只想搜索sData[3].Name 换句话说,关键字DSE 应该只在"Name": "CloudView abcmission Automation Support" 内搜索

【问题讨论】:

    标签: elasticsearch lucene elastic-stack elasticsearch-5


    【解决方案1】:

    我尝试根据您的输入创建索引。尝试使用

    "wildcard": {
        "sData.Name.keyword": {
        "wildcard": "*DSE*",
            "boost": 1
        }
    }
    
    
    

    完整的查询是:

    
    
    PUT /abc_history
    {
      "mappings": {
        "abc-data-type": {
          "properties": {
            "sData": {
              "properties": {
                "status": {
                  "type": "keyword"
                },
                "Name": {
                  "type": "text",
                  "fields": {
                         "keyword": {
                              "type": "keyword",
                              "ignore_above": 256
                            }
                    }
                }
              }
            }
          }
        }
      }
    }
    
    
    GET /abc_history/_search
    {
        "from": 0,
        "size": 200,
        "query": {
            "bool": {
                "filter": [
                    {
                        "bool": {
                            "must": [
                                {
                                    "wildcard": {
                                        "sData.Name.keyword": {
                                            "wildcard": "*DSE*",
                                            "boost": 1
                                        }
                                    }
                                }
                            ],
                            "adjust_pure_negative": true,
                            "boost": 1
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1
            }
        }
    }
    
    
    

    可能

    
    
    GET /abc_history/_search
    {
        "from": 0,
        "size": 200,
        "query": {
            "bool": {
                "filter": [
                    {
                        "bool": {
                            "must": [
                                {
                                    "wildcard": {
                                        "sData.Name": {
                                            "wildcard": "*ddd*",
                                            "boost": 1
                                        }
                                    }
                                }
                            ],
                            "adjust_pure_negative": true,
                            "boost": 1
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1
            }
        },
        "sort": [
            {
                "sData.startDate": {
                    "order": "asc"
                }
            }
        ]
    }
    
    

    【讨论】:

    • 这不起作用,仍然给出空结果
    • 请在此处粘贴您的索引创建脚本。
    • 您是指用于创建abc_history 索引的脚本吗?我只有_search 查询
    • 你有像kibana这样的查询控制台吗?
    • 不,我们没有查询控制台。我可以使用postman 运行查询,例如_search_mappings
    猜你喜欢
    • 2016-10-24
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2021-12-20
    • 2013-12-26
    • 2011-10-17
    • 2014-05-20
    相关资源
    最近更新 更多