21

Elasticsearch クエリに問題があります。結果を並べ替えたいのですが、elasticsearch は並べ替えタグを無視しています。ここで私のクエリ:

{
    "sort": [{
        "title": {"order": "desc"}
    }],
    "query":{
        "term": { "title": "pagos" }
    }
}

ただし、クエリ部分を削除してソートタグのみを送信すると機能します。誰かが私に正しい方法を指摘できますか?

私はまた、私が持っている完全なクエリである次のクエリを試しました:

{
  "sort": [{
    "title": {"order": "asc"}
  }],
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "title":{
              "query":"Pagos",
              "boost":9
            }
          }
        },
        {
          "match":{
            "description":{
              "query":"Pagos",
              "boost":5
            }
          }
        },
        {
          "match":{
            "keywords":{
              "query":"Pagos",
              "boost":3
            }
          }
        },
        {
          "match":{
            "owner":{
              "query":"Pagos",
              "boost":2
            }
          }
        }
      ]
    }
  }
}

設定

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 15,
          "token_chars": [
            "letter",
            "digit",
            "punctuation",
            "symbol"
          ]
        }
      },
      "analyzer": {
        "default" : {
          "tokenizer" : "standard",
          "filter" : ["standard", "lowercase", "asciifolding"]
        },
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding",
            "autocomplete_filter"
          ]
        }
      }
    }
  }
}

マッピング

{
  "objects": {
    "properties": {
      "id":             { "type": "string", "index": "not_analyzed" },
      "type":           { "type": "string" },
      "title":          { "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard" },
      "owner":          { "type": "string", "boost": 2 },
      "description":    { "type": "string", "boost": 4 },
      "keywords":       { "type": "string", "boost": 1 }
    }
  }
}

前もって感謝します!

4

1 に答える 1

25

ドキュメントのフィールド「タイトル」は、分析された文字列フィールドであり、多値フィールドでもあります。これは、elasticsearch がフィールドの内容をトークンに分割し、インデックスに個別に格納することを意味します。おそらく、「タイトル」フィールドを最初の用語でアルファベット順に並べ替え、次に2番目の用語で並べ替える必要がありますが、elasticsearch は並べ替え時にこの情報を自由に使用できません。

したがって、 「タイトル」フィールドのマッピングを次のように変更できます。

{
  "title": {
    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard"
  }
}

次のようなマルチフィールド マッピングに変換します。

{
  "title": {
    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer":"standard",
    "fields": {
      "raw": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}

分析され た「タイトル」フィールドに基づいて検索を実行し、 not_analyzed の「title.raw」フィールドに基づいて並べ替えます

{
    "sort": [{
        "title.raw": {"order": "desc"}
    }],
    "query":{
        "term": { "title": "pagos" }
    }
}

ここで美しく説明されています:文字列の並べ替えとマルチフィールド

于 2015-12-28T14:37:33.287 に答える