Lar*_*tle 9 query-string elasticsearch
如果不将数字字段转换为字符串,我该如何在其上执行全文搜索?
我正在尝试模仿_all在执行查询时将数字字段动态转换为字符串的行为.
curl -XPUT http://localhost:9200/test/items/1 -d '{accountId : 12341234, name:"Bob"}'
curl -XPUT http://localhost:9200/test/items/2 -d '{accountId : 980987, name:"Marry"}'
curl -XPUT http://localhost:9200/test/items/3 -d '{accountId : 234234, name:"Daniel"}'
Run Code Online (Sandbox Code Playgroud)
找到一个accountId数字4.
我尝试了这两个查询,但收到了0次点击.
查询:
curl -XPOST "http://localhost:9200/test/items/_search" -d '{
"query": {
"term": {
"accountId": "4"
}
}
}'
curl -XPOST "http://localhost:9200/test/items/_search" -d '{
"query": {
"query_string": {
"query": "4"
}
}
}'
Run Code Online (Sandbox Code Playgroud)
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
Run Code Online (Sandbox Code Playgroud)
我建议你为此目的使用ngram tokenizer.
以下是您可能需要的代码示例.您可能希望首先使用要使用的标记器设置分析器设置.
curl -XPUT localhost:9200/test?pretty=true -d '{
"settings":{
"analysis":{
"analyzer":{
"my_ngram_analyzer":{
"tokenizer":"my_ngram_tokenizer"
}
},
"tokenizer":{
"my_ngram_tokenizer":{
"type":"nGram",
"token_chars":[
"letter",
"digit"
]
}
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
更多关于Ngram Tokenizer的信息.
然后你应该定义以下映射:
curl -XPUT localhost:9200/test/items/_mapping?pretty=true -d '{
"items":{
"properties":{
"accountId":{
"analyzer":"my_ngram_analyzer",
"type":"string"
},
"name":{
"type":"string"
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
'accountId'是'字符串'的原因是Ngram tokenizer不适用于数字字段.
现在您可以查询索引:
curl -XGET localhost:9200/test/_search?pretty=true -d'
{
"query": {
"query_string": {
"default_field": "accountId",
"query": "4"
}
}
}'
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到我用来测试它的bash脚本.
注意:当然这只是关于如何使用Ngram Tokenizer的演示.我希望它会有所帮助
创建一个多字段,其中将包含一个数字字段以及一个用于搜索的字符串
PUT /test/items/_mapping
{
"items" : {
"properties" : {
"accountId" : {
"type" : "multi_field",
"fields" : {
"numeric" : {
"type" : "integer",
"index" : "not_analyzed"
},
"text" : {
"type" : "string",
"index" : "analyzed"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
验证您的映射:
GET /test/items/_mapping
Run Code Online (Sandbox Code Playgroud)
输出量
{
"test": {
"mappings": {
"items": {
"properties": {
"accountId": {
"type": "integer",
"index": "no",
"fields": {
"numeric": {
"type": "integer"
},
"text": {
"type": "string"
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将数据放入弹性搜索。
现在您可以以字符串形式进行搜索,但是将以数字形式获得结果:
GET /test/items/_search
{
"query": {
"query_string": {
"default_field": "accountId.text",
"query": "*4*"
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出量
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 4,
"successful": 4,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "items",
"_id": "3",
"_score": 1,
"_source": {
"accountId": 234234,
"name": "Daniel"
}
},
{
"_index": "test",
"_type": "items",
"_id": "1",
"_score": 1,
"_source": {
"accountId": 12341234,
"name": "Bob"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6683 次 |
| 最近记录: |