小编典典

PHPelasticsearch过滤查询字符串搜索

elasticsearch

所有人都希望使用过滤的查询,其中结果应包含来自“ query_string”和来自“ term-filter”的数据。

GET blog/_search
{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "fields": [ "description" ],
                    "query": "a"                 // or just ""
                }
            },
            "filter": {
                "terms": {
                    "topic_id": [
                        10
                    ]
                }
            }
        }
    }
}

预期结果是:

  1. 所有带有topic_id的字母“ a”或“”的博客记录均为10。
  2. 即使其描述为空白/空,也会记录topic_id为10的其余记录。

因此,最终结果应该是-具有较高分数的匹配记录,并且应该位于顶部,然后才是与过滤器中的“ topic_id”匹配的记录。


阅读 649

收藏
2020-06-22

共1个答案

小编典典

实现此目的的一种方法是对字段使用muti_fields映射description。多字段中的一个字段不应进行分析。数据重新索引后,您可以使用简单的布尔查询来实现所需的目标:

创建索引:

put test
{
    "mappings": {
        "data" : {
            "properties": {
                "description" : {
                    "type": "string",
                     "fields": {
                        "raw" : {"type": "string","index": "not_analyzed"}
                     }
                }
            }   
        }
    }
}

索引数据:

put test/data/1 
{
    "description" : "a",
    "test_id" : 10
}
put test/data/2
{
    "description" : "",
    "test_id" : 10
}

put test/data/3
{
    "description" : "hello",
    "test_id" : 10
}


put test/data/4
{
    "description": "a",
    "test_id" : 20
}

查询:

post test/data/_search
{
   "query": {
      "filtered": {
         "query": {
            "bool": {
               "disable_coord": "true",
               "should": [
                  {
                     "query_string": {
                        "fields": [
                           "description"
                        ],
                        "query": "a"
                     }
                  },
                  {
                     "constant_score": {
                        "filter": {
                           "term": {
                              "description.raw": ""
                           }
                        },
                        "boost": 0.2
                     }
                  },
                  {
                     "constant_score": {
                        "filter": {
                           "exists": {
                              "field": "description"
                           }
                        },
                        "boost": 0.1
                     }
                  }
               ]
            }
         },
         "filter": {
            "terms": {
               "test_id": [
                  10
               ]
            }
         }
      }
   }
}

结果:

 "hits": [
         {
            "_index": "test",
            "_type": "data",
            "_id": "1",
            "_score": 0.5113713,
            "_source": {
               "description": "a",
               "test_id": 10
            }
         },
         {
            "_index": "test",
            "_type": "data",
            "_id": "2",
            "_score": 0.29277003,
            "_source": {
               "description": "",
               "test_id": 10
            }
         },
         {
            "_index": "test",
            "_type": "data",
            "_id": "3",
            "_score": 0.097590014,
            "_source": {
               "description": "hello",
               "test_id": 10
            }
         }
      ]

查询空字符串:

{
   "query": {
      "filtered": {
         "query": {
            "bool": {
               "disable_coord": "true",
               "should": [
                  {
                     "query_string": {
                        "fields": [
                           "description"
                        ],
                        "query": ""
                     }
                  },
                  {
                     "constant_score": {
                        "filter": {
                           "term": {
                              "description.raw": ""
                           }
                        },
                        "boost": 0.2
                     }
                  },
                  {
                     "constant_score": {
                        "filter": {
                           "exists": {
                              "field": "description"
                           }
                        },
                        "boost": 0.1
                     }
                  }
               ]
            }
         },
         "filter": {
            "terms": {
               "test_id": [
                  10
               ]
            }
         }
      }
   }
}

结果:

  "hits": [
         {
            "_index": "test",
            "_type": "data",
            "_id": "2",
            "_score": 1.3416407,
            "_source": {
               "description": "",
               "test_id": 10
            }
         },
         {
            "_index": "test",
            "_type": "data",
            "_id": "1",
            "_score": 0.44721356,
            "_source": {
               "description": "a",
               "test_id": 10
            }
         },
         {
            "_index": "test",
            "_type": "data",
            "_id": "3",
            "_score": 0.44721356,
            "_source": {
              "description": "hello",
               "test_id": 10
            }
         }
      ]
2020-06-22