2017-06-06 8 views
0

私はelasticsearchインデックスのいくつかの文書を持っています。ここではサンプル文書elasticsearch複数の値を持つfeildのブーストクエリ

DOC1はある

{ 
"feild1":["hi","hello","goodmorning"] 
"feild2":"some string" 
"feild3":{} 
} 

DOC2

{ 
"feild1":["hi","goodmorning"] 
"feild2":"some string" 
"feild3":{} 
} 

DOC3

{ 
"feild1":["hi","hello"] 
"feild2":"some string" 
"feild3":{} 
} 

私は "ハイ" と "ハロー" の両方の場合feild1た値を照会したいです存在していればその文書が最初に来るはずです。 たとえば、 の結果は、DOC1、DOC3、DOC2の順になります。私は、ブーストクエリを試みた。それは私が望む順序ではなく、再調整しています。ここで私が試しているクエリです。

{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "match_phrase": { 
         "avail_status": true 
        } 
       }, 
       { 
        "bool": { 
         "should": [ 
          { 
           "constant_score": { 
            "filter": { 
            "terms": { 
            "feild1": [ 
             "hi" 
            ] 
            } 
            }, 
            "boost": 20 
           } 
          }, 
          { 
           "constant_score": { 
            "filter": { 
            "terms": { 
            "feild1": [ 
             "hello" 
            ] 
            } 
            }, 
            "boost": 18 
           } 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
} 

これは最初に "hi"を持つドキュメントと "hello"を持つドキュメントを返します。前もって感謝します!

+0

のために、より大きなサイズのための余分な_scoreを追加しますあなたができることを確認してくださいe doc3とdoc1はcustom_score igonore TDF/IDFの中にフィルターをラップしてから等しいスコアを得ました。私はESがdoc1の上にdoc3を置くという点は見当たりません。配列の上に配列の上にフィールドが多く、配列のフィールドが少なく、一致した値の値を増やしたい場合は、関数のスコアを調べることをお勧めします。あなたのためのこの場合は私に知らせてください – user3775217

+0

はい私はそれだけを探しています、文書は高い一致のスコアを持っています –

+0

私はfuntionのスコア – user3775217

答えて

1

field1のドキュメントに追加の追加機能を追加するには、funtion_scoreのスクリプトスコアを入力します。できれば

マッピング

{ 
    "mappings": { 
    "document_type" : { 
     "properties": { 
     "field1" : { 
      "type": "text", 
      "fielddata": true 
     }, 
     "field2" : { 
      "type": "text" 
     }, 
     "field3" : { 
      "type": "text" 
     } 
     } 
    } 
    } 
} 

インデックス文書機能スコアと

POST custom_score_index1/document_type 

{ 
"feild1":["hi","hello","goodmorning"], 
"feild2":"some string", 
"feild3":{} 
} 

POST custom_score_index1/document_type 

{ 
"feild1":["hi","goodmorning"], 
"feild2":"some string", 
"feild3":{} 
} 

POST custom_score_index1/document_type 

{ 
"feild1":["hi","hello"], 
"feild2":"some string", 
"feild3":{} 
} 

クエリは現在のクエリでフィールド1

POST custom_score_index1/document_type/_search 
{ 
    "query": { 
     "function_score": { 
      "query": { 
       "bool": { 
        "must": [{ 
          "match_phrase": { 
           "avail_status": true 
          } 
         }, 
         { 
          "bool": { 
           "should": [{ 
             "constant_score": { 
              "filter": { 
               "terms": { 
                "feild1": [ 
                 "hi" 
                ] 
               } 
              }, 
              "boost": 20 
             } 
            }, 
            { 
             "constant_score": { 
              "filter": { 
               "terms": { 
                "feild1": [ 
                 "hello" 
                ] 
               } 
              }, 
              "boost": 18 
             } 
            } 
           ] 
          } 
         } 
        ] 
       } 
      }, 
      "functions": [{ 
       "script_score": { 
        "script": { 
         "inline": "_score + 10000 * doc['field1'].length" 
        } 
       } 
      }], 
      "score_mode": "sum", 
      "boost_mode": "sum" 
     } 
    } 
} 
関連する問題