Query() public method

public Query ( String field ) : Query
field String
return Lucene.Net.Search.Query
Ejemplo n.º 1
0
    /// <summary>
    /// Gets the items from the GetIndexes()
    /// </summary>
    /// <param name="queryString">
    /// The query string.
    /// </param>
    /// <param name="useQueryParser">
    /// if set to <c>true</c> [use query parser].
    /// </param>
    /// <returns>
    /// Returns Query Result
    /// </returns>
    public QueryResult GetItems(string queryString, bool useQueryParser)
    {
      // Result object used to pass result and errormessages back to sender.
      QueryResult result = new QueryResult();

      List<string> indexes = this.GetIndexes();
      string[] resultItemIds = null;
      foreach (string indexName in indexes)
      {
        string database = "master";
        HighResTimer timer = new HighResTimer(true);

        // get the specified index
        Sitecore.Search.Index searchIndex = SearchManager.GetIndex(indexName);

        // get the database to perform the search in..
        Database db = Factory.GetDatabase(database);

        SearchHits hits;
        try
        {
          if (useQueryParser)
          {
            using (IndexSearchContext searchContext = searchIndex.CreateSearchContext())
            {
              // get a new standard analyser so we can create a query..
              Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
              QueryParser queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, queryString, analyzer);
              Query qry = queryParser.Query("_content");
              hits = searchContext.Search(qry, int.MaxValue);
            }
          }
          else
          {
            using (IndexSearchContext searchContext = searchIndex.CreateSearchContext())
            {
              // perform the search and get the results back as a Hits list..
              hits = searchContext.Search(queryString, int.MaxValue);
            }
          }

          result.Success = true;
          
          resultItemIds = new string[hits.Length];

          int i = 0;

          foreach (Document document in hits.Documents)
          {
            string str = document.Get("_docID");
            resultItemIds[i++] = str;
          }
        }
        catch (ParseException e)
        {
          result.ErrorMessage = e.Message;
          result.ParseException = e;
          result.Success = false;
        }
      }

      result.Result = resultItemIds;
      return result;
    }