New

The executive guide to generative AI

Read more

Term 查询

编辑

返回在提供的字段中包含精确词项的文档。

您可以使用 term 查询来查找基于精确值的文档,例如价格、产品 ID 或用户名。

避免将 term 查询用于 text 字段。

默认情况下,Elasticsearch 会在 分析过程中更改 text 字段的值。 这会使查找 text 字段值的精确匹配项变得困难。

要搜索 text 字段值,请改用 match 查询。

示例请求

编辑
resp = client.search(
    query={
        "term": {
            "user.id": {
                "value": "kimchy",
                "boost": 1
            }
        }
    },
)
print(resp)
response = client.search(
  body: {
    query: {
      term: {
        'user.id' => {
          value: 'kimchy',
          boost: 1
        }
      }
    }
  }
)
puts response
const response = await client.search({
  query: {
    term: {
      "user.id": {
        value: "kimchy",
        boost: 1,
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "term": {
      "user.id": {
        "value": "kimchy",
        "boost": 1.0
      }
    }
  }
}

term 的顶级参数

编辑
<field>
(必需,对象)您希望搜索的字段。

<field> 的参数

编辑
value
(必需,字符串)您希望在提供的 <field> 中查找的词项。 要返回文档,该词项必须与字段值完全匹配,包括空格和大小写。
boost

(可选,浮点数)用于降低或增加查询的相关性评分的浮点数。 默认为 1.0

您可以使用 boost 参数来调整包含两个或多个查询的搜索的相关性评分。

Boost 值相对于默认值 1.0。介于 01.0 之间的 boost 值会降低相关性评分。大于 1.0 的值会增加相关性评分。

case_insensitive [7.10.0] 在 7.10.0 中添加。
(可选,布尔值)当设置为 true 时,允许值与索引字段值进行 ASCII 大小写不敏感匹配。 默认值为 false,这意味着匹配的大小写敏感性取决于底层字段的映射。

注意事项

编辑

避免将 term 查询用于 text 字段

编辑

默认情况下,Elasticsearch 在分析期间会更改 text 字段的值。 例如,默认的 标准分析器 按如下方式更改 text 字段值

  • 删除大多数标点符号
  • 将剩余内容分成称为词条的各个单词
  • 将词条转换为小写

为了更好地搜索 text 字段,match 查询还会在执行搜索之前分析您提供的搜索词。 这意味着 match 查询可以搜索 text 字段中的已分析词条,而不是精确词项。

term 查询分析搜索词。term 查询仅搜索您提供的精确词项。 这意味着在搜索 text 字段时,term 查询可能会返回较差的结果或没有结果。

要查看搜索结果的差异,请尝试以下示例。

  1. 创建一个带有名为 full_texttext 字段的索引。

    resp = client.indices.create(
        index="my-index-000001",
        mappings={
            "properties": {
                "full_text": {
                    "type": "text"
                }
            }
        },
    )
    print(resp)
    response = client.indices.create(
      index: 'my-index-000001',
      body: {
        mappings: {
          properties: {
            full_text: {
              type: 'text'
            }
          }
        }
      }
    )
    puts response
    res, err := es.Indices.Create(
    	"my-index-000001",
    	es.Indices.Create.WithBody(strings.NewReader(`{
    	  "mappings": {
    	    "properties": {
    	      "full_text": {
    	        "type": "text"
    	      }
    	    }
    	  }
    	}`)),
    )
    fmt.Println(res, err)
    const response = await client.indices.create({
      index: "my-index-000001",
      mappings: {
        properties: {
          full_text: {
            type: "text",
          },
        },
      },
    });
    console.log(response);
    PUT my-index-000001
    {
      "mappings": {
        "properties": {
          "full_text": { "type": "text" }
        }
      }
    }
  2. full_text 字段中索引一个值为 Quick Brown Foxes! 的文档。

    resp = client.index(
        index="my-index-000001",
        id="1",
        document={
            "full_text": "Quick Brown Foxes!"
        },
    )
    print(resp)
    response = client.index(
      index: 'my-index-000001',
      id: 1,
      body: {
        full_text: 'Quick Brown Foxes!'
      }
    )
    puts response
    res, err := es.Index(
    	"my-index-000001",
    	strings.NewReader(`{
    	  "full_text": "Quick Brown Foxes!"
    	}`),
    	es.Index.WithDocumentID("1"),
    	es.Index.WithPretty(),
    )
    fmt.Println(res, err)
    const response = await client.index({
      index: "my-index-000001",
      id: 1,
      document: {
        full_text: "Quick Brown Foxes!",
      },
    });
    console.log(response);
    PUT my-index-000001/_doc/1
    {
      "full_text":   "Quick Brown Foxes!"
    }

    因为 full_text 是一个 text 字段,所以在分析期间,Elasticsearch 将 Quick Brown Foxes! 更改为 [quick, brown, fox]

  3. 使用 term 查询在 full_text 字段中搜索 Quick Brown Foxes!。 包括 pretty 参数,以便响应更易于阅读。

    resp = client.search(
        index="my-index-000001",
        pretty=True,
        query={
            "term": {
                "full_text": "Quick Brown Foxes!"
            }
        },
    )
    print(resp)
    response = client.search(
      index: 'my-index-000001',
      pretty: true,
      body: {
        query: {
          term: {
            full_text: 'Quick Brown Foxes!'
          }
        }
      }
    )
    puts response
    res, err := es.Search(
    	es.Search.WithIndex("my-index-000001"),
    	es.Search.WithBody(strings.NewReader(`{
    	  "query": {
    	    "term": {
    	      "full_text": "Quick Brown Foxes!"
    	    }
    	  }
    	}`)),
    	es.Search.WithPretty(),
    )
    fmt.Println(res, err)
    const response = await client.search({
      index: "my-index-000001",
      pretty: "true",
      query: {
        term: {
          full_text: "Quick Brown Foxes!",
        },
      },
    });
    console.log(response);
    GET my-index-000001/_search?pretty
    {
      "query": {
        "term": {
          "full_text": "Quick Brown Foxes!"
        }
      }
    }

    由于 full_text 字段不再包含精确词项 Quick Brown Foxes!term 查询搜索不返回任何结果。

  4. 使用 match 查询在 full_text 字段中搜索 Quick Brown Foxes!

    resp = client.search(
        index="my-index-000001",
        pretty=True,
        query={
            "match": {
                "full_text": "Quick Brown Foxes!"
            }
        },
    )
    print(resp)
    response = client.search(
      index: 'my-index-000001',
      pretty: true,
      body: {
        query: {
          match: {
            full_text: 'Quick Brown Foxes!'
          }
        }
      }
    )
    puts response
    res, err := es.Search(
    	es.Search.WithIndex("my-index-000001"),
    	es.Search.WithBody(strings.NewReader(`{
    	  "query": {
    	    "match": {
    	      "full_text": "Quick Brown Foxes!"
    	    }
    	  }
    	}`)),
    	es.Search.WithPretty(),
    )
    fmt.Println(res, err)
    const response = await client.search({
      index: "my-index-000001",
      pretty: "true",
      query: {
        match: {
          full_text: "Quick Brown Foxes!",
        },
      },
    });
    console.log(response);
    GET my-index-000001/_search?pretty
    {
      "query": {
        "match": {
          "full_text": "Quick Brown Foxes!"
        }
      }
    }

    term 查询不同,match 查询会在执行搜索之前分析您提供的搜索词 Quick Brown Foxes!match 查询然后返回在 full_text 字段中包含 quickbrownfox 词条的任何文档。

    这是包含结果中索引文档的 match 查询搜索的响应。

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.8630463,
        "hits" : [
          {
            "_index" : "my-index-000001",
            "_id" : "1",
            "_score" : 0.8630463,
            "_source" : {
              "full_text" : "Quick Brown Foxes!"
            }
          }
        ]
      }
    }