0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Solrで文字列をSortする

Last updated at Posted at 2025-02-04

Solrで文字列(英字の大文字小文字をlowercaseして)をソートする

  • solr version:9.8.0

既存スキーマーにインデックスがある場合、削除する。

curl-H 'Content-Type: application/json' 'http://localhost:8983/solr/test/update?commit=true' -d '{ delete: { query: "*:*" }}''

管理画面からadd fieldで項目(str1)を追加する。

  • field typeをlowercaseを選択する

image.png

str1という項目にデータを追加

prd_data.json
[
  {
    "item_id" : "001",
    "isbn" : "9784041062050",
    "item_nm" : "異世界迷宮でハーレムを(1)",
    "item_yomi" : "いせかいめいきゅうではーれむを",
    "item_cls" : 1,
    "cont_intr" : "怪しげなウェブサイトでゲームキャラメイクをしたら何故か異世界で目覚めた道夫。しかしその世界の「奴隷」制度を知った道夫はゲーム設定時に獲得したスキルを使い夢のハーレム生活を送るため冒険に出るのだった!",
    "price" : 638,
    "sls_st_dttm" : "2018-01-01",
    "str1" :"aaaa"
  }
,
  {
    "item_id" : "002",
    "isbn" : "9784041067215",
    "item_nm" : "異世界迷宮でハーレムを(2)",
    "item_yomi" : "いせかいめいきゅうではーれむを",
    "item_cls" : 1,
    "cont_intr" : "迷宮探索・盗賊退治と命懸けで金策した道夫は、ついに「ロクサーヌ」購入資金を手に入れる。再び商館を訪れた道夫は無事「ロクサーヌ」の所有者となり、初夜を迎えるべく自分の宿に向かうのだった!?",
    "price" : 638,
    "sls_st_dttm" : "2018-04-26",
    "str1" : "BBBB"
  }
,
  {
    "item_id" : "003",
    "isbn" : "9784041074756",
    "item_nm" : "異世界迷宮でハーレムを(3)",
    "item_yomi" : "いせかいめいきゅうではーれむを",
    "item_cls" : 1,
    "cont_intr" : "ロクサーヌとPTを組んでから迷宮探索も楽になり、宿ではロクサーヌを磨き上げたりと充実したハーレム生活を過ごす道夫。順調に探索が進む道夫は、新たなジョブの獲得を目指すのだった!",
    "price" : 638,
    "sls_st_dttm" : "2018-10-26",
    "str1" : "bbbb"
  }
,
  {
    "item_id" : "004",
    "isbn" : "9784041074763",
    "item_nm" : "異世界迷宮でハーレムを(4)",
    "item_yomi" : "いせかいめいきゅうではーれむを",
    "item_cls" : 1,
    "cont_intr" : "盗賊と戦闘中にボーナス呪文を使った道夫は「魔法使い」のジョブを獲得する。勢いに乗る道夫は、迷宮では魔法を使い、宿ではロクサーヌを磨き上げたりと充実し>たハーレム生活を送るのだった!",
    "price" : 682,
    "sls_st_dttm" : "2019-04-26",
    "str1" : "AAAA"
  }

]

スキーマーにデータを投入する

curl'http://localhost:8983/solr/test/update?commit=true&indent=true' --data-binary @prd_data.json -H 'Content-Type: text/json'

ソートする1

  • str1 asc,item_id asc でソートする
http://localhost:8983/solr/#/test/query?q=*:*&q.op=OR&indent=true&sort=str1%20asc,item_id%20asc&fl=item_id%20str1&useParams=
{
  "responseHeader":{
    "status":0,
    "QTime":19,
    "params":{
      "q":"*:*",
      "indent":"true",
      "fl":"item_id str1",
      "q.op":"OR",
      "sort":"str1 asc,item_id asc",
      "useParams":"",
      "_":"1738686810126"
    }
  },
  "response":{
    "numFound":4,
    "start":0,
    "numFoundExact":true,
    "docs":[{
      "item_id":"001",
      "str1":"aaaa"
    },{
      "item_id":"004",
      "str1":"AAAA"
    },{
      "item_id":"002",
      "str1":"BBBB"
    },{
      "item_id":"003",
      "str1":"bbbb"
    }]
  }
}

ソートする2

  • str1 asc,item_id desc でソートする
http://localhost:8983/solr/#/test/query?q=*:*&q.op=OR&indent=true&sort=str1%20asc,item_id%20desc&fl=item_id%20str1&useParams=
  • 第2ソートキーのprd_idが降順になる
  • str1の英字 Aとa,Bとbの違いが同値として評価されているので第2ソートキーが並び順に影響する
{
  "responseHeader":{
    "status":0,
    "QTime":10,
    "params":{
      "q":"*:*",
      "indent":"true",
      "fl":"item_id str1",
      "q.op":"OR",
      "sort":"str1 asc,item_id desc",
      "useParams":"",
      "_":"1738686810126"
    }
  },
  "response":{
    "numFound":4,
    "start":0,
    "numFoundExact":true,
    "docs":[{
      "item_id":"004",
      "str1":"AAAA"
    },{
      "item_id":"001",
      "str1":"aaaa"
    },{
      "item_id":"003",
      "str1":"bbbb"
    },{
      "item_id":"002",
      "str1":"BBBB"
    }]
  }
}

fielType lowercase

  • filterにlowercaseが付いているのでindex作成で英字は小文字に統一化されてる
  <fieldType name="lowercase" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <tokenizer name="keyword"/>
      <filter name="lowercase"/>
    </analyzer>
  </fieldType>

image.png

  • 追記
    データの不備でソートがわかりにくかったので、データを修正し結果を理解しやすくしました
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?