在elasticsearch中搜索字段

时间:2018-06-10 10:50:48

标签: elasticsearch

我有一个类似于这个

的JSON文档
{"type":"line","line_id":89522,"play_name":"Taming of the Shrew","speech_number":19,"line_number":"2.1.58","speaker":"PETRUCHIO","text_entry":"To instruct her fully in those sciences,"}
{"index":{"_index":"shakespeare","_id":89522}}
{"type":"line","line_id":89523,"play_name":"Taming of the Shrew","speech_number":19,"line_number":"2.1.59","speaker":"PETRUCHIO","text_entry":"Whereof I know she is not ignorant:"}
{"index":{"_index":"shakespeare","_id":89523}}
{"type":"line","line_id":89524,"play_name":"Taming Day","speech_number":19,"line_number":"2.1.60","speaker":"PETRUCHIO","text_entry":"Accept of him, or else you do me wrong:"}
{"index":{"_index":"shakespeare","_id":89524}}

我正在尝试对play_name进行搜索,只要play_name包含“Taming”,它就会返回。

我的查询就像这样

GET /shakespeare/doc/_search
{
  "query":{
    "match":{"play_name":"Taming"}
  }

}

HOwever,事情是它不会返回play_name包含Taming的所有结果。我阅读了文档,看起来这应该有效。因此,任何人都可以告诉我我做错了什么?

PS:这是我的映射设置

{
  "shakespeare": {
    "mappings": {
      "doc": {
        "properties": {
          "line_id": {
            "type": "integer"
          },
          "line_number": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "play_name": {
            "type": "keyword"
          },
          "speaker": {
            "type": "keyword"
          },
          "speech_number": {
            "type": "integer"
          },
          "text_entry": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:1)

修改

试试这样:

"query": {
        "wildcard" : {
             "play_name":"*Taming*" 
           }
 }    

答案 1 :(得分:0)

您应该将play_name字段的映射更改为"type": "text"。如果您打算搜索完全匹配,您仍然可以包含关键字映射。

play_name的最终映射应如下所示:

      "play_name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }