簡體   English   中英

PHP彈性搜索過濾查詢字符串搜索

[英]PHP Elastic Search Filtered Query String Search

所有人都希望使用過濾后的查詢,其中結果應包含來自“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”匹配。

實現此目的的一種方法是使用muti_fields映射來description字段。 多字段中的一個字段應該是未分析的。 重新編制數據后,您可以使用簡單的bool查詢來實現您想要的效果:

創建索引:

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

您是否考慮過使用通配符查詢? 檢查此查詢它將適合您。

所有帶有topic_id的字母“a”的博客記錄都是10。

{
  "filter": {
    "and": [
      {
        "in": {
          "topic_id": [
            "10"
          ]
        }
      },
      {
        "query": {
          "filtered": {
            "filter": {
              "bool": {
                "should": [
                  {
                    "query": {
                      "wildcard": {
                        "description": {
                          "value": "*a*"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    ]
  }
}

即使描述為空/空,其余的記錄其中topic_id為10。 這將返回與通配符不匹配的所有其他記錄。

{
  "filter": {
    "and": [
      {
        "in": {
          "topic_id": [
            "10"
          ]
        }
      },
      {
        "not": {
          "query": {
            "filtered": {
              "filter": {
                "bool": {
                  "should": [
                    {
                      "query": {
                        "wildcard": {
                          "description": {
                            "value": "*a*"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}

要僅使用topic_id 10查找空的“”描述字段,請嘗試以下操作,

{
  "filter": {
    "and": [
      {
        "in": {
          "topic_id": [
            "10"
          ]
        }
      },
      {
        "query": {
          "filtered": {
            "filter": {
              "script": {
                "script": "_source.description.length() == 0"
              }
            }
          }
        }
      }
    ]
  }
}

適用於ES 2.x.

使用bool查詢應該可以解決問題。

這是我將使用的查詢:

GET blog/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "fields": [ "description" ],
              "query": "a"
          }
        }
      ],
      "must": [
        {
          "terms": {
            "topic_id": [
              10
            ]
          }
        }
      ]
    }
  }
}

在這里, should在布爾查詢的WHERE子句會告訴Elassticsearch該文檔匹配query_string應返回。 query_string如果要匹配包含a任何文檔,請考慮使用通配符。 例如"query_string": { "query": "*a*" }

另一方面, must子句將告訴我,為了將文檔視為有效匹配,它必須在topic_id字段中包含10 這個should條款可以匹配也可以不匹配。

布爾過濾器

我希望這可以幫到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM