elasticsearch 如何向elasticsearch查询添加用户定义的字段和值
目标:我想要一个添加鉴别器字段的查询,以区分模糊结果和非模糊结果 考虑这些文件:elasticsearch 如何向elasticsearch查询添加用户定义的字段和值,elasticsearch,elasticsearch,目标:我想要一个添加鉴别器字段的查询,以区分模糊结果和非模糊结果 考虑这些文件: curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d' { "index": { "_index": "dishes", "_type": "dish",
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{
"index": {
"_index": "dishes",
"_type": "dish",
"_id": "1"
}
}
{
"name": "butter chicken"
}
{
"index": {
"_index": "dishes",
"_type": "dish",
"_id": "2"
}
}
{
"name": "chicken burger"
}
'
考虑以下查询:
curl -X POST "localhost:9200/dishes/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"term": {
"name": "burger"
}
},
{
"fuzzy": {
"name": {
"value": "burger"
}
}
}
],
"minimum_should_match": 1,
"boost": 1.0
}
}
}
'
我可以在查询过程中创建一个带有附加标记的结果(它不在文档中),该标记可用于区分模糊结果和非模糊结果
...
"hits" : [
{
"_index" : "dishes",
"_type" : "dish",
"_id" : "2",
"_score" : 1.3862942,
"_source" : {
"name" : "chicken burger"
},
"is_fuzzy": false
},
{
"_index" : "dishes",
"_type" : "dish",
"_id" : "1",
"_score" : 0.46209806,
"_source" : {
"name" : "butter chicken"
},
"is_fuzzy": true
}
]
还有一个选择是使用术语
过滤器,但需要稍微修改:
GET dishes/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"name": {
"value": "burger",
"_name": "not_fuzzy"
}
}
},
{
"fuzzy": {
"name": {
"value": "burger",
"_name": "fuzzy"
}
}
}
],
"minimum_should_match": 1,
"boost": 1
}
}
}
屈服
[
{
"_index":"dishes",
"_type":"dish",
"_id":"2",
"_score":1.3862944,
"_source":{
"name":"chicken burger"
},
"matched_queries":[ <---
"fuzzy",
"not_fuzzy"
]
},
{
"_index":"dishes",
"_type":"dish",
"_id":"1",
"_score":0.46209806,
"_source":{
"name":"butter chicken"
},
"matched_queries":[ <---
"fuzzy"
]
}
]
[
{
“_索引”:“菜品”,
“_type”:“dish”,
“_id”:“2”,
“_分数”:1.3862944,
“_来源”:{
“名称”:“鸡肉汉堡”
},
“匹配的_查询”:[还有一个选项需要使用,但您的术语
过滤器需要稍微修改:
GET dishes/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"name": {
"value": "burger",
"_name": "not_fuzzy"
}
}
},
{
"fuzzy": {
"name": {
"value": "burger",
"_name": "fuzzy"
}
}
}
],
"minimum_should_match": 1,
"boost": 1
}
}
}
屈服
[
{
"_index":"dishes",
"_type":"dish",
"_id":"2",
"_score":1.3862944,
"_source":{
"name":"chicken burger"
},
"matched_queries":[ <---
"fuzzy",
"not_fuzzy"
]
},
{
"_index":"dishes",
"_type":"dish",
"_id":"1",
"_score":0.46209806,
"_source":{
"name":"butter chicken"
},
"matched_queries":[ <---
"fuzzy"
]
}
]
[
{
“_索引”:“菜品”,
“_type”:“dish”,
“_id”:“2”,
“_分数”:1.3862944,
“_来源”:{
“名称”:“鸡肉汉堡”
},
“匹配的查询”:[这不一定是你所要求的,但是你是否考虑过仅仅给精确匹配一个显著的提升,以便它们总是领先于模糊匹配?这不一定是你所要求的,但是你是否考虑过仅仅给精确匹配一个显著的提升,以便它们总是领先于模糊匹配?