4.2 ElasticSearch搜索之Term Query

1.term
(1).query
将查询语句作为整体进行查询,不对查询语句做分词处理,如elasticsearch中存储了“How are you!”文档,且待查询语句为“How are you”,那么查询结果中不会匹配到“How old are you!”或者“How are you!”。

POST /people/_search
{
	"query": {
		"term": {
			"description": "How are you!"
		}
	}
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

(2).原因分析
elasticsearch对“How are you!”进行分词时,拆成以下3个单词,没有将其作为一个整体存储。

GET /people/_analyze
{
  "text": [ "How are you!"],
  
  "field": "description"
}
{
  "tokens" : [
    {
      "token" : "how",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "are",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "you",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]
}

(3).解决办法

POST /people/_search
{
	"query": {
		"match": {
			"description": "How are you!"
		}
	}
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "zIlIoHsBEsHOdz1YB8pI",
        "_score" : 0.8630463,
        "_source" : {
          "name" : "Deron Williams",
          "country" : "Englend",
          "age" : 32,
          "date" : "1989-01-01",
          "description" : "How are you!"
        }
      }
    ]
  }
}

2.terms
通过terms参数控制查询的query_clause个数。

POST /people/_search
{
	"query": {
		"terms": {
			"country": ["Englend","China"]
		}
	}
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "1111111111",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.example.spring.elasticsearch.People",
          "id" : "1111111111",
          "date" : 599616000000,
          "country" : "Englend",
          "name" : "haha",
          "description" : "How are you!",
          "age" : 20
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "nIm-gXsBEsHOdz1Yrcq7",
        "_score" : 1.0,
        "_source" : {
          "name" : "Deron Williams",
          "country" : "Englend",
          "age" : 32,
          "date" : "1989-01-01",
          "description" : "How are you!"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "Mike Steven",
          "country" : "China",
          "age" : 26,
          "date" : "1995-01-01",
          "description" : "I am Mike Steven."
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "mom6gXsBEsHOdz1YRcov",
        "_score" : 1.0,
        "_source" : {
          "name" : "Mike Sherry",
          "country" : "China",
          "age" : 23,
          "date" : "1998-06-01",
          "description" : "I am Mike Sherry."
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "2222222",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.example.spring.elasticsearch.People",
          "id" : "2222222",
          "date" : "1989-01-01",
          "country" : "Englend",
          "name" : "haha",
          "description" : "How are you!",
          "age" : 20
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "nonCgXsBEsHOdz1YDcoP",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.example.spring.elasticsearch.People",
          "id" : "nonCgXsBEsHOdz1YDcoP",
          "name" : "Mike Owen",
          "age" : "34",
          "date" : "1987-03-12",
          "country" : "Englend",
          "description" : "How are you!",
          "class" : "class com.example.spring.elasticsearch.People"
        }
      }
    ]
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值