且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP弹性搜索过滤的查询字符串搜索

更新时间:2023-02-05 13:02:48

实现此目的的一种方法是使用 muti_fields 映射描述字段。多领域的一个领域应该是不分析的。
数据重新编号后,您可以使用简单的 bool query 以达到您想要的目的:



示例



创建索引

  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
}

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


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


查询:

 数据/ _search 
{
查询:{
已过滤:{
查询:{
bool:{
disable_coord :true,
should:[
{
query_string:{
fields:[
description
] ,
query:a
}
},
{
constant_score:{
filter:{
term:{
description.raw:

},
boost:0.2
}
},
{
constant_score:{
filter
存在:{
field:description
}
},
boost:0.1
}
}
]
}
},
过滤器:{
条款:{
test_id:[
10
]
}
}
}
}
}

结果

 hits:[
{
_index:test,
_type:data,
_id:1,
_sco re: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:{
已过滤:{
查询:{
bool:{
disable_coord:true,
应该:[
{
query_string:{
fields:[
description
],
query
}
},
{
constant_score:{
filter:{
term:{
description 。
}
},
boost:0.2
}
},
{
constant_score {
filter:{
exists:{
field:description
}
},
boost:0.1

}
]
}
},
过滤器:{
条款:{
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:{
描述:a,
test_id:10
}
},
{
_index:test,
_type:data,
_id:3,
_score:0.44721356,
_source {
description:hello,
test_id:10
}
}
]
pre>

All would like to use the filtered query where results should contain data from the "query_string" and also from the "term - filter" applied.

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

The expected result is:

  1. all blog records having letter "a" or "" in it with topic_id is 10.
  2. also rest of the records where topic_id is 10 even if the description is blank/empty.

So final result should be - the matching records with higher score and should come at the top, then the records just matching the "topic_id" from the filter.

One way to achieve this is use muti_fields mapping for description field. One of the fields in multi-field should be non-analyzed. Once the data has been reindexed you can use a simple bool query to achieve what you want :

Example

Create Index:

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

Index Data:

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
}

Query:

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
               ]
            }
         }
      }
   }
}

Results :

 "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 Empty string:

{
   "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
               ]
            }
         }
      }
   }
} 

Result :

  "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
            }
         }
      ]