ElasticSearch - 如何在输出结果中返回输入术语条件

时间:2017-09-19 07:39:34

标签: elasticsearch

我正在尝试使用regexp过滤器在elasticsearch中进行搜索。以下是我的询问:

{
  "from": 0,
  "size": 10,
  "_source":["CODE"],
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [ 
                {
                   "regexp" : {
                      "CODE" : {
                        "value" : "[0]?[0]?[0]?[0]?3410086456[0-9]?",
                        "flags_value" : 0,
                        "boost" : 20.0
                      }
                    }
                },
                {
                   "regexp" : {
                      "CODE" : {
                        "value" : "[0]?[0]?[0]?[0]?83560900204[0-9]?",
                        "flags_value" : 0,
                        "boost" : 20.0
                      }
                    }
                }
            ]
          }
        },
        {
            "terms": {
                "CODETYPE": [
                     "TYPE1", "TYPE2", "TYPE3"
                ]
            }
        }
      ]
    }
  }
}

以下是查询结果:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 20.091797,
        "hits": [
            {
                "_index": "index1",
                "_type": "type1",
                "_id": "142242",
                "_score": 20.091797,
                "_source": {
                    "CODE": "003410086456"
                }
            },
            {
                "_index": "index1",
                "_type": "type1",
                "_id": "375897",
                "_score": 20.091797,
                "_source": {
                    "CODE": "083560900204"
                }
            }
        ]
    }
}

我需要在输出中另外获得的是每个结果与之匹配的输入项。像这样:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 20.091797,
        "hits": [
            {
                "_index": "index1",
                "_type": "type1",
                "_id": "142242",
                "_score": 20.091797,
                "_source": {
                    "CODE": "003410086456",
                    "INPUT": "3410086456"
                }
            },
            {
                "_index": "index1",
                "_type": "type1",
                "_id": "375897",
                "_score": 20.091797,
                "_source": {
                    "CODE": "083560900204",
                    "INPUT": "83560900204"
                }
            }
        ]
    }
}

请注意上面的其他 INPUT 字段。这样我可以映射哪些模式映射到哪个结果。在弹性搜索中我有可能做到这一点吗?我目前无法找到实现这一目标的任何方法。

感谢您对此的帮助。如果我需要提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

您可以使用突出显示,虽然它不会在_source中获胜,但它会创建一个单独的字段highlight,它会给出字段值。

{
  "from": 0,
  "size": 10,
  "_source": [
    "CODE"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "regexp": {
                  "CODE": {
                    "value": "[0]?[0]?[0]?[0]?3410086456[0-9]?",
                    "flags_value": 0,
                    "boost": 20
                  }
                }
              },
              {
                "regexp": {
                  "CODE": {
                    "value": "[0]?[0]?[0]?[0]?83560900204[0-9]?",
                    "flags_value": 0,
                    "boost": 20
                  }
                }
              }
            ]
          }
        },
        {
          "terms": {
            "CODETYPE": [
              "TYPE1",
              "TYPE2",
              "TYPE3"
            ]
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "CODE": {}
    }
  }
}

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#search-request-highlighting