0

I recently try migrating from MySQL Full Text Search to ElasticSearch, and I'm a little bit confused with translating some queries.

I have this query.

 "SELECT * FROM Books WHERE MATCH (description) AGAINST ('+Harry +Potter' IN BOOLEAN MODE)"

It means both "Harry" and "Potter" must shown in the description column, regardless the order or the position. (For the sake of example, please assume "Harry" and "Potter" can be independent from each other.)

I tried this using ElasticSearch

{
    "query": {
        "query_string": {
            "query": "Harry Potter",
            "fields": ["description"]
        }
    }
}

but it's still give some result that only contains "Harry" or "Potter".

I tried this one also,

{
    "query": {
        "bool": {
           "must" : {
               "term" : { "description" : "Harry Potter" }
           }
        }
    }
}

this one returns all result contains "Harry Potter", not "Harry Bla Bla Bla Potter" and "Potter Bla Bla Bla Harry".

What is the simplest (or perhaps also fastest) ElasticSearch query that returns the same result with the MySQL query above.

UPDATE

I just found something like this

{
    "query": {
        "match" : {
            "description" : {
                "query" : "Harry Potter",
                "operator" : "and"
            }
         }
    }
}

the result seems about right. But is there other way that is more common?

4

2 に答える 2

2

Visの回答に関する詳細情報

"Harry Potter" も "Harry blabla Potter" に一致させたい場合は、query_string のphrase_slopを 0 よりも大きい値に調整できます。これは、一致した用語間の許容距離です:検索フェーズ間 - 1 に設定すると、その間に 1 つの用語が許可されることを意味するため、"Harry blalal Potter" は一致しますが、"Harry blabla bloblo Potter" は一致しません。- ...

于 2013-09-20T08:01:12.897 に答える
1

すでに見つかったようにデフォルトの演算子を AND に設定するか、または

+Harry +Potter 

query_string クエリで MySQL にも使用した構文。

ElasticSearch はデフォルトで OR を使用するため、"Harry Potter" の query_string クエリは単語の 1 つだけを含む結果を返しました。

用語クエリは、用語クエリに入力した正確な用語がインデックスに含まれている場合にのみ一致します。文字列 "Harry Potter" は "Harry blabla Potter" と一致しないため、一致は得られませんでした。

別の方法が必要な場合は、ブールクエリを使用して、「Harry」用と「Potter」用の 2 つの別々の must 句を含めることができますが、この場合は必要ない場合があります。

于 2013-09-17T12:05:23.730 に答える