Elasticsearch在不应该匹配的情况下匹配子字符串

时间:2017-08-03 15:21:03

标签: elasticsearch dsl

这是我正在运行的get请求:

curl -XGET https://search-mycluster-xxxxxxxxxxxxxxxxxxxxxx.us-east-2.es.amazonaws.com/test/_search? -d '
{ "size": 1,
     "_source": ["url"],
    "query" :
    { "match": { "circular": "NC-xx-xxxx-y"} }

}
'

它工作正常,如果我将圆形从“NC-xx-xxxx-y”更改为“didntwork”,则不会按预期返回任何结果。如果我将其更改为“NC”(原始的子串),则会显示“NC-xx-xxxx-y”的结果。即使我制作圆形“NC-xx-xxxx-ya”结果为“NC-xx-xxxx-y”。如果循环正好是“NC-xx-xxxx-y”,我只希望查询起作用。有任何想法如何更改此查询?

这是我的映射:

{
   "test" : {
      "mappings" : {
         "files" : {
            "properties" : {
               "submitted_by" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "effective_date" : {
                  "type" : "date",
                  "fields" : {
                     "raw" : {
                        "type" : "date"
                     }
                  }
               },
               "date_filed" : {
                  "type" : "date",
                  "fields" : {
                     "raw" : {
                        "type" : "date"
                     }
                  }
               },
               "date" : {
                  "fields" : {
                     "keyword" : {
                        "ignore_above" : 256,
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "form" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "topic" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "circular" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "profit_center" : {
                  "type" : "text",
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  }
               },
               "url" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "circular_link" : {
                  "type" : "text",
                  "fields" : {
                     "keyword" : {
                        "type" : "keyword",
                        "ignore_above" : 256
                     }
                  }
               },
               "subject" : {
                  "type" : "text",
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  }
               },
               "form_title" : {
                  "type" : "text",
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  }
               },
               "state" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "lob" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "contractor" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "link_rate_form" : {
                  "type" : "text",
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  }
               },
               "product_filing" : {
                  "type" : "text",
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  }
               },
               "status" : {
                  "fields" : {
                     "raw" : {
                        "type" : "keyword"
                     }
                  },
                  "type" : "text"
               },
               "date_entered" : {
                  "type" : "date",
                  "fields" : {
                     "raw" : {
                        "type" : "date"
                     }
                  }
               }
            }
         }
      }
   }
}

1 个答案:

答案 0 :(得分:0)

您在索引和搜索时使用标准分析器,因此搜索词“NC-xx-xxxx-y”被破坏为[“nc”,“xx”,“xxxx”,“y]。如果你想要完全匹配你可以使用术语查询。如果它将用于过滤查询,它应该更快一点。

{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "circular.raw" : "NC-xx-xxxx-y" } 
            }
        }
    }
}

编辑:将.raw添加到循环中以搜索关键字。

https://www.elastic.co/guide/en/elasticsearch/guide/current/term-vs-full-text.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html