我已经有了。5.4.3
文件具有这样的映射:
mappings {
folders {
properties {
id {
type "long"
}
records {
properties {
name {
type "text"
}
}
}
}
}
}我有两个文件(“文件夹”):
[
{
id: 1,
records: [
{
name: "foo"
}
]
},
{
id: 2,
records: [
{
name: "foo bar"
}
]
}
]预期结果:如果正在请求"foo“(而不是两者),则只返回"foo”文件夹,如果请求"foo bar“,则只返回"foo bar”。
我尝试使用"match_phrase“查询,在"foo bar”的情况下,它工作得很好,但是如果我将它与"foo“ES一起使用,则会返回两个”文件夹“。
我的问题查询是:
{
"query": {
"bool": {
"must": {
"match_phrase": {
"records.name": "foo"
}
}
}
}
}如果请求"foo“,有谁知道是否有一些只返回id=1的选项?
发布于 2020-08-28 18:19:15
如果您的字段records.name为关键字类型,则可以使用术语查询。
如果索引映射中只有文本类型的records.name,最好将其keyword字段添加为多个字段,这样就可以使用records.name进行文本匹配,而将records.name.keyword用于精确匹配。
添加一个具有索引映射、数据和搜索查询的工作示例。(在Elasticsearch版本5.6中尝试了下面的搜索查询)
指数映射
{
"mappings": {
"doc": {
"properties": {
"records": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
}
}索引数据
{
"id": 1,
"records": [
{
"name": "foo"
}
]
}
{
"id": 2,
"records": [
{
"name": "foo bar"
}
]
}搜索查询
{
"query": {
"term": {
"records.name.keyword": "foo"
}
}
}搜索结果
"hits": [
{
"_index": "my_index",
"_type": "doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"id": 1,
"records": [
{
"name": "foo"
}
]
}
}
]https://stackoverflow.com/questions/63637094
复制相似问题