Rakesh Shetty
Rakesh Shetty

Reputation: 4578

How to search from database using solr

Solr Version : 5.0

So I am working on Solr for first time, and really not understand perfectly. Here what I did :-

I have created a core named - search Then my schema.xml file has follwoing code :

<?xml version="1.0" encoding="UTF-8" ?>
<schema name="simple" version="1.5">

    <types>
        <fieldtype name='string' class='solr.StrField' />
        <fieldtype name='long' class='solr.TrieLongField' />
    </types>

<fields>

        <field name='id' type='int' required='true' indexed="true"/>
        <field name='name' type='text' required='true' indexed="true"/>


    </fields>

    <uniqueKey>id</uniqueKey>
    <defaultSearchField>fullText</defaultSearchField>
    <solrQueryParser defaultOperator='OR' />

</schema>

solrconfig.xml :

<?xml version='1.0' encoding='UTF-8' ?>
<config>
    <luceneMatchVersion>5.0.0</luceneMatchVersion>
    <lib dir="../../../../dist/" regex="solr-dataimporthandler-.*\.jar" />

    <requestHandler name="standard" class="solr.StandardRequestHandler" default='true' />
    <requestHandler name="/update" class="solr.UpdateRequestHandler" />
    <requestHandler name="/admin/" class="org.apache.solr.handler.admin.AdminHandlers" />

     <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
      <lst name="defaults">
       <str name="config">db-data-config.xml</str>
      </lst>
    </requestHandler>

    <admin>
        <defaultQuery>*:*</defaultQuery>
    </admin>

</config>

db-data-config.xml :

<dataConfig>
  <dataSource type="JdbcDataSource"
            driver="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/solr"
            user="root"
            password="" /> 
  <document>
    <entity name="users" query="select id,name from users;" />
  </document>
</dataConfig>

I have created a database on PHPmyadmin please find below SG :

enter image description here

when I clicked query on solr panel then it shows empty why ?

enter image description here

Can anyone help me on this, as I am new to solr search. What I am doing wrong ?

Upvotes: 1

Views: 247

Answers (2)

Abhijit Bashetti
Abhijit Bashetti

Reputation: 8658

I dont see a field named "fulltext" in schema.xml but why its defined as the default search

<defaultSearchField>fullText</defaultSearchField>

change it

<defaultSearchField>name</defaultSearchField>

mention the fields in the data config xml

<field column="ID" name="id" /> 
<field column="NAME" name="name" />

your data-config should look alike

<dataConfig>
  <dataSource type="JdbcDataSource"
            driver="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/solr"
            user="root"
            password="" /> 
  <document>
    <entity name="users" query="select id,name from users">
     <field column="ID" name="id" /> 
     <field column="NAME" name="name" />
    </entity>
  </document>
</dataConfig>

add it as in schema.xml

    <types>
        <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/>
    </types>
    <fields>
        <field name='id' type='int' required='true' indexed="true" stored="true"/> 
        <field name='name' type='string' required='true' indexed="true" stored="true"/> 
<fields>

Upvotes: 1

userMadhav
userMadhav

Reputation: 144

Make the changes in your db-data-config.xml similar to what i have done

 <entity name="city_masters" pk="city_id" query="SELECT delete_status as  
city_masters_delete_status,city_id,country_id,city_name,city_updated  from   
city_masters> 
<field column="city_id" name="id"/> 
<field column="city_name" name="city_name" indexed="true" stored="true" />
<field column="country_id" name="country_id" indexed="true" stored="true" />

 <field column="city_masters_delete_status" name="city_masters_delete_status"   

      indexed="true" stored="true" />

     </entity>

You missed out the field column part.Add them like i have done for my code and it should work.If still doesnt work let me know

Upvotes: 0

Related Questions