English 中文(简体)
django / haystack / solr simple config - partial field matching issue
原标题:

I have a simple config of haystack/solr on my django app:

from the models.py of this app:

class device(models.Model):
    ...
    hostname = models.CharField(max_length=45, help_text="The hostname for this device")
    ...

from the search_sites.py of this app:

class devIndex(indexes.SearchIndex):
       Haystack class to allow for indexing device objects in TOMS   
    text = indexes.CharField(document=True, use_template=True)

from templates/search/indexes/systems_management/device_text.txt fo this app (names all jibe)

...
{{ object.hostname }}
...

The Problem:

a system is named static1.foo.com:

if I search for "static", I get results for all static servers ("static" is in their description fields)

if I search for "static1", I get 0 results

if I search for "static1.foo.com" I get results, including this server.

my question is, why is haystack/solr not matching the "static1" query?

问题回答

It s likely an analysis problem. I d guess you are using the StandardTokenizer in your schema.xml file for this field.

The standard tokenizer tokenizes host names as a single token. (ref: http://www.lucidimagination.com/search/document/CDRG_ch05_5.5.1), so you can only match it with the full host name.

If you want to search by pieces, you ll need to use a different tokenizer. The default text field in the Solr example uses the WhitespaceTokenizer and WordDelimeter filter, which will split the host name. This would allow you to find by the query of static1 .

Solr has many configuration possible. For your use case, you may want to user an edge ngram in you schema.xml. Here is an example:

<fieldType name="edge_ngram" class="solr.TextField" positionIncrementGap="1">
  <analyzer type="index">
    <tokenizer class="solr.WhitespaceTokenizerFactory" />
    <filter class="solr.LowerCaseFilterFactory" />
    <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="1" splitOnCaseChange="1"/>
    <filter class="solr.EdgeNGramFilterFactory" minGramSize="4" maxGramSize="15" side="front" />
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.WhitespaceTokenizerFactory" />
    <filter class="solr.LowerCaseFilterFactory" />
    <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="1" splitOnCaseChange="1"/>
  </analyzer>

Use this example and tweak it a little bit untill it returns the desired results.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签