How to find exact keywords in solr search?

若如初见. 提交于 2021-01-04 08:49:58

问题


Actually I am facing one problem in solr search. There is an author field in which I stored two value like "deep kumar-singh" and "deep kumar singh". When I search the author:"deep kumar-singh" It returns both results. But I want only one result to be the exact one.

Here is my field description:

<field name="author" type="text_general" indexed="true" stored="true" multiValued="true"/>

I created authorFacet Field to get author facet.

<field name="authorFacet" type="string_ci" indexed="true" stored="false" multiValued="true"/>

When I choosed authorFacet it returns count 1 for both author Like:

deep kumar-singh(1)

deep kumar singh(1)

I want only one results to be exact.

How Can I get this?, Any suggestion would be very helpfull.


回答1:


Change the fieldType of author to string from text_general and re-index the data. You would get the desired result. As the field which has the string as its type, it will not create any token of the word and would help to achieve the exact match.

Also, the same can be analysed from the solr admin page. Go to the solr admin page. Select the core/collection. CLick on the analysis. You can select the field and check the index time token and query time if they are matching meeting your expectation.

And with this you don't need 2 different fields for author. You can use one field which has string as field type can be used for both faceting and searching.




回答2:


If you want an exact match then you should use a string field type rather than a text field. String fields are stored as-is, with no transformations made to them at all.

<field name="author" type="string" indexed="true" stored="true" multiValued="true"/>

Keep in mind that if you use string then "abc" will be different from "abc." (notice the extra period) or from "abc " (notice the extra space). The concept of an exact search is actually quite complicated if you want to handle those cases different (see https://stackoverflow.com/a/29105025/446681)




回答3:


You could modify your field type by adding a charFilter in both index and query analyzers like this:

<charFilter class="solr.PatternReplaceCharFilterFactory" pattern="[-]" replacement=""/>

Please note that the charFilter has to be placed before tokenizer. Basically, you are removing hyphens before tokenization.

So, if you search with:

  • "deep", you get "deep kumar-singh" and "deep kumar singh".
  • "kumar-singh", you get "deep kumar-singh" only.
  • "kumar singh", you get "deep kumar singh" only.

I believe, this is what you need.

If you do not want to change text_general field type, you could create another field type with same analyzers as that of text_general and add the charFilter to the new field type.



来源:https://stackoverflow.com/questions/60431935/how-to-find-exact-keywords-in-solr-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!